| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
I dont know what Im doing wrong.
This is my code and it compiles but i cant get numbers to be stored in. I get the menu to come up and i try to make a selection but than the menu just loops over again without it asking me to enter in a number for at_bats or walk ect..... Does anybody know how i can get it to do what i want. Thanks
#include <iostream> using namespace std; int displayMenu(void); int main(void) { int menuChoice = 0; menuChoice = displayMenu(); while(menuChoice!=6) { if (menuChoice); menuChoice = displayMenu(); } return 0; } float get_option () { float a, b, c; cout<<"Give me a number"<<endl; cin>>a; return (a); } int displayMenu(void) { float option, hits, at_bats, walks; int retValue = 0; cout<<"Please select a number from the list below:"<<endl; cout<<"1. Enter total number of at bats"<<endl; cout<<"2. Enter total number of hits"<<endl; cout<<"3. Enter total number of walks"<<endl; cout<<"4. Display batting average"<<endl; cout<<"5. Display on base percentage"<<endl; cout<<"6. Exit"<<endl; cin>>retValue; return(retValue); option=get_option (); int menuChoice (int choice); { float option, hits, walks, at_bats; if (option==1.0) { cout<<"How many at bats"; cin>>at_bats; } else if (option == 2.0) { cout<<"How many hits"; cin>>hits; } else if (option == 3.0) { cout<<"How many walks"; cin>>walks; } else if (option == 4.0) { cout<<"Your batting average is"<<hits/at_bats<<endl; } else if (option == 5.0) { cout<<"Your on base percentage is"<<(hits+walks)/at_bats<<endl; } else { cout<<"Goodbye"<<endl; } } } |
|
#2
|
|||
|
|||
|
Hey leftd90! Your code wasn't all wrong...just missplaced. I rewrote it for you below. Try this to see how it goes.
Good Luck Anibal. Code:
#include <iostream>
using namespace std;
int displayMenu(void);
void menuChoice(int);
//global variables...(could be a global struct and pass a pointer also!)
float hits, walks, at_bats;
int main(void)
{
int menu = 0;
int option;
menu = displayMenu();
while((menu != 6) && (menu > 0) && (menu < 7))
{
menuChoice(menu);
menu = displayMenu();
}
cout<<"Goodbye"<<endl;
return 0;
}
//<-----------------end of main ------------->
//<-----------------declaration of functions------------->
int displayMenu(void)
{
int retValue = 0;
cout<<"Please select a number from the list below:"<<endl;
cout<<"1. Enter total number of at bats"<<endl;
cout<<"2. Enter total number of hits"<<endl;
cout<<"3. Enter total number of walks"<<endl;
cout<<"4. Display batting average"<<endl;
cout<<"5. Display on base percentage"<<endl;
cout<<"6. Exit"<<endl;
cin>>retValue;
return(retValue);
}
void menuChoice (int option);
{
//simply to give the user time to see the statistics
int waitkey
switch(option)
{
case 1: cout<<"How many at bats";
cin>>at_bats;
break;
case 2: cout<<"How many hits";
cin>>hits;
break;
case 3: cout<<"How many walks";
cin>>walks;
break;
case 4: cout<<"Your batting average is"<<hits/at_bats<<endl;
cout<<"Press a Key";
cin>>waitkey;
break;
case 5: cout<<"Your on base percentage is"<<(hits+walks)/at_bats<<endl;
cout<<"Press a Key";
cin>>waitkey;
break;
default: break;
}
}
|
|
#3
|
|||
|
|||
|
Im getting these errors
assign4.cpp:44: parse error before `{' token assign4.cpp:51: syntax error before `>>' token assign4.cpp:55: syntax error before `>>' token assign4.cpp:59: syntax error before `>>' token assign4.cpp:62:31: warning: multi-line string literals are deprecated assign4.cpp:64: syntax error before `<<' token assign4.cpp:65: syntax error before `>>' token assign4.cpp:68:31: warning: multi-line string literals are deprecated assign4.cpp:70: syntax error before `<<' token assign4.cpp:71: syntax error before `>>' token |
|
#4
|
|||
|
|||
|
Hey! Sorry...I accidently wrote a ; at the end of line 44 ( void menuChoice(int option);)
remove the ; and you shouldn't have any problems Anibal. PS: when you find an error, check the line. Generaly is either that line (44) or the line before. I'm happy to help you, but you need to learn how to debug errors on your own! |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > I dont know what Im doing wrong. |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|