Going Back To The Start OF C++.

S

Sunny123

hello,

i am usung the following code in C++

std::cout<<"\nDo You Want To Solve Another Quadratic
Equation?\nYes(y==Y) No(n==N): ";
fflush(stdin); // flush input buffer
int sel = tolower(getchar());
while(sel != 'y' && sel != 'n')
{
std::cout<<"\nEither Choose \"y or Y\" For \"YES\" OR \"n or N\"
For \"NO\": "<<std::endl;
fflush(stdin); // flush input buffer
sel = tolower(getchar());
}
if(sel == 'y')
{
system("cls");
goto Start;
}
else
return 0;
}

to clear the program and go back to the start of it.

i.e. once you have worked out one quadratic it gives you the option
whether to do another or quit.

is there a better way of writing this code, because my teacher says
that i should not use fflush(stdin);

thanks
 
S

Sunny123

sorry for the repost,

if there is not another code that will do the same thing, can i make
this code any better.

thanks
 
G

Githlar

To flush the input buffer C++ style use this while loop:

while(std::cin.get() != '\n');

To flush the output buffer however, you can use std::cout.flush().

Githlar
 
D

Daniel T.

hello,

i am usung the following code in C++

std::cout<<"\nDo You Want To Solve Another Quadratic
Equation?\nYes(y==Y) No(n==N): ";
fflush(stdin); // flush input buffer
int sel = tolower(getchar());
while(sel != 'y' && sel != 'n')
{
std::cout<<"\nEither Choose \"y or Y\" For \"YES\" OR \"n or N\"
For \"NO\": "<<std::endl;
fflush(stdin); // flush input buffer
sel = tolower(getchar());
}
if(sel == 'y')
{
system("cls");
goto Start;
}
else
return 0;
}

to clear the program and go back to the start of it.

i.e. once you have worked out one quadratic it gives you the option
whether to do another or quit.

is there a better way of writing this code, because my teacher says
that i should not use fflush(stdin);

What were his reasons? If he doesn't want you to mix C IO usage and C++
IO usage, then I suggest looking into how to use 'cin' and istreams.

I can't help but wonder what he thinks about the goto, but I don't want
to start a flame war so maybe I should delete this sentence...
 
S

Sunny123

Daniel said:
What were his reasons? If he doesn't want you to mix C IO usage and C++
IO usage, then I suggest looking into how to use 'cin' and istreams.

I can't help but wonder what he thinks about the goto, but I don't want
to start a flame war so maybe I should delete this sentence...

"I can't help but wonder what he thinks about the goto, but I don't
want
to start a flame war so maybe I should delete this sentence... "

yeah he was saying that this was not a good thing to do.

any ideas would be much appreciated.
 
R

Ron Natalie

Daniel said:
What were his reasons? If he doesn't want you to mix C IO usage and C++
IO usage, then I suggest looking into how to use 'cin' and istreams.

How about the fact that it's undefined behavior. Fflush only does
something on output streams (or streams not being used for input).
Calling fflush in stdin is UNDEFINED BEHAVIOR.
 
S

Sunny123

could someone tell me what each line in the code is

1cout << "\nDoYouWantoSolveAnotherQuadratic Equation?\nYes(y==Y)
No(n==N): ";
2 while(cin.get() != '\n');
3 int ch = tolower(getchar());
4 while(ch != 'y' && ch != 'n')
5 {
6cout << "\nEither Choose \"y or Y\" For \"YES\" OR \"n or N\" For
\"NO\": " << endl;
7 while(cin.get() != '\n');
8 ch = tolower(getchar());
9 }
10 if(ch == 'y')
11 {
12 system("cls");
13 goto Start;
14 }
15 else
16 return 0;
17 }


it is mainly lines 2 and 3, and lines 7 and 8
 
S

Sunny123

could someone tell me what each line in the code is

1cout << "\nDoYouWantoSolveAnotherQuadratic Equation?\nYes(y==Y)
No(n==N): ";
2 while(cin.get() != '\n');
3 int ch = tolower(getchar());
4 while(ch != 'y' && ch != 'n')
5 {
6cout << "\nEither Choose \"y or Y\" For \"YES\" OR \"n or N\" For
\"NO\": " << endl;
7 while(cin.get() != '\n');
8 ch = tolower(getchar());
9 }
10 if(ch == 'y')
11 {
12 system("cls");
13 goto Start;
14 }
15 else
16 return 0;
17 }


it is mainly lines 2 and 3, and lines 7 and 8.

thanks
 
R

REH

Githlar said:
To flush the input buffer C++ style use this while loop:

while(std::cin.get() != '\n');

To flush the output buffer however, you can use std::cout.flush().

Githlar

What is wrong with:

std::cin.sync();

REH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top