New line added in loop

When adding stucts to a vector, middle elements will have a blank line before the getline field output. When trying to compare fields will cause the comparison to fail, causing the program to crash. I am having problems with the nutrient struct specifically, the only difference that I see between the two is the delimiter.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
struct ingredient
    {
        float amount;
        string units;
        string name;
        float calories;
    };


int main (int argc, char** argv)
{
  if (argc != 3)
    {
      cerr << "Usage: " << argv[0] << " nutrientFile recipeFile" << endl;
      return -1;
    }

    ifstream nutrientFile (argv[1]);
    ifstream recipeFile (argv[2]);


   string recipeName;
   
   int servings;
   int i;
   int k;
   int w;
   int counter;
   
   ingredient list;
   vector <ingredient> recipe;
   vector <ingredient> nutrient;

i=0;
k=0;

    recipeFile>>recipeName>>servings;
    recipeFile>>list.amount>>list.units;
    recipeFile>>ws;
    getline (recipeFile, list.name);

    addElement(recipe, k, i, list);



counter=0;

while (recipeFile)
  {
        recipeFile>>list.amount>>list.units;
        recipeFile>>ws;
        getline (recipeFile, list.name);

if(recipeFile.eof())break;
       
        addElement(recipe, i,k, list);

         counter++;

         i++;
         k++;

  }

i=0; //size
k=0; //index

    getline(nutrientFile, list.name, ';');
    nutrientFile>>list.amount>>list.units>>list.calories;
    addElement(nutrient,i,k,list);


while (nutrientFile)
{   

    getline (nutrientFile, list.name,';');
    nutrientFile>>list.amount>>list.units>>list.calories;

if(nutrientFile.eof()) break;

    addElement(nutrient,i,k,list);

       i++;
       k++;
}


k=0;
w=0;

cerr<<"\n-------------------------------";

cerr<<nutrient[0].name<<nutrient[0].amount<<nutrient[0].units<<nutrient[0].calories<<endl;
cerr<<nutrient[1].name<<nutrient[1].amount<<nutrient[1].units<<nutrient[1].calories<<endl;
cerr<<nutrient[2].name<<nutrient[2].amount<<nutrient[2].units<<nutrient[2].calories<<endl;
cerr<<nutrient[3].name<<nutrient[3].amount<<nutrient[3].units<<nutrient[3].calories<<endl;


for(i=0;i<counter;i++)
{


 while(k<recipe.size())
{

        if(recipe[k].units==nutrient[w].units)
            {
                cerr<<"this contains "<<nutrient[w].calories<<endl;
            k++;
            w=0;
            
                 break;
            }
        else
        {

            w++;

        }

            }

}


  return 0;

}


The vector building function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

template <typename T>
void addElement (std::vector<T>& vectr, int& size, int index, T value)
{
  // Make room for the insertion
  int toBeMoved = vectr.size() - 1;
  vectr.push_back(value);

  // Shift elements up to make room
  while (toBeMoved >= index) {
    vectr[toBeMoved+1] = vectr[toBeMoved];
    --toBeMoved;
  }
  // Insert the new value
  vectr[index] = value;





-------------------------------
milk chocolate1bar235

cheese, swiss1oz108

marshmallows1cup159
graham crackers2squares59
this contains 235

Process returned -1073741819 (0xC0000005)   execution time : 3.418 s
Press any key to continue.



Thanks.
Last edited on
Topic archived. No new replies allowed.