473,402 Members | 2,050 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

Going Back To The Start OF C++.

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

Mar 12 '06 #1
8 2773
sorry for the repost,

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

thanks

Mar 12 '06 #2
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
Mar 13 '06 #3
In article <11*********************@j52g2000cwj.googlegroups. com>,
Su******@ntlworld.com wrote:
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...
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Mar 13 '06 #4

Daniel T. wrote:
In article <11*********************@j52g2000cwj.googlegroups. com>,
Su******@ntlworld.com wrote:
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...
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.


"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.

Mar 13 '06 #5
Daniel T. wrote:

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.


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.
Mar 13 '06 #6
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

Mar 13 '06 #7
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

Mar 13 '06 #8
REH

Githlar wrote:
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

Mar 13 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Steve Mauldin | last post by:
This is a standard ASP application that has several pages at the root withthe global.asa. I set a session variable session("accountid") = "123456" within an asp page and then response.redirect to...
7
by: Nick Zdunic | last post by:
I have a remotable object running in my host application. The host starts up and creates the object. Within a method to start the remote object doing its thing it creates an object. ...
132
by: Kevin | last post by:
I don't know if I should even start this topic but here goes. I'm an ex vb6 developer, now developing in C#. The reason why I started developing in C# is because the company that I worked for at...
2
by: nvv via DotNetMonster.com | last post by:
Hi, I am working on a web site which authenticates an user using forms. And once they logout, I observed that, for any reason if they click on "BACK" button of the browser, the user is being taken...
8
by: Ravi Ambros Wallau | last post by:
Hey guys: What can I do when an "Error Creating Control" is displayed on the form (instead of the control), and a tooltip indicating the error never is displayed? Is there some log, some hidden...
12
by: Jeff | last post by:
Looking for your highly subjective opinions (from those of you who have been working extensively with VS 2005). I have a client that needs/wants a complete rewrite of an existing application. I...
28
by: Rickster66 | last post by:
This thread is similar to the thread (Select only 10 columns going back) that I posted regarding the dynamic selection of data going back one month at a time for 12 months. Here is the qiote fron...
22
by: Rickster66 | last post by:
As Instructed this is a new thread regarding my original post: "Select Only 10 Columns Going Back" I'm sorry for the late response. I've been gathering up information and carefully with as much...
1
by: shashi shekhar singh | last post by:
Respected Sir, I am currently facing a problem in disabling the back Button of IExplorer. Actually I have taken two UpdatePanel on the page(Page A). At first when Page(Page A ) loads then Update...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.