May 5, 2018 at 8:20pm May 5, 2018 at 8:20pm UTC
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 May 5, 2018 at 8:35pm May 5, 2018 at 8:35pm UTC
May 5, 2018 at 8:43pm May 5, 2018 at 8:43pm UTC
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 May 5, 2018 at 8:44pm May 5, 2018 at 8:44pm UTC
May 5, 2018 at 8:48pm May 5, 2018 at 8:48pm UTC
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
May 5, 2018 at 8:55pm May 5, 2018 at 8:55pm UTC
Handy Andy,
Thank you for your help!
I was just about to ask about the "count" value
kalik