Programing question

Hello,
Can anyone please give me an example of a program which runs for ever unless the user enters q or Q ?
There are a lot of examples floating around here of this in a larger context, however it should be easy to extract the code. In pseudo-code:

1
2
3
4
5
6
int main() {
    do {
       // stuff here
       // ask user to enter q or Q to quit
    } while( user did not enter 'q' or 'Q' );
}

you can use either a while loop or a do while loop to accomplish this

i prefer the do while loop then you will have to have another loop inside this.

1
2
3
4
5
6
7
char q;
do 
{
 something
something
cout<<"enter Q or q to quit";
}while (i=="Q" || i=="q");
#include<iostream>
using namespace std;
int main()
{
char q;
do
{ int a;

cout <<"enter your number ";
cin>>a;
cout <<"press Q or q to quit ";

} while ( q=='q');
return 0 ;
}



is this what you mean ?
how can I do press q to quit and c to contiue ?

Last edited on
Your above code is missing a cin >> q;

pseudo-code:
1
2
3
4
5
6
7
8
do {
   // stuff
      
   do {
      // ask user to enter C or c to continue, or Q or q to quit
      // read user input HERE
   } while( user input was not 'C', 'c', 'Q', or 'q' );
} while( user input from HERE was not 'Q' or 'q' );   


your initial question was for the user to press Q or q to quit the initial Question.

anything other then q when asked to quit is entered the loop will repeat itself. but the program will pause till something is entered. you can prompt.

using your example of the whole program you could do this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<iostream>
using namespace std;
int main()
{
char q;
do
{ int a;

cout <<"enter your number ";
cin>>a;
cout <<"press q to quit or c to continue";
cin >>q;

} while ( q=='q');
return 0 ;
}

that way the user will either press q or c and if q =q the the program will terminate else continue.
thanks jaydr !
your example has helped me alot .
thanks again !
Topic archived. No new replies allowed.