How to do repetiton by using while loops

I keep try in many way also cannot make the repetition work.can any one show me about this question?Thanks.
Repetition of what? Could you post what you want to do with the loop and some code showing what you tried.
Depends on what you want to do.

You could do something similar to this with a counter:

1
2
3
4
5
6
7
int count = 0;

while (count < 10)
{
    std::cout << count << std::endl;
    ++count;
}
Here is a simple example:
1
2
3
4
5
6
7
8
9
10
11
12
#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
    }
}
SNAP!! LOL!
.......
Last edited on
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
26
27
28
#include <iostream>
using namespace 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;
}

Might do the trick.
Last edited on
Just a reply to Kyon's solution -

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?
What's the problem?

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 
.....
Last edited on
while (key == 'Y' || key == 'y');

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');
I try already...Still is the same..Actually izit must put 'do' if want continue??
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:

http://cplusplus.com/doc/tutorial/control/
Topic archived. No new replies allowed.