Using cin to input the number of times a while loop will run.

I've been trying to Use cin to input the number of times a while loop will run but it never works. I've tried it on a simple program, input a number and have the program count from 1 to that number but every time it only counts to zero. Could someone please tell me what i have wrong, I have tried everything I could think of.


[code]
#include <iostream>

using namespace std;

int main(){

int count = 0;
int userNum = 0;

cout<< "entra numero: ";
cin>> userNum

while(count <= 100){

cout<< count <<endl;

count = userNum + 1;

}


return 0;
}
Last edited on
Hello kalik56,

I think you missed the closing code tag. this link may help http://www.cplusplus.com/articles/z13hAqkS/

You are fine until you get to the while condition. I believe what you want is:
1
2
3
4
5
6
while (count <= userNum)
{
	cout << count << endl;

	count++;
}


Give that a try.

Hope that helps,

Andy
change line count = usernum + 1 ;
with count += usernum + 1 ;

else you have infinite loop

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 
#include <iostream>

using namespace std;

int main(){

	int count = 0;
	int userNum = 0;

	cout<< "entra numero: ";
	cin>> userNum ;

	while( count <= 100 )
	{

		cout<< count <<endl;

		count += userNum + 1;

	}


	return 0;
}
Last edited on
Hello kalik56,

My fix works, but if you want to start at "1" you will need to change where you defined "count" and set it to "1".

Andy
Handy Andy,

Thank you for your help!

I was just about to ask about the "count" value

kalik
Topic archived. No new replies allowed.