Extra line added to stuct field in a vector

Trying to do comparisons using stucts that are in vectors. Except the first element when the stuct.name field is displayed there is a newline inserted prior to the actual field messing up the comparison.

Is this even acceptable code or should I just start over? I have been debugging and the code follows the path that I want it to, however the blank line prior to the name field is escaping all attempts to figure out what I am missing. Any suggestions would be greatly appreciated.

(I added in the vector template that I am using for reference, along with the original input, and output. I posted this request earlier and after rereading the post requirements I tried to parse it down. )


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

    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++;
}

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;


INPUT:
graham crackers; 2 squares 59
milk chocolate; 1 bar 235
cheese, swiss; 1 oz 108
marshmallows; 1 cup 159



milk chocolate1bar235

cheese, swiss1oz108

marshmallows1cup159
graham crackers2squares59

Process returned 0 (0x0)   execution time : 0.071 s
Press any key to continue.


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;
}
vectr.push_back(value); is not the way to make space for your new element - it appends the new element to the end of the vector.

Try just replacing the whole function with a call to vector::insert

Jim
I actually took out that function and it works much better as far as being in the order in which it was input, however I am still having issues with the blank line occurring before the next element. Oddly enough it doesnt add that blank in for the first input, only for the following inputs.

Any thoughts on that?

Thanks for the help so far.
Each line of the input has a '\n' newline character and you're not reading it.
You need to read it after you read the amount, units and colories, and before you read the next string, otherwise it gets included with that.

Add a cin.ignore() call, which skips any newline left on the input.

[Edit] Sorry, that should probably be nutrientFile.ignore()

Jim
Last edited on
graham crackers 2 squares 59
milk chocolate1bar235
cheese, swiss1oz108
marshmallows1cup159
this contains 59
this contains 235

Process returned 0 (0x0)   execution time : 0.077 s
Press any key to continue.


I could hug you right now Jim, I was putting the .ignore command in the wrong spot. Hours wasted but its ok, Im moving forward!

Thanks again!
No worries
Topic archived. No new replies allowed.