Hi! I'm fairly new to C++ and only know basic functions. For my term project, we were tasked to make a cash register program. Everything a cash register does is done and works fine. The only issue now is that when printing the receipt, it only prints the last input. And if I include it in the loop where it gives the options to add items, it shows it in separate bits. How do I go about accomplishing this?
#Need help with printing results from a loop
27 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.
@buoyant plaza
Screenshots!
Your message appears to contain screenshots but no code. Please send code and error messages in text instead of screenshots if applicable!
Can you give a demonstration?
Like showing us what is printed to the terminal for given inputs?
for the second screenshot, its supposed to cout all the items bought altogether. with my current code, all it prints is the last thing that the loop recognizes.
this is the desired output
the input for this 'receipt'
So what is looks like to me is that for each loop, you overwrite the data from the previous loop, as a result, when you eventually print the values, all that is left is the data from the most recent loop
What you need to add is someway to store the values from the loop in some sort of list that was created outside the loop
Then when you print the final receipt, loop over that list to print the receipt data
i tried doing this, but someone said that the values that i used to store my strings in doesn't exist outside the if statement
You have to declare the variables outside the loop and then inside the loop is where you assign them
;compile
int first;
int second;
for (int i = 0; i < 2; i++) {
if (i == 0)
first = 5;
if (i == 1)
second = 7;
}
std::cout << first << second;
Program Output
57
ZeroPointer#6105 | 40ms | c++ | x86-64 gcc 12.2 | godbolt.org
Have you been taught/are you allowed to use vectors or arrays?
havent learned either of those yet unfortunately
for this one as well, i donbt quite understand this yet because i got this from a stackoverflow forum
So for that, outside the loop, just have the line
std::string first, second, thrid, fourth, fifth;
and then inside the loop write
first = one.str();
instead of
std::string first = one.str();
@rich sky thanks so much for the help!! i managed to finish my code and got the receipt part working
thanks for the time and help <33!!
@buoyant plaza Has your question been resolved? If so, run !solved :)