I am gonna start off saying that I am new to C++, and I have little programming knowledge (just Q Basic, and it has been a while since I have used that), and I need help on an assignment in my C++ class.
We have to use a case function to collect information for an order and display the total, but I have no clue how to get the case to work at all. I am sure the solution is simple, but as I have said I really do not know much about what I am doing.
And... here is the code:
double S=2.50;
double C=1.00;
double B=2.00;
double R=1.00;
double L=1.50;
double &T;
int menu();
int choices();
int main()
{
menu();
choices();
system ("pause");
return 0;
}
int choices()
{
for (n=1, n++)
{
do
{
case 'S'
case 's':
T=T+S;
break;
case 'C'
case 'c':
T=T+C;
case 'B'
case 'b':
T=T+B;
break;
case 'R'
case 'r':
T=T+R;
break;
case 'L'
case 'l':
T=T+L;
break;
case 'X'
case 'x'
T=0;
cout << "The Order Has Been Canceled. \n"
break;
}
}
}
I already have the int menu() function working fine, it is the case stuff I am struggling with.
I don't understand your for loop (which is also syntactically erronous) , or the do loop. Just get rid of them for now, and implement them later if need be.
Hmm you are using the switch syntax wrong...try this:
switch(tolower(choice))
{ case 's':
T=T+S;
break;
case 'c':
T=T+C;
case 'b':
T=T+B;
break;
case 'r':
T=T+R;
break;
case 'l':
T=T+L;
break;
case 'x'
T=0;
cout << "The Order Has Been Canceled. \n"
break;
default:
cout << "Invalid choice";
break;
}
Also, you should either be passing choice an argument, or have a cin input a char to use in the switch statement. Perhaps have menu() return a char, and pass it to choices for testing. Here's what I mean :
#include <iostream>
using std::cin;
using std::cout;
double S=2.50;
double C=1.00;
double B=2.00;
double R=1.00;
double L = 1.50;
double T(0);
char Menu();
void Choices(char);
int main()
{
Choices(Menu());
return 0;
}
char Menu()
{
char cChoice(0);
cout << "Menu:\n\nA)Do Something\nB)Do Something\nC)Quit\n\nChoice:";
do {
cin.clear();
cin.ignore(cin.rdbuf()->in_avail());
cin >> cChoice;
} while (!cin.fail() && cin.rdbuf()->in_avail() > 1);
//just makes sure you didn't screw up the input buffer with some crappy input
return cChoice;
}
void Choices(char choice)
{
switch(tolower(choice))
{ case 's':
T=T+S;
break;
case 'c':
T=T+C;
case 'b':
T=T+B;
break;
case 'r':
T=T+R;
break;
case 'l':
T=T+L;
break;
case 'x':
T=0;
cout << "The Order Has Been Canceled. \n";
break;
default:
cout << "Invalid choice";
break;
}
}
Don't expect this to compile; I didn't test it. But try and learn from it, as you had alot of syntax errors!