I'm new to C++ and I'm having trouble understanding how to determine what to put inside a loop. I know it differs from program to program, but a lot of my assignments deal with the user inputting a number of how many numbers they want to input (it can be any number)

For example, I'm working on an assignment that asks the user how many numbers they want to enter. The user then enters any number they want. My teacher wants us to then use a for loop to ask the user to enter numbers. For example:

"How many numbers would you like to enter?" 3
"Enter a number" 3
"Enter a number" 5
"Enter a number" 7

^The loop should stop here, but it keeps asking the user to enter more numbers...This is what I have for code so far.

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
	int number, value;
		cout << "How many numbers do you wish to enter? ";
		cin >> number;
		for (number = 0; number == number; number++)
		{
			cout << "Enter a number: ";
			cin >> value;
		}

	getch();
	return 0;
}

i am newbie too. so not totally sure if i am giving u the right answer.but i think the part where u have number == number. u can try to put a limit there. like number <=5 or something.of course this need to be verified by a professor of c++.

You need to read a tutorial or book about how to write for loops. The for loop has three parts
1. The sttement that initializes the loop counter and/or other variables

2. The test for a condition which will terminate the loop.

3. Increment or decrement the loop counter

Any or all of the three parts are optional and may be replaced with just ; character

For example if you want to enter a certain quantity of numbers

int num = 3;
for(int i = 0; i < num; i++)

The above for loop
1. Declares and initializes a loop counter i. If you need to use the value of the loop counter later on in the program, outside the loop, then you will have to declare the variable before the loop starts.
2. Checks if the value of i is less than the value of num. The
loop terminates when this condition is true.
3. Increments the loop counter.

Now you should be able to change your program so that it meets the above requirements.

Edited 6 Years Ago by Ancient Dragon: n/a

Once you have entered "number", you should not change it again. You then need to use a different variable, usually called 'i' (but it can be called anything you like), to control your loop.

cout << "How many numbers: ":
cin >> number;
for (int i = 0; i < number; ++i) {
  /* do something */
} //end for loop

Here is a link to a tutorial on control structures. You will find
information on several different things, including the for loop, in it.

EDIT:
Rats, AD beat me to it.

>>The loop terminates when this condition is true.
Doesn't it terminate when the condition is false?

Edited 6 Years Ago by Fbody: n/a

Thanks for the help. I actually, figured it out on my own and now the loop works, but now I'm trying to add all of the inputted numbers together for a sum. (I'm doing the assignment in parts).

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
	int UserInput, number, i;
	int sum = 0; 
		cout << "How many numbers do you wish to enter? ";
		cin >> UserInput;
		for (i = 0; i < UserInput; i++)
		{
			cout << "Enter a number: ";
			cin >> number;
			sum = number + number;
		}
			
		
		cout << "The sum is: " << sum << endl;
		
	
	getch();
	return 0;
}

Because of the loop, the sum is the last number that was typed by the user, but I'm not sure how to fix that.

This article has been dead for over six months. Start a new discussion instead.