how to stop this program

Apr 19, 2009 at 1:52am
can anyone tell me please, how to stop this program.
It should finish for q and give the result for c.

#include<iostream>
using namespace std;
int main()
{
char ch;
cout<<"press c to continue or q to quit ";
cin>>ch;
do{
cout<<"hello world 1";
cout<<endl;
}
while(ch=='c');
return 0;
}
Apr 19, 2009 at 1:58am
You need to move your "do {" up two lines.
Apr 19, 2009 at 2:05am
But this still gives the same result for q ad c
for q it should not show the result

#include<iostream>
using namespace std;
int main()
{
char ch;
do{
cout<<"press c to continue or q to quit ";
cin>>ch;

cout<<"hello world 1";
cout<<endl;
}
while(ch=='c');
return 0;
}
Apr 19, 2009 at 2:32am
Please use the code formatting tool when posting your code so it's easier to refer to line numbers ("#" in the Format menu on the right).

You will need to change the flow a bit:

1
2
3
4
ask the question
while (response is OK)
    print hello world
    ask the question


HTH
Apr 19, 2009 at 2:39am
sorry sir i didn't know
I am new here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<iostream>
using namespace std;
int main()
{
char ch;
do{
cout<<"press c to continue or q to quit ";
cin>>ch;

cout<<"hello world 1";
cout<<endl;
}
while(ch=='c');
return 0;
}



can you be a little specific ?
Last edited on Apr 19, 2009 at 2:52am
Apr 19, 2009 at 8:44am
I think your problem is when you type in 'q', the program outputs "Hello world 1" and then quits, while you want it to quit immediately after you type in 'q', right?

If this is the case, you should modify your code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<iostream>
using namespace std;

int main()
{
char ch;
do   
{
cout<<"hello world 1";
cout<<endl;

cout<<"press c to continue or q to quit ";
cin>>ch;   //ask for input just before checking the condition
}
while(ch=='c');

return 0;
}



Topic archived. No new replies allowed.