My second C++ program and I dont know what to do...

Started by
17 comments, last by Liquid Darkness 21 years, 7 months ago
You forgot a ''}'' to close your Add() function. Just add it and everything should work (it did here).

Hope this helps.
Advertisement
You have several major problems here. Take your mon through sun declarations out of your functions and place them before main() (make them global). Make sure you actually remove the declarations from the functions. There are other solutions but this is the simplest to implement. As it stands, your add function is going to add a few undefined variables. Actually doing it this way you don''t need the return statements in the Monday() through Sunday() functions either. Secondly, put brackets around the contents of your final if statement in main. This is why your program closes unexpectedly; the break statement is always executed. Hmm... change "else if(( Input == ''Q'') && (Input == ''q''))" to "else if(( Input == ''Q'') || (Input == ''q''))" (its impossible to have those two keys down at the same time lol).


_____________________________

And the Phoenix shall rise from the ashes...

--Thunder_Hawk -- ¦þ
______________________________
______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Formats______________________________________________________________________________________
I was bored so I decided to rewrite your program. I can explain how it works tomorrow, if you dont have any other way to figure it out. I didn''t feel like fixing the || format problem caused by the different lengths of the days. And there is only one case of error checking so it''ll screw up if you enter lets say ''45'' for a menu selection since I had to use a char type so it would work right whether you entered ''3'' or ''q''. I''m tired, going to sleep.


  #include <iostream>using namespace std;int total = 0;char day[10];InvalidKey(){	char pause;	system("cls");  		cout << "<----------------------------->\n";		cout << "|*---------------------------*|\n";		cout << "||Class Schedule Calculator  ||\n";		cout << "||===========================||\n";		cout << "||                           ||\n";		cout << "||                           ||\n";		cout << "||  Sorry, but that choice   ||\n";		cout << "||   is not on the menu...   ||\n";		cout << "||                           ||\n";		cout << "||     Choose 1-7, or Q      ||\n";   		cout << "||     ----------------      ||\n";		cout << "||                           ||\n";		cout << "||===========================||\n";		cout << "|*---------------------------*|\n";		cout << "|  _   _   _   _     -------  |\n";		cout << "| [1] [2] [3] [4]   ( ENTER ) |\n";		cout << "|  =   =   =   =     =======  |\n";		cout << "| [5] [6] [7] [Q]             |\n";		cout << "|  -   -   -   -              |\n";		cout << "|_____________________________|\n";		cout << " V___________________________V \n";	cout << "Enter any character and press enter to continue: ";	cin >> pause;		return 0;}int DisplayMenu(){	char input;	system("cls");		cout << "<----------------------------->\n";	cout << "|*---------------------------*|\n";	cout << "||Class Schedule Calculator  ||\n";	cout << "||===========================||\n";		cout << "|| 1) Monday                 ||\n";		cout << "|| 2) Tuesday                ||\n";		cout << "|| 3) Wednesday              ||\n";	cout << "|| 4) Thursday               ||\n";		cout << "|| 5) friday                 ||\n";		cout << "|| 6) Saterday               ||\n";    		cout << "|| 7) Sunday                 ||\n";		cout << "|| 8) Add Them All Up!!      ||\n";		cout << "|| Q) Quit                   ||\n";		cout << "|*---------------------------*|\n";		cout << "|  _   _   _   _     -------  |\n";		cout << "| [1] [2] [3] [4]   ( ENTER ) |\n";		cout << "|  =   =   =   =     =======  |\n";		cout << "| [5] [6] [7] [Q]             |\n";		cout << "|  -   -   -   -              |\n";		cout << "|_____________________________|\n";		cout << " V___________________________V \n";	cout << "Total number of classes so far is " << total << ".\n";	cout << "Choose a day: ";		cin >> input;      				return input;}	DisplayClassMenu( char day[10])   {		int classes;		system("cls");  			cout << "<----------------------------->\n";		cout << "|*---------------------------*|\n";		cout << "||Class Schedule Calculator  ||\n";		cout << "||===========================||\n";		cout << "||                           ||\n";		cout << "||                           ||\n";		cout << "||                           ||\n";		cout << "||    Enter The Amount       ||\n";	cout << "||  Of Classes On " << day << ".    ||\n";		cout << "||                           ||\n";   		cout << "||                           ||\n";		cout << "||                           ||\n";		cout << "||===========================||\n";		cout << "|*---------------------------*|\n";		cout << "|  _   _   _   _     -------  |\n";		cout << "| [1] [2] [3] [4]   ( ENTER ) |\n";		cout << "|  =   =   =   =     =======  |\n";		cout << "| [5] [6] [7] [Q]             |\n";		cout << "|  -   -   -   -              |\n";		cout << "|_____________________________|\n";		cout << " V___________________________V \n";			cout << "Classes: ";		cin >> classes;			if( classes > 0 && classes < 8)		total += classes;	else		InvalidKey();		return 0;}	DisplayThankYou(){	cout << "<----------------------------->\n";		cout << "|*---------------------------*|\n";		cout << "||Class Schedule Calculator  ||\n";		cout << "||===========================||\n";		cout << "||                           ||\n";		cout << "||    Thanks For Using       ||\n";		cout << "||   The Class Schedule      ||\n";		cout << "||       Calculator...       ||\n";		cout << "||                           ||\n";		cout << "||                           ||\n";   		cout << "||                           ||\n";		cout << "||                           ||\n";		cout << "||===========================||\n";		cout << "|*---------------------------*|\n";		cout << "|  _   _   _   _     -------  |\n";		cout << "| [1] [2] [3] [4]   ( ENTER ) |\n";		cout << "|  =   =   =   =     =======  |\n";		cout << "| [5] [6] [7] [Q]             |\n";		cout << "|  -   -   -   -              |\n";		cout << "|_____________________________|\n";		cout << " V___________________________V \n";			  			cout << "Total number of classes: " << total << ".\n";		return 0;}int main()   {	char Input;        	  	bool Exit = false;   		while(!Exit)       	{           		Input =  DisplayMenu(); //get the value DisplayMenu returned		   				switch(Input)		{		case ''1'':	DisplayClassMenu("Monday");					break;		case ''2'':	DisplayClassMenu("Tuesday");					break;		case ''3'':	DisplayClassMenu("Wednesday");					break;		case ''4'':	DisplayClassMenu("Thursday");					break;		case ''5'':	DisplayClassMenu("Friday");					break;		case ''6'':	DisplayClassMenu("Saturday");					break;		case ''7'':	DisplayClassMenu("Sunday");					break;				default: if( Input != ''Q'' && Input != ''q'' )					 InvalidKey();		}		if( Input == ''Q'' || Input == ''q'' )			Exit = true;	}	DisplayThankYou();	return 0;}  








