[SOLVED]allow user to exit program at any time

is there a way to allow the user to exit a program at any time they wi

Page 1 of 1

14 Replies - 2366 Views - Last Post: 08 October 2009 - 10:50 AM Rate Topic: -----

#1 woodpryan  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 65
  • Joined: 30-April 09

[SOLVED]allow user to exit program at any time

Posted 07 October 2009 - 10:58 PM

I am a beginning C++ user. I wrote this program to calculate a budget. it error traps invalid responses and send the user back to the beginning of the program. It also allows the user to re-run the program without having to restart the program. But, during the process of debugging this program, I descovered that it gets pretty annoying after a while that I have to go through the entire program in order to exit. is there any way to allow the user to input the character "q" at any time durring the program to exit? here is my code. It's pretty lengthy so bear with me. I have no idea where i would need to put the command or where the best place would be or if it even exists. Thanks for your help.
#include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
using namespace std;
int main()
{
	char choice;
	double hours1, hours2,grosspay1, grosspay2, rate, grosspay, bamatax, fica, fedtax, Netincome, wperyear;
		cout<<"Welcome to Viaticus 2.1."<<endl<<endl;
		cout<<"Would you like to calculate your budget? |Y|es or |N|o ";
		cin>>choice;
		while(choice!='N'  && choice!='Y')
		{
		cout<<"invalid response. Restarting Program."<<endl;
		cout<<"Would you like to calculate your budget? |Y|es or |N|o "<<endl;
		cin>>choice;
		continue;
		}
		while(choice=='Y')
		{
		cout<<"Please enter total hours worked in the first week.\t";
		cin>>hours1;
		cout<<"Please enter total hours worked for the second week.\t";
		cin>>hours2;
		cout<<"Please enter pay rate.\t\t\t\t\t";
		cin>>rate;
		wperyear=26;
		if(hours1 > 40)
			grosspay2=(hours1-40)*(1.5*rate)+(40*rate);
		else
			grosspay2= hours1*rate;
		if(hours2 > 40)
			grosspay1=(hours2-40)*(1.5*rate)+(40*rate);
		else 
			grosspay1= hours2*rate;
		grosspay=grosspay1+grosspay2;

		if (grosspay*wperyear<106800)
			fica= (grosspay*0.0765);
		else
			fica= (106800*.0765)/wperyear;
			
				if (grosspay*wperyear <= 500)
					bamatax=(grosspay*wperyear*.02)/wperyear;
				else if (500<grosspay*wperyear && grosspay*wperyear<=3000)
				bamatax=((grosspay*wperyear-500)*.04+10)/wperyear;
				else
					bamatax=((grosspay*wperyear-3000)*.05+110)/wperyear;
				if (grosspay<=276)
					fedtax= 0;
				else if (276<grosspay && grosspay<=400)
					fedtax= ((grosspay-276)*.10);
				else if (400<grosspay && grosspay<=1392)
					fedtax= ((grosspay-400)*.15+12.4);
				else if (1392<grosspay && grosspay<=2559)
					fedtax=((grosspay-1392)*.25+161.20);
				else if (2559<grosspay && grosspay <=6677)
					fedtax=((grosspay-2559)*.28+452.95);
				else if (6677<grosspay && grosspay<=14423)
					fedtax=((grosspay-6677)*.33+1605.90);
				else	
					fedtax=((grosspay-14423)*.35+4162.17);
			Netincome= (grosspay-fica-bamatax-fedtax);
		cout<<"Gross Income= $\t\t\t"<<setprecision(2)<<showpoint<<fixed<<grosspay<<endl<<endl;
		cout<<"Fica tax = $\t\t"<<fica<<endl<<endl;
		cout<<"alabama inc tax= $\t"<<bamatax<<endl<<endl;
		cout<<"Federal inc tax= $\t"<<fedtax<<endl<<endl;
		cout<<"Net/take home pay= $\t\t"<<Netincome<<endl<<endl;
		
	double totalExpenses, gasAndSmokes, carInsurance, rent, utilities, food, loans, cableInternet, misc, savings;

		cout<<"please enter the following expenditures for a two week period:"<<endl<<endl;
		cout<<"gas and cigarettes.\t\t";
		cin>>gasAndSmokes;
		cout<<"rent.\t\t\t\t";
		cin>>rent;
		cout<<"utilities.\t\t\t";
		cin>>utilities;
		cout<<"car insurance.\t\t\t";
		cin>>carInsurance;
		cout<<"food.\t\t\t\t";
		cin>>food;
		cout<<"loans/other money owed.\t\t";
		cin>>loans;
		cout<<"cable and internet.\t\t";
		cin>>cableInternet;
		cout<<"miscilanious.\t\t\t";
		cin>>misc;
		totalExpenses=gasAndSmokes+carInsurance+loans+rent+utilities+food+cableInternet+misc;
		cout<<"your total expenses for the two week period are:\t"<<totalExpenses<<endl<<endl;
		savings=Netincome-totalExpenses;
		if(savings>0)
			cout<<"I recommend that you put the following in your savings account:\t"<<savings<<endl<<endl;
		else
			cout<<"I recomend that you lower your expenses by at least:\t"<<savings*-1<<endl<<endl;
		cout<<"Would you like to re-calculate your budget? |Y|es or |N|o ";
		cin>>choice;
		while(choice!='Y'  && choice!='N')
		{
		cout<<"invalid response! Can't you follow directions? enter Y for Yes or N for No! ";
		cin>>choice;
		continue;
		}
	}
return 0;
}


This post has been edited by woodpryan: 08 October 2009 - 09:11 AM


Is This A Good Question/Topic? 0
  • +

Replies To: [SOLVED]allow user to exit program at any time

#2 prog_dude  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 10-September 09

Re: [SOLVED]allow user to exit program at any time

Posted 08 October 2009 - 12:11 AM

use ctrl+pause break to come out of your output anytime while it is still running

This post has been edited by prog_dude: 08 October 2009 - 12:17 AM

Was This Post Helpful? 0
  • +
  • -

#3 woodpryan  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 65
  • Joined: 30-April 09

Re: [SOLVED]allow user to exit program at any time

Posted 08 October 2009 - 12:15 AM

View Postprog_dude, on 7 Oct, 2009 - 11:11 PM, said:

put whole code of your main inside a do-while loop as follows:

is there a second part to this response? there is no code there. :(
Was This Post Helpful? 0
  • +
  • -

#4 woodpryan  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 65
  • Joined: 30-April 09

Re: [SOLVED]allow user to exit program at any time

Posted 08 October 2009 - 12:26 AM

i don't know if that works in Windows or not, but i do know that it doesn't work in Linux as I just attempted it. also, Mac OS X users do not have a pause/break key. I would like the user to be able to simply press "Q" at any point in time during the running of the program and exit the program. is there any way to do that?
Was This Post Helpful? 0
  • +
  • -

#5 aks29921  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 84
  • View blog
  • Posts: 230
  • Joined: 24-August 09

Re: [SOLVED]allow user to exit program at any time

Posted 08 October 2009 - 12:31 AM

looks like someone edited their response..
however, his idea was right, if you have to do it programmatically try something like
int main()
{
do
{
//your existing code here
}
while (kbhit() && getch()=='q')
return 0;


am not 100% sure about this but it may be worth a try..

edit: i am sorry, instead of checking getch=='q', you must check for the ascii value of q (or Q) as getch returns the integer value (just remembered this)

This post has been edited by aks29921: 08 October 2009 - 12:40 AM

Was This Post Helpful? 0
  • +
  • -

#6 woodpryan  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 65
  • Joined: 30-April 09

Re: [SOLVED]allow user to exit program at any time

Posted 08 October 2009 - 12:47 AM

View Postaks29921, on 7 Oct, 2009 - 11:31 PM, said:

looks like someone edited their response..
however, his idea was right, if you have to do it programmatically try something like
int main()
{
do
{
//your existing code here
}
while (kbhit() && getch()=='q')
return 0;


am not 100% sure about this but it may be worth a try..

edit: i am sorry, instead of checking getch=='q', you must check for the ascii value of q (or Q) as getch returns the integer value (just remembered this)

i'm confused. what do i need to declare at the top? do i need to include another header file?
Was This Post Helpful? 0
  • +
  • -

#7 aks29921  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 84
  • View blog
  • Posts: 230
  • Joined: 24-August 09

Re: [SOLVED]allow user to exit program at any time

Posted 08 October 2009 - 01:05 AM

i meant you to try this:
#include<iostream.h>
#include<string.h>
#include<iomanip.h>
#include<math.h>
#include<conio.h>//for getch() & kbhit()
int main()
{
	char choice;
	double hours1, hours2,grosspay1, grosspay2, rate, grosspay, bamatax, fica, fedtax, Netincome, wperyear;
do
{
		cout<<"Welcome to Viaticus 2.1."<<endl<<endl;
		cout<<"Would you like to calculate your budget? |Y|es or |N|o ";
		cin>>choice;
		while(choice!='N'  && choice!='Y')
		{
		cout<<"invalid response. Restarting Program."<<endl;
		cout<<"Would you like to calculate your budget? |Y|es or |N|o "<<endl;
		cin>>choice;
		continue;
		}
		while(choice=='Y')
		{
		cout<<"Please enter total hours worked in the first week.\t";
		cin>>hours1;
		cout<<"Please enter total hours worked for the second week.\t";
		cin>>hours2;
		cout<<"Please enter pay rate.\t\t\t\t\t";
		cin>>rate;
		wperyear=26;
		if(hours1 > 40)
			grosspay2=(hours1-40)*(1.5*rate)+(40*rate);
		else
			grosspay2= hours1*rate;
		if(hours2 > 40)
			grosspay1=(hours2-40)*(1.5*rate)+(40*rate);
		else
			grosspay1= hours2*rate;
		grosspay=grosspay1+grosspay2;

		if (grosspay*wperyear<106800)
			fica= (grosspay*0.0765);
		else
			fica= (106800*.0765)/wperyear;
		  
				if (grosspay*wperyear <= 500)
					bamatax=(grosspay*wperyear*.02)/wperyear;
				else if (500<grosspay*wperyear && grosspay*wperyear<=3000)
				bamatax=((grosspay*wperyear-500)*.04+10)/wperyear;
				else
					bamatax=((grosspay*wperyear-3000)*.05+110)/wperyear;
				if (grosspay<=276)
					fedtax= 0;
				else if (276<grosspay && grosspay<=400)
					fedtax= ((grosspay-276)*.10);
				else if (400<grosspay && grosspay<=1392)
					fedtax= ((grosspay-400)*.15+12.4);
				else if (1392<grosspay && grosspay<=2559)
					fedtax=((grosspay-1392)*.25+161.20);
				else if (2559<grosspay && grosspay <=6677)
					fedtax=((grosspay-2559)*.28+452.95);
				else if (6677<grosspay && grosspay<=14423)
					fedtax=((grosspay-6677)*.33+1605.90);
				else	
					fedtax=((grosspay-14423)*.35+4162.17);
			Netincome= (grosspay-fica-bamatax-fedtax);
		cout<<"Gross Income= $\t\t\t"<<setprecision(2)<<showpoint<<fixed<<grosspay<<endl<<endl;
		cout<<"Fica tax = $\t\t"<<fica<<endl<<endl;
		cout<<"alabama inc tax= $\t"<<bamatax<<endl<<endl;
		cout<<"Federal inc tax= $\t"<<fedtax<<endl<<endl;
		cout<<"Net/take home pay= $\t\t"<<Netincome<<endl<<endl;
	  
	double totalExpenses, gasAndSmokes, carInsurance, rent, utilities, food, loans, cableInternet, misc, savings;

		cout<<"please enter the following expenditures for a two week period:"<<endl<<endl;
		cout<<"gas and cigarettes.\t\t";
		cin>>gasAndSmokes;
		cout<<"rent.\t\t\t\t";
		cin>>rent;
		cout<<"utilities.\t\t\t";
		cin>>utilities;
		cout<<"car insurance.\t\t\t";
		cin>>carInsurance;
		cout<<"food.\t\t\t\t";
		cin>>food;
		cout<<"loans/other money owed.\t\t";
		cin>>loans;
		cout<<"cable and internet.\t\t";
		cin>>cableInternet;
		cout<<"miscilanious.\t\t\t";
		cin>>misc;
	   totalExpenses=gasAndSmokes+carInsurance+loans+rent+utilities+food+cableInternet+misc;
		cout<<"your total expenses for the two week period are:\t"<<totalExpenses<<endl<<endl;
		savings=Netincome-totalExpenses;
		if(savings>0)
			cout<<"I recommend that you put the following in your savings account:\t"<<savings<<endl<<endl;
		else
			cout<<"I recomend that you lower your expenses by at least:\t"<<savings*-1<<endl<<endl;
		cout<<"Would you like to re-calculate your budget? |Y|es or |N|o ";
		cin>>choice;
		while(choice!='Y'  && choice!='N')
		{
		cout<<"invalid response! Can't you follow directions? enter Y for Yes or N for No! ";
		cin>>choice;
		continue;
		}
	} }
while (kbhit() && getch()==113);
return 0;
}


This post has been edited by aks29921: 08 October 2009 - 01:19 AM

Was This Post Helpful? 0
  • +
  • -

#8 woodpryan  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 65
  • Joined: 30-April 09

Re: [SOLVED]allow user to exit program at any time

Posted 08 October 2009 - 01:06 AM

if i use a do while loop structure, the code that i post after the "do" will run no matter what. the condition for the "do while" structure is not checked for untill the code is executed at least once. but i want the user to be able to quit the program at any time. "do while" will surely not work for what i want to do. i need to get some sleep for class tomorrow, but i will be back online tomorrow with updates on my progress. thanks for the help. feel free to post any ideas while i'm asleep. i will read them before i get back to work on this program.
Was This Post Helpful? 0
  • +
  • -

#9 woodpryan  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 65
  • Joined: 30-April 09

Re: [SOLVED]allow user to exit program at any time

Posted 08 October 2009 - 01:16 AM

i get the error conio: no such file or directory. i run linux and my compiler is g++. this might have something to do with it. all subsequent errors are caused by this error and it's pointless to list them.
Was This Post Helpful? 0
  • +
  • -

#10 aks29921  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 84
  • View blog
  • Posts: 230
  • Joined: 24-August 09

Re: [SOLVED]allow user to exit program at any time

Posted 08 October 2009 - 01:23 AM

also i believe you have not initialized your variables showpoint and fixed...


Quote

if i use a do while loop structure, the code that i post after the "do" will run no matter what. the condition for the "do while" structure is not checked for untill the code is executed at least once. but i want the user to be able to quit the program at any time. "do while" will surely not work for what i want to do.

in that case use the while loop instead of do-while so that the condotion is checked before the code is executed...

This post has been edited by aks29921: 08 October 2009 - 01:24 AM

Was This Post Helpful? 0
  • +
  • -

#11 woodpryan  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 65
  • Joined: 30-April 09

Re: [SOLVED]allow user to exit program at any time

Posted 08 October 2009 - 01:27 AM

showpoint, fixed, and setprecision are initialized in the header #include<iomanip> i would try a while loop instead of do while except that the header you suggested is not available for my compiler.
Was This Post Helpful? 0
  • +
  • -

#12 aks29921  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 84
  • View blog
  • Posts: 230
  • Joined: 24-August 09

Re: [SOLVED]allow user to exit program at any time

Posted 08 October 2009 - 01:47 AM

View Postwoodpryan, on 8 Oct, 2009 - 12:27 AM, said:

showpoint, fixed, and setprecision are initialized in the header #include<iomanip> i would try a while loop instead of do while except that the header you suggested is not available for my compiler.

well it gave errors on showpoint and fixed in borland turbo c++ when i tried to compile it.


edit: it is true that conio.h is not available in your compiler i myself have never used it...
guess you need to start a separate thread on using getch() & kbhit() in g c c or try googling a bit... lolz..
however, this link might help you a bit
http://www.justlinux...nbuffered+input
i suggest you look at ncurses.h closely

This post has been edited by aks29921: 08 October 2009 - 01:56 AM

Was This Post Helpful? 0
  • +
  • -

#13 Hyper_Eye  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 39
  • View blog
  • Posts: 116
  • Joined: 13-September 07

Re: [SOLVED]allow user to exit program at any time

Posted 08 October 2009 - 01:57 AM

You don't even need code to do this. Ctrl+C will quit your application on any platform at anytime. If you want to do something before exiting you can even implement a sig handler to catch SIGINT and you would then be able to perform a task before exiting (such as a farewell message.) It is late and I haven't really looked at your code so I am not going to provide an example right now but if you do a search for "sig handler" you should find plenty to read up on. If not I will be happy to provide a simple example of a sig handler that is appropriate for your program.
Was This Post Helpful? 0
  • +
  • -

#14 chls89  Icon User is offline

  • New D.I.C Head

Reputation: 12
  • View blog
  • Posts: 47
  • Joined: 19-September 09

Re: [SOLVED]allow user to exit program at any time

Posted 08 October 2009 - 03:03 AM

I didn't read through all of the responses. You would need some sort of an event handler, or as the person above me posted a sig handler. Event-driven programming seems to me, however, to be more in the domain of languages like Java, where what you are asking to do is extremely easy to implement. I have been in school for a few years now and in fact have not as of yet heard of event-driven programming in C++. You may wish to look into sig handlers (I don't know of them), but you best bet might be to leave such sorts of things for a Java program, unless you are willing to invest the time to find something that is uncommon in this language.
Was This Post Helpful? 0
  • +
  • -

#15 Hyper_Eye  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 39
  • View blog
  • Posts: 116
  • Joined: 13-September 07

Re: [SOLVED]allow user to exit program at any time

Posted 08 October 2009 - 10:50 AM

You can develop event-driven code in C++. A quick google search reveals this article: http://www.artima.co...sp?thread=84958

Many C++ GUI libraries are event driven. One I am greatly familiar with, wxWidgets, is event driven.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1