Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,673 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,176 people online right now. Registration is fast and FREE... Join Now!




Repeating loop

 
Reply to this topicStart new topic

Repeating loop, Prompting the user

cRaZi_RiCaN
post 9 Jun, 2007 - 04:51 PM
Post #1


New D.I.C Head

*
Joined: 25 May, 2007
Posts: 11


My Contributions


Hello I'm trying to make my program ask the "do you want to do this again question?" so the user doesn't have to re run my program between calculations. I tried a series of loops but all of them end up skipping the actual program and just asking the same question over and over again.

Whats the prefered loop for this function or any other way to do it besides a loop?
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 9 Jun, 2007 - 04:58 PM
Post #2


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,178



Thanked 33 times

Dream Kudos: 25
My Contributions


If the number of loops is unknown, I would suggest a while or do while loop:

CODE

string answer = "yes";
while(answer=="yes")
{
   //perform calulcations
   cout<<"Do you wish to continue?(yes/no)"<<endl;
   cin>>answer
}

User is offlineProfile CardPM

Go to the top of the page

PennyBoki
post 9 Jun, 2007 - 05:03 PM
Post #3


system("revolution");

Group Icon
Joined: 11 Dec, 2006
Posts: 2,009



Thanked 5 times

Dream Kudos: 500

Expert In: Java,C++,C

My Contributions


Hi, maybe if we see some code your question would be more clear, this way I can guess that most of your program should be in a loop whose condition would determine whether the program will start again or will be terminated.
User is offlineProfile CardPM

Go to the top of the page

cRaZi_RiCaN
post 9 Jun, 2007 - 05:09 PM
Post #4


New D.I.C Head

*
Joined: 25 May, 2007
Posts: 11


My Contributions


Ok thanks also i dont want to make another topic but i was searching eysterday and didnt find a command/function from the conio.h header that lets me clear the screen. Is there substitues for system("cls");?

that'd be all, thanks!
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 9 Jun, 2007 - 06:03 PM
Post #5


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,178



Thanked 33 times

Dream Kudos: 25
My Contributions


There are no standard screen clearing functions - they are compiler/platform dependent. What compiler/operating system are you using?

You can always write a loop of newlines, but dependent functions do exist.
User is offlineProfile CardPM

Go to the top of the page

cRaZi_RiCaN
post 10 Jun, 2007 - 03:14 AM
Post #6


New D.I.C Head

*
Joined: 25 May, 2007
Posts: 11


My Contributions


I'm using Dev-c++ and windows OS.
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 10 Jun, 2007 - 06:53 AM
Post #7


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,178



Thanked 33 times

Dream Kudos: 25
My Contributions


The you should be fine with the system("cls") call...is there a reason you do not want to use? You'll need to include <cstdlib> for c++, stdlib.h for c.
User is offlineProfile CardPM

Go to the top of the page

cRaZi_RiCaN
post 10 Jun, 2007 - 11:26 AM
Post #8


New D.I.C Head

*
Joined: 25 May, 2007
Posts: 11


My Contributions


Not a particular reason just that since theres is a substitute for system("pause"); (getch(); ) i thought the same for cls, Also system cls works for me without the <cstdlib> header.

This post has been edited by cRaZi_RiCaN: 10 Jun, 2007 - 11:27 AM
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 10 Jun, 2007 - 11:37 AM
Post #9


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,178



Thanked 33 times

Dream Kudos: 25
My Contributions


Those are not substitutes...they are not related in any way, it simply seems that way from the actions performed.

getch() is a c function that takes user input from the buffer or stream. system("pause") is a call to an existing executable present on some platforms.

Neither of those methods is compliant to ANSI standards, however...the use of either will limit your application portability.
User is offlineProfile CardPM

Go to the top of the page

cRaZi_RiCaN
post 10 Jun, 2007 - 11:39 AM
Post #10


New D.I.C Head

*
Joined: 25 May, 2007
Posts: 11


My Contributions


Oh ok, didn't know that thanks.

User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/23/08 06:18AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month