need help badly

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.
Who are "Things On Us" ?
So what have you done so far?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;

int main(){

    char item = ' ';

    cout<<"Please enter O G or M and press Enter. ";
    cin >> item ;
    item = toupper(item);
    cout << endl << item << endl;
    if(item == 'O') {
        cout<<"Orange 0.99 each";
    } else if(item == 'G') {
        cout<<"Grapes 1.49/Lb.";
    } else if(item == 'M') {
        cout<<"Mango 1.0 each";
    } else {
        cout<<"invalid letter";
    }

    return 0;
}

for switch statement

#include <iostream>

using namespace std;

int main ( )
{
char O, o, M, m, G, g;




cout<<"Letter Fruit Price"<<endl;
cout<<"O or o Orange 00.99"<<endl;
cout<<"G or g Grapes 01.49"<<endl;
cout<<"M or m Mango 01.00"<<endl;


int code;
cout << "Enter the fruit code: " ;
cin>> code;

switch (code)
{
case 1:
if (code = o || O)
cout <<"Orange" << endl;
break;

case 2:
cout <<"Grapes" <<endl;

break;

case 3:
cout <<"Mango" <<endl;

break;

default:
cout <<"please enter correct fruit code" <<endl;
}
system ("pause");
return 0;

}
sorry this is what i have for switch statement

#include <iostream>

using namespace std;

int main ( )
{
char O, o, M, m, G, g




cout<<"Letter Fruit Price"<<endl;
cout<<"O or o Orange 00.99"<<endl;
cout<<"G or g Grapes 01.49"<<endl;
cout<<"M or m Mango 01.00"<<endl;


int code;
cout << "Enter the fruit code: " ;
cin>> code;

switch (code)
{
case O: case o:
cout <<"Orange" << endl;
break;

case G: case g:
cout <<"Grapes" <<endl;

break;

case M: case m:
cout <<"Mango" <<endl;

break;

default:
cout <<"please enter correct fruit code" <<endl;
}
system ("pause");
return 0;

}
Yes you can use case M: case m:
Below I have just converted any lowercase to uppercase.
What is the line char O, o, M, m, G, g for?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// for switch statement

#include <iostream>

using namespace std;

int main ( )
{

cout<<"Letter Fruit Price"<<endl;
cout<<"O or o Orange 00.99"<<endl;
cout<<"G or g Grapes 01.49"<<endl;
cout<<"M or m Mango 01.00"<<endl;


char code;
cout << "Enter the fruit code: " ;
cin  >> code;
code = toupper(code);

switch (code)
{
    case 'O':
    cout <<"Orange" << endl;
    break;

    case 'G':
    cout <<"Grapes" <<endl;
    break;

    case 'M':
    cout <<"Mango" <<endl;
    break;

    default:
    cout <<"please enter correct fruit code" <<endl;
}
system ("pause");
return 0;

}

Last edited on
@OP character constants have an apostrophe {'}around them so your switch statement will not work.

Example:
case O: case o: should be case 'O': case 'o':
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.

using namespace std;
int main()
{

int hrsWorked;
double firstName, lastName;
double payRate, taxRate, grossPay, taxAmount, netWeeklyPay, jobStatus;


char fullTime = 'F' or 'f';
char partTime = 'P' or 'p';
jobStatus = fullTime or partTime;

//user inputs
cout << "Please enter your Name:\n " << endl;
cin >> firstName;

cout << "Please enter your Last Name:\n " << endl;
cin >> lastName;

cout << "Are you Fulltime or Parttime:\n" << endl;
cout<<"F. Full Time"<<endl;
cout<<"P. Part Time:\n "<<endl;
cin >> jobStatus;

cout << "Please enter your payRate:\n " << endl;
cin >> payRate;
cout << "Please enter total hours of work:\n " << endl;
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 if(hrsWorked <= 40)
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;
}
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.
1
2
3
//user inputs
//cout << "Enter firstName, lastName, jobStatus (F or P), payRate, hours worked:\n " << endl;
//cin  >> firstName >> lastName >> jobStatus >> payRate >> hrsWorked; 


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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>
#include <string>
#include <sstream>

using namespace std;
int main()
{

int hrsWorked;
string firstName, lastName;
double payRate, taxRate, grossPay, taxAmount, netWeeklyPay;
string  jobStatus;
string  inStr;

string fullTime;
string partTime;


//user inputs
//cout << "Enter firstName, lastName, jobStatus (F or P), payRate, hours worked:\n " << endl;
//cin  >> firstName >> lastName >> jobStatus >> payRate >> hrsWorked;
//user inputs


cout << "\nPlease enter your First Name: ";
getline (cin,firstName);

cout << endl << "\nPlease enter your Last Name: ";
getline (cin,lastName);

cout << endl << "\nAre you Fulltime or Parttime?\n";
cout << "F. Full Time\n";
cout << "P. Part Time:\n";
//cin >> partTime;
getline (cin,inStr);
cout << partTime;

cout << endl << "Please enter your payRate: ";
getline (cin,inStr);
stringstream(inStr) >> payRate;

cout << endl << "\nPlease enter total hours of work: ";
getline (cin,inStr);
stringstream(inStr) >> hrsWorked;
cout << hrsWorked << endl;

grossPay = payRate * hrsWorked;

taxRate = 0.12;

taxAmount = grossPay * taxRate;

netWeeklyPay = grossPay - taxAmount;


if(hrsWorked > 40)
cout << "You will get Over Time Pay" << endl;


else if(hrsWorked <= 40)
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;
}

Last edited on
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <string>

using namespace 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. :)
Last edited on
everyone thanks for your help how can i repay you
Topic archived. No new replies allowed.