#Need help with printing results from a loop

27 messages · Page 1 of 1 (latest)

buoyant plaza
#

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?

golden sundialBOT
#

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!

rich sky
#

Can you give a demonstration?
Like showing us what is printed to the terminal for given inputs?

buoyant plaza
#

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

buoyant plaza
rich sky
#

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

buoyant plaza
#

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

rich sky
#

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;
worthy yewBOT
#
Program Output
57
rich sky
#

Have you been taught/are you allowed to use vectors or arrays?

buoyant plaza
buoyant plaza
rich sky
#

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();
buoyant plaza
#

@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!!

rich sky
#

No problem

#

Also I like your avatar

#

It's cute

golden sundialBOT
#

@buoyant plaza Has your question been resolved? If so, run !solved :)

buoyant plaza
#

thank u its killjoy from valorant,, not my art tho i i just found it on twt by Cut3knife

#

!solved