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. )
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;
}
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.
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()
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!