i see

if(input>''7'')...

numbers dont need '''' just do > 7 ) it would be fine,
Well I fixed it up A LOT, I actually got it to compile until I took the mon, through sun declairations out of my functions. I put them in front of the main function as Thunder_Hawk said but it wont compile because sun, through mon were all undeclaired identifiers. Anyway, while it WAS working, it went into an endless loop or clearing the screen everytime I quit or put in the wrong number. Other than that everything else worked.


  // This is a Class Schedule Calculation program that should let you input// a number of classes for each day and then tally up the total classes and // display the ammount when you quit the program.#include <iostream>using namespace std;int DisplayMenu()  //creates a funtion to display the menu{      int Input;  //declairing a variable	  system("cls");  //clears the screen	cout << "<----------------------------->\n";	cout << "|*---------------------------*|\n";	cout << "||Class Schedule Calculator  ||\n";	cout << "||===========================||\n";	cout << "|| 1) Monday                 ||\n";	cout << "|| 2) Tuesday                ||\n";	cout << "|| 3) Wednesday              ||\n";	cout << "|| 4) Thursday               ||\n";	cout << "|| 5) friday                 ||\n";	cout << "|| 6) Saterday               ||\n";    //Menu Interface	cout << "|| 7) Sunday                 ||\n";	cout << "|| 8) Add Them All Up!!      ||\n";	cout << "|| Q) Quit                   ||\n";	cout << "|*---------------------------*|\n";	cout << "|  _   _   _   _     -------  |\n";	cout << "| [1] [2] [3] [4]   ( ENTER ) |\n";	cout << "|  =   =   =   =     =======  |\n";	cout << "| [5] [6] [7] [Q]             |\n";	cout << "|  -   -   -   -              |\n";	cout << "|_____________________________|\n";	cout << " V___________________________V \n";    cout << "Choose a day: ";	cin >> Input;      	return Input;   //returns the users input for this funtion}// FUNTIONS FOR THE MENU:int Monday()   //Funtion for monday{	system("cls");  //clear the screen	cout << "<----------------------------->\n";	cout << "|*---------------------------*|\n";	cout << "||Class Schedule Calculator  ||\n";	cout << "||===========================||\n";	cout << "||                           ||\n";	cout << "||                           ||\n";	cout << "||                           ||\n";	cout << "||    Enter The Ammount      ||\n";	cout << "||  Of Classes On Monday:    ||\n";	cout << "||                           ||\n";   //message to the user	cout << "||                           ||\n";	cout << "||                           ||\n";	cout << "||===========================||\n";	cout << "|*---------------------------*|\n";	cout << "|  _   _   _   _     -------  |\n";	cout << "| [1] [2] [3] [4]   ( ENTER ) |\n";	cout << "|  =   =   =   =     =======  |\n";	cout << "| [5] [6] [7] [Q]             |\n";	cout << "|  -   -   -   -              |\n";	cout << "|_____________________________|\n";	cout << " V___________________________V \n";	cout << "Classes: ";	cin >> mon;	return mon;}int Tuesday()   //Funtion for Tuesday{	system("cls");  //clear the screen	cout << "<----------------------------->\n";	cout << "|*---------------------------*|\n";	cout << "||Class Schedule Calculator  ||\n";	cout << "||===========================||\n";	cout << "||                           ||\n";	cout << "||                           ||\n";	cout << "||                           ||\n";	cout << "||    Enter The Ammount      ||\n";	cout << "||  Of Classes On Tuesday:   ||\n";	cout << "||                           ||\n";   //message to the user	cout << "||                           ||\n";	cout << "||                           ||\n";	cout << "||===========================||\n";	cout << "|*---------------------------*|\n";	cout << "|  _   _   _   _     -------  |\n";	cout << "| [1] [2] [3] [4]   ( ENTER ) |\n";	cout << "|  =   =   =   =     =======  |\n";	cout << "| [5] [6] [7] [Q]             |\n";	cout << "|  -   -   -   -              |\n";	cout << "|_____________________________|\n";	cout << " V___________________________V \n";	cout << "Classes: ";	cin >> tue;	return tue;}int Wednesday()   //Funtion for Wednesday{	system("cls");  //clear the screen	cout << "<----------------------------->\n";	cout << "|*---------------------------*|\n";	cout << "||Class Schedule Calculator  ||\n";	cout << "||===========================||\n";	cout << "||                           ||\n";	cout << "||                           ||\n";	cout << "||                           ||\n";	cout << "||    Enter The Ammount      ||\n";	cout << "|| Of Classes On Wednesday:  ||\n";	cout << "||                           ||\n";   //message to the user	cout << "||                           ||\n";	cout << "||                           ||\n";	cout << "||===========================||\n";	cout << "|*---------------------------*|\n";	cout << "|  _   _   _   _     -------  |\n";	cout << "| [1] [2] [3] [4]   ( ENTER ) |\n";	cout << "|  =   =   =   =     =======  |\n";	cout << "| [5] [6] [7] [Q]             |\n";	cout << "|  -   -   -   -              |\n";	cout << "|_____________________________|\n";	cout << " V___________________________V \n";	cout << "Classes: ";	cin >> wed;	return wed;}int Thursday()   //Funtion for Thursday{	system("cls");  //clear the screen	cout << "<----------------------------->\n";	cout << "|*---------------------------*|\n";	cout << "||Class Schedule Calculator  ||\n";	cout << "||===========================||\n";	cout << "||                           ||\n";	cout << "||                           ||\n";	cout << "||                           ||\n";	cout << "||    Enter The Ammount      ||\n";	cout << "||  Of Classes On Thursday:  ||\n";	cout << "||                           ||\n";   //message to the user	cout << "||                           ||\n";	cout << "||                           ||\n";	cout << "||===========================||\n";	cout << "|*---------------------------*|\n";	cout << "|  _   _   _   _     -------  |\n";	cout << "| [1] [2] [3] [4]   ( ENTER ) |\n";	cout << "|  =   =   =   =     =======  |\n";	cout << "| [5] [6] [7] [Q]             |\n";	cout << "|  -   -   -   -              |\n";	cout << "|_____________________________|\n";	cout << " V___________________________V \n";	cout << "Classes: ";	cin >> thu;	return thu;}int Friday()   //Funtion for Friday{	system("cls");  //clear the screen	cout << "<----------------------------->\n";	cout << "|*---------------------------*|\n";	cout << "||Class Schedule Calculator  ||\n";	cout << "||===========================||\n";	cout << "||                           ||\n";	cout << "||                           ||\n";	cout << "||                           ||\n";	cout << "||    Enter The Ammount      ||\n";	cout << "||  Of Classes On Friday:    ||\n";	cout << "||                           ||\n";   //message to the user	cout << "||                           ||\n";	cout << "||                           ||\n";	cout << "||===========================||\n";	cout << "|*---------------------------*|\n";	cout << "|  _   _   _   _     -------  |\n";	cout << "| [1] [2] [3] [4]   ( ENTER ) |\n";	cout << "|  =   =   =   =     =======  |\n";	cout << "| [5] [6] [7] [Q]             |\n";	cout << "|  -   -   -   -              |\n";	cout << "|_____________________________|\n";	cout << " V___________________________V \n";	cout << "Classes: ";	cin >> fri;	return fri;}int Saterday()   //Funtion for Saterday{	system("cls");  //clear the screen	cout << "<----------------------------->\n";	cout << "|*---------------------------*|\n";	cout << "||Class Schedule Calculator  ||\n";	cout << "||===========================||\n";	cout << "||                           ||\n";	cout << "||                           ||\n";	cout << "||                           ||\n";	cout << "||    Enter The Ammount      ||\n";	cout << "||  Of Classes On Saterday:  ||\n";	cout << "||                           ||\n";   //message to the user	cout << "||                           ||\n";	cout << "||                           ||\n";	cout << "||===========================||\n";	cout << "|*---------------------------*|\n";	cout << "|  _   _   _   _     -------  |\n";	cout << "| [1] [2] [3] [4]   ( ENTER ) |\n";	cout << "|  =   =   =   =     =======  |\n";	cout << "| [5] [6] [7] [Q]             |\n";	cout << "|  -   -   -   -              |\n";	cout << "|_____________________________|\n";	cout << " V___________________________V \n";	cout << "Classes: ";	cin >> sat;	return sat;}int Sunday()   //Funtion for Sunday{	system("cls");  //clear the screen	cout << "<----------------------------->\n";	cout << "|*---------------------------*|\n";	cout << "||Class Schedule Calculator  ||\n";	cout << "||===========================||\n";	cout << "||                           ||\n";	cout << "||                           ||\n";	cout << "||                           ||\n";	cout << "||    Enter The Ammount      ||\n";	cout << "||  Of Classes On Sunday:    ||\n";	cout << "||                           ||\n";   //message to the user	cout << "||                           ||\n";	cout << "||                           ||\n";	cout << "||===========================||\n";	cout << "|*---------------------------*|\n";	cout << "|  _   _   _   _     -------  |\n";	cout << "| [1] [2] [3] [4]   ( ENTER ) |\n";	cout << "|  =   =   =   =     =======  |\n";	cout << "| [5] [6] [7] [Q]             |\n";	cout << "|  -   -   -   -              |\n";	cout << "|_____________________________|\n";	cout << " V___________________________V \n";	cout << "Classes: ";	cin >> sun;	return sun;}int Add() //Function to add all the days together{	int Total, mon, tue, wed, thu, fri, sat, sun;	Total = (mon+tue+wed+thu+fri+sat+sun); //Add all the days together	system("cls");  //clears the screen	cout << "<----------------------------->\n";	cout << "|*---------------------------*|\n";	cout << "||Class Schedule Calculator  ||\n";	cout << "||===========================||\n";	cout << "|| 1) Monday                 ||\n";	cout << "|| 2) Tuesday                ||\n";	cout << "|| 3) Wednesday              ||\n";	cout << "|| 4) Thursday               ||\n";	cout << "|| 5) friday                 ||\n";	cout << "|| 6) Saterday               ||\n";    //Menu Interface	cout << "|| 7) Sunday                 ||\n";	cout << "|| 8) Add Them All Up!!      ||\n";	cout << "|| Q) Quit                   ||\n";	cout << "|*---------------------------*|\n";	cout << "|  _   _   _   _     -------  |\n";	cout << "| [1] [2] [3] [4]   ( ENTER ) |\n";	cout << "|  =   =   =   =     =======  |\n";	cout << "| [5] [6] [7] [Q]             |\n";	cout << "|  -   -   -   -              |\n";	cout << "|_____________________________|\n";	cout << " V___________________________V \n";    cout << "You have: ";	cout << Total;	cout << " days of class per week!\n";			return 0;}int mon, tue, wed, thu, fri, sat, sun;int main()   // The main function{      int Input;  //declaire variable 	  while(1)  //while loop      {           Input =  DisplayMenu(); //get the value DisplayMenu returned		   if( Input == 1) Monday();           else if( Input == 2) Tuesday();		   else if( Input == 3) Wednesday();     // Take what the user chose		   else if( Input == 4) Thursday();      // and go to the corisponding function		   else if( Input == 5) Friday();		   else if( Input == 6) Saterday();		   else if( Input == 7) Sunday();		   else if( Input == 8) Add();		   else if((Input > ''7'') || (Input != ''Q'') || (Input != ''q'')) DisplayMenu();			   		   else if(( Input == ''Q'') || (Input == ''q''))		   {				cout << "<----------------------------->\n";				cout << "|*---------------------------*|\n";				cout << "||Class Schedule Calculator  ||\n";				cout << "||===========================||\n";				cout << "||                           ||\n";				cout << "||    Thanks For Using       ||\n";				cout << "||   The Class Schedule      ||\n";				cout << "||       Calculator...       ||\n";				cout << "||                           ||\n";				cout << "||                           ||\n";   //message to the user				cout << "||                           ||\n";				cout << "||                           ||\n";				cout << "||===========================||\n";				cout << "|*---------------------------*|\n";				cout << "|  _   _   _   _     -------  |\n";				cout << "| [1] [2] [3] [4]   ( ENTER ) |\n";				cout << "|  =   =   =   =     =======  |\n";				cout << "| [5] [6] [7] [Q]             |\n";				cout << "|  -   -   -   -              |\n";				cout << "|_____________________________|\n";				cout << " V___________________________V \n";			  					return 0;			  }	  }      return 0;}  
=-=-=-=-=-=-=-=-=-=-=-=-----I am a signature virus. Please add me to your signature so that I may multiply.
quote:Original post by twysted
I was bored so I decided to rewrite your program. I can explain how it works tomorrow, if you dont have any other way to figure it out. I didn''t feel like fixing the || format problem caused by the different lengths of the days. And there is only one case of error checking so it''ll screw up if you enter lets say ''45'' for a menu selection since I had to use a char type so it would work right whether you entered ''3'' or ''q''. I''m tired, going to sleep.


WOW.. thanks a lot... That helped me understand some of it a lot better. I had been wondering about switches too. I''m going to study the code and see what i can figure out.

I''m usually pretty good at figuring things out by seeing examples. In my C++ class I missed when she went over while loops so i was kinda trying to play it by ear.

quote:
numbers dont need '''' just do > 7 ) it would be fine,


Ahh okay thanks...

=-=-=-=-=-=-=-=-=-=-=-=-----I am a signature virus. Please add me to your signature so that I may multiply.
quote:Original post by Liquid Darkness
I''m usually pretty good at figuring things out by seeing examples. In my C++ class I missed when she went over while loops so i was kinda trying to play it by ear.


I suggest you get yourself a couple of good books on C++ - it''s not that easy to play it by ear, or by example.

Unfortunately, I don''t know any good beginners books. I hear that the following is pretty good, though:

''Teach Yourself C++ in 21 Days Complete Compiler Edition (4th Edition)''

- Robert

Where are you taking a C++ class??? Are you in College? Do they actually like go up to the board and teach you, or do they just make you read a book and do its exercises?

What do you mean ''Someday become a programmer?'' I'm a programmer right now!

Actually I have Sam''s teach Yourself C++ in 21 days, 3rd edition. I started reading it a little while ago and I really liked it. I decided to just take a class on it since I was going to be in school anyway but the bok that we are using SUCKS. Its written like a textbook, it has bad examples and its more than confusing.

quote:Original post by Shadow1234567890
Where are you taking a C++ class??? Are you in College? Do they actually like go up to the board and teach you, or do they just make you read a book and do its exercises?



I''m in community college and my teacher has a masters in software engineering but I get the impression she hasnt programed in C++ in a while. Shes just teaching out of the book, when she lectures she just reads powerpoints that came with the book. I''ve asked her questions that she answered WRONG and even I knew the answer to. Either way, I''m taking the class more as motivation to learn. I could teach myself but at least this way I''l have a set amount of time every few days to work just on learning C++.
=-=-=-=-=-=-=-=-=-=-=-=-----I am a signature virus. Please add me to your signature so that I may multiply.

This topic is closed to new replies.

Advertisement