#include <iostream>
int main()
{
int i = 0;
while(i < 10) // inside the {} will repeat as long as i is less than 10
{
std::cout << i << std::endl; // print i
++i; // add one to i
}
}
#include <iostream>
usingnamespace std;
int main()
{
char contine = 'y';
while( continue == 'y') // While we want the program to continue (which is on at the start)
{
int num, rev;
cout << "Please enter an integer: ";
cin >> num;
cout << "Your reversed number is ";
while( num>0 )
{
rev = num % 10;
num=num / 10;
cout << rev;
}
cout <<"\n";
do // Repeat whats in the code block following while the input is not correct (not y and not n)
{
cout << "Redo? (Y/N)\n";
cin >> continue;
} while( (continue != 'y') and (continue!='n') );
cout << "\n";
}
return 0;
}
Your "continue" variable declaration was misspelt "contine" (just a minor mistake), but also, you shouldn't use continue as a variable as it is a reserved keyword (see: http://www.cplusplus.com/doc/tutorial/variables/).
Galik,
Haha 99% similar codes with 1 minute difference xD
Thanks for all your comment...I got try to use the Kyon's solution but it does not work...Although i already change the "contine" to other, but it still cannot work...Anyone still got other comment?
Kyon's solution is pseudo-code, such as while ( (continue != 'y') and (continue != 'n') );, if that's the problem (not compiling). You're going to have to convert it to C++.
I dint really understand what you mean...And when i compile the program got so many error...Actually izit must use do while loops if want to do continue for the program??
Would be good to post some of the errors you are getting.. how are we gonna know? Vision through your eyes to your computer?
Since your question is repetition with while loops, I'm assuming you need to use while loops. A while loop and a do while loop are very similar. The only difference is that a do while loop always operates once even if the while condition is already fulfilled beforehand, whilst a while loop won't operate if its condition isn't fulfilled beforehand.
1 2 3 4 5 6
int i = 100;
while (i < 10)
{
//do something
}
//while loop doesn't operate
1 2 3 4 5 6
int i = 100;
do
{
//do something
} while (i < 10);
//goes through once before it reaches condition and stops
Remember a while loop needs { } for its scope. In that line, if key is y or Y, it will just be stuck. A while loop only needs a ; at the end if its part of a do while loop.
Try putting a do above cout << "Enter a number : "; and make the parantheses enclose from that line to the beginning of the while (key == 'Y' || key == 'y');
1 2 3 4 5 6
do
{
cout << "Enter a number : ";
//...
cin >> key;
} while (key == 'Y' || key == 'y');
It sounds like you're just very confused about the most basic concepts in programming. Have you tried reading a manual or tutorial? Here's one about loops and control structures from this site: