i am trying to figure out how to do this question it says that i should do a switch and a if statement
Things On Us wants you to write a program to display fruit names and prices. If user enter
Letter Fruit Price
O or o Orange 0.99 each
G or g Grapes 1.49/ Lb.
M or m Mango 1.00 each
If user enter any other letter the program must display error message, please enter correct fruit code
Write program using switch and if statements.
oh ok now i get it. i did this program also. i compiled ok but when i test the program to put in my first name it does not prompt me to do the last name, nor to enter other information.
Not sure why it doesn't work, still learning this stuff myself, very complicated compared with the BASIC language I am familiar with.
It worked like this but not how you would like to input the data.
After some searching for solutions this seems to work but not sure why. It seems to use stringstream as a strange way to convert a string to a number type.
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
int hrsWorked;
string firstName, lastName;
double payRate, taxRate, grossPay, taxAmount, netWeeklyPay;
char jobStatus;
//user inputs
cout << "Please enter your Name: ";
cin >> firstName;
cout << "Please enter your Last Name: ";
cin >> lastName;
cout << "Are you Fulltime (F) or Parttime (P) ?";
cin >> jobStatus;
cout << "Please enter your payRate: ";
cin >> payRate;
cout << "Please enter total hours of work: ";
cin >> hrsWorked ;
grossPay = payRate * hrsWorked;
taxRate = 0.12;
taxAmount = grossPay * taxRate;
netWeeklyPay = grossPay - taxAmount;
if(hrsWorked > 40)
cout << "You will get Over Time Pay" << endl;
else
cout << "You will not get Over Time Pay" << endl;
cout<<"Employee's First Name: = "<<firstName<<endl;
cout<<"Employee's Last Name = "<<lastName<<endl;
cout<<"Hours Worked = "<<hrsWorked<<endl;
cout<<"Pay Rate = "<<payRate<<endl;
cout<<"Tax Rate = "<<taxRate<<endl;
cout<<"Gross Pay = "<<grossPay<<endl;
cout<<"Tax Amount = "<<taxAmount<<endl;
cout<<"Net Weekly Pay = "<<netWeeklyPay<<endl;
system("pause");
return 0;
}
You had a number of errors:
1. Names etc are strings unless you were asking for employee numbers which are unlikely to be 'doubles'.
2. Your if statement wasn't wrong so much as badly constructed - unnecessarily complicated.
3. Your char variables were wrong - look carefully how they are tidied up, corrected and rationalized. :)