loop

What is the best loop that i need to use for this kind of situation:

1.user input the quantity for the drink/s
2.user input the flavour for the drink/s

im new at c++, but i need to submit this assignment -,-
the loop is at the process of choosing the flavour

We need more info. Specifically, on what condition should the loop terminate?

There are 3 basic loops.
1 - while loops: Used in cases where there is a possibility that the body of the loop should never be run. The while condition is tested before the body of the loop is run.

2 - do{} while loops: Use when the body of the loop should always be executed at least once. The while condition is tested after the body is run.

3 - for() loops: Used when the number of times you will iterate through the loop is known beforehand. Often used to iterate through arrays, vectors, and other list structures.

These are broad guidelines for which loop to use, not strict rules. Knowing which loop to use will come with experience.


<edit>Nevermind, I missread your post in haste. Since you know the number of drinks use a 'for' loop.
for( int i = 0; i < numDrinks; i++)
Last edited on
the loop wil be terminated after the user finish choosing the flavour, n the output (price) is displayed
tqvm! now i can send my assignment T__T
Not sure if you saw my edit above. What you want to use is a for loop since you know the number of drinks.

something like this:
1
2
3
4
5
for( int i = 0; i < numDrinks; i++ )
{
	// ask user for flavor of drink i
	// output cost of drink i
}


the best thing about this assignment, instead of choosing the flavour, the user also can choose the topping. As a beginner, i hate this assignment.
closed account (1CfG1hU5)
the for loop, while loop, and do while loops all have same stuff. pick one.

for (i = 0; i < 3, i++) // if only one line under for loop, then no braces needed
{
...
}
while (i < 3) // like mid of for loop, int or char i declaration should come before loop
{
...
i++; // incrementer/decrementer etc has to be added unlike for loop has this
}

do
{
...
i++; // like while loop has to be added
} while (i < 3);

Topic archived. No new replies allowed.