#1
I can't for the life of me remember the code to give the user an option to rerun or stop the program.

thanks
Method of Groove




"Hardcore without punk isn't music, it's a genre of porn.
Punk isn't a genre of music, it’s a thought process."
#3
yeah i guess. i havent used c++ in about 5 years.

i basically want to ask "would you like to rerun the program? y-n"

y will rerun and n will close it
Method of Groove




"Hardcore without punk isn't music, it's a genre of porn.
Punk isn't a genre of music, it’s a thought process."
#4
Just make a loop. i.e.

int main (){

while(temp)
{
//code here
int temp = 0;
cout << "Type 0 and enter to quit program, or 1 to re-run ";
cin >> temp;
if(temp = 0)
return 0;
else
temp = 1;
}

Might want to put some error checking though in case user doesn't put in an integer.
my MG15DFX has a button that simulates the sound of one of the expensive tube marshall amps


Fender Stratocaster HSS
LTD EC-400AT
Traynor YCV-50 Blue
Peavey Envoy 110

Wishlist: Hamer USA Explorer, Gibson Explorer
#5
As far as I know, you'd have to put the program in an entire while loop

char answer = 'y'

while (answer == 'y')
{

//Your entire program here

cout << "Would you like to rerun the program?" << endl;
cin >> answer
}

Something like that.
Or you may wanna test the condition at the end instead of the beginning, that might be better.

You also might wanna take it in as a string instead of a char, and use the toUpperCase (I think that was it, I know thats it in Java) so that whether they enter upper or lowercase it'll still work.
#6
If you want to have it run once, put it in a do{...}while loop. ^ Like he said.

EDIT: Have it run *at least* once, I mean.
#7
well you'll need a variable that will store user input

put everything in a while loop:

bool exit = false;
char user_input;

while(!exit){

// Program

cout << "blablabla would you like to rerun the program? Y/N" << endl;
cin >> user_input;

if( user_input == 'N' ){
exit = true;
}
}

something like that?

if you enter anything other than N, it will jump to the beginning of the while loop.
[img]http://img300.imageshack.us/img300/3760/356h356h365pc1.png[/img]
Die Ruhe vor dem Sturm.
#8
"The easiest way would probably be to put your code into a loop and put an if-statement on the end to determine if the user wants to rerun the program. If he doesnt the code inside the if-statement will break the loop and the program will exit.

Kind of like this:
CODEint main()
{
int exit;
while(1==1)
{
cout << "Hello"<<endl;
cout << "Hello"<<endl;
cout << "Hello"<<endl;
cout << "Would you like to exit?"<<endl;
cin >> exit;

if(exit == 1)
{
break;
}
}


return 0;
}
This asks the user if he wants to quit. If he enters 1, the app will quit. Does he enter anything else than 1 it will continue because 1 is always equal to 1.
Good luck mate"

I didn't write this but a quick google search found it. Looks like it is what you need. credit to Moezzie over at dreamincode.net
Quote by dubstar92
A few years a ago, I played with it alot and got my time down to 42 secs. Right now, I'm probably around a 55 sec average.
#9
Oh, and BTW, Gamedev.net is a great place for advice on stuff like this (they have a general programming forum as well).
#10
You could do it a number of ways, the most efficient it a while loop tho.............as many examples has stated above how to do it.
#11
Quote by identityxcrysis
As far as I know, you'd have to put the program in an entire while loop

char answer = 'y'

while (answer == 'y')
{

//Your entire program here

cout << "Would you like to rerun the program?" << endl;
cin >> answer
}

Something like that.
Or you may wanna test the condition at the end instead of the beginning, that might be better.

You also might wanna take it in as a string instead of a char, and use the toUpperCase (I think that was it, I know thats it in Java) so that whether they enter upper or lowercase it'll still work.



this is the way i was familiar with.
but it puts me into an infinite loop when i enter y or n
Method of Groove




"Hardcore without punk isn't music, it's a genre of porn.
Punk isn't a genre of music, it’s a thought process."
#12
int enddata(int *answer)
{
printf("Wanna insert another data?");
scanf("%c", &answer);
if (*answer == "y")
return 1
else
return 0;
}

int main()
{
int answer;
while (enddata(answer))
{


Not sure that the syntax and pointers are fine. Anyway, you understand the algorithm.
||||||||||||||||||||
||||||||||||||||||||
||||||||||||||||||||

Last edited by urik at Sep 25, 2008,
#13
Quote by alm0st a skat3r
this is the way i was familiar with.
but it puts me into an infinite loop when i enter y or n

Prolly because you're doing while (temp = 'y') rather than while(temp == 'y').

That's my guess.
my MG15DFX has a button that simulates the sound of one of the expensive tube marshall amps


Fender Stratocaster HSS
LTD EC-400AT
Traynor YCV-50 Blue
Peavey Envoy 110

Wishlist: Hamer USA Explorer, Gibson Explorer
#14
Quote by MustangSVT
Prolly because you're doing while (temp = 'y') rather than while(temp == 'y').

That's my guess.


nope i have:

char answer = 'y';
while(answer == 'y')
{
Method of Groove




"Hardcore without punk isn't music, it's a genre of porn.
Punk isn't a genre of music, it’s a thought process."
#15
This is a program that I have written
See if this helps


	ofstream outputFile;
	char answer = 'Y';
	char reminder[256];
	while ((answer == 'y') || (answer == 'Y'))
	{
	outputFile.open("C:\\Documents and Settings\\JOe\\Start Menu\\Programs\\Startup\\Reminder.vbs" , std::ios::app);
	cout << "Enter the event to be reminded of and press enter." << endl;
	cin.getline(reminder,256);
	outputFile << "msgbox(\"" << reminder << "\")" << endl;
	outputFile.close();
	cout << "Would you like to enter another reminder event? (Y/N)";
	cin >> answer;
	cin.get();
	}
#17
Try throwing in a cin.get(); after your cin >> answer;
could do the trick.
I believe I had the same problem for a while that you're having.
#19
This works. I tested it.

#include "iostream.h"

int main()
{
        char answer = 0;

        while(answer != 'y') {
                cout << "End program (y/n) ? --> ";
                cin >> answer;
        }

        cout << "Bubbye now\n";

        return 0;
}

If that doesn't work for you, what compiler are you using ?
#20
dev c++

apparently a shitty free one cause nothing is working
Method of Groove




"Hardcore without punk isn't music, it's a genre of porn.
Punk isn't a genre of music, it’s a thought process."
#22
Quote by iimjpii
Hm...

USE JAVA

:P

are you an idiot? cause that is something an idiot would say. java is so limited in comparison
Ibanez RG350ex
#23
Quote by bono_b
are you an idiot? cause that is something an idiot would say. java is so limited in comparison


proof please.
#24
Quote by bono_b
are you an idiot? cause that is something an idiot would say. java is so limited in comparison



I wouldn't say that....


Quote by iimjpii
Hm...

USE JAVA

:P


Everyone has their own preference and knowledge to languages. Don't tell him to use Java just because.

If it's a generally simple program, c++ is way better because commands are a hell of a lot shorter than in Java.
If it's working for him, don't tell him to switch programs.

Anyway, have you made any progress on your program?
Wanna share the source and maybe we can take a look and find the error?

Oh and, Visual Studio Express = free and a good C++ compiler
#25
I think what everyones forgetting is
you have to include the appropriate headers for
cin and cout
EDIT: I see someone posted a code sample with the include BUT they never declared which namespace to use. And 90% of functions in iostream.h require the std namespace.

//Insert at very top of code.
#include <iostream>
using namespace std;

//this is where main is which contains any of the loops posted before

or alternatively
#include <iostream>
//and stick std:: infront of every cin/cout call (e.g. std:: cout >> "IM A WEINER")
▼▲
Quote by shdowfox17
awesome avatar,denn0069!
Quote by Valo
The most truth I've heard in the pit.
ever.
▼▲
Last edited by denn0069 at Sep 25, 2008,