My switch statement code isn't working

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
/* QUESTION */
/* Write a function named getPrice
 It takes in one parameter called size which is a character and
 returns the price depending on the size
 When size is 'L' return 20.00
 When size is 'M' return 15.00
 When size is 'S' return 10.00
 For all other chars, return -1
 Use switch case statement
*/

#include <iostream>
using namespace std;

double getPrice(char size){
    cout << "Enter size: ";
    cin >> size;
    
    double price = 1;
    
    switch(size){
        case 'L':
            price = 20.00;
            break;
        case 'M':
            price = 15.00;
            break;
        case 'S':
            price = 10.00;
            break;
        default:
            price = -1;
    }
    return price;
}

int main(){
    cout << getPrice();
}




The error says No matching function for call to 'getPrice'. I don't understand why it isn't running
Last edited on
On line 15, double getPrice(char size), it takes a parameter of char data type; however, when the main function calls it at line 38, cout << getPrice(); you will get an error because those functions are not the same.

getPrice(char size) is not the same as getPrice()
Last edited on
P.S.: maybe if you change
1
2
double getPrice(char size){
    cout << "Enter size: ";

to:
1
2
3
double getPrice() {
	char size{};
	cout << "Enter size: ";


Something like this...
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
/* QUESTION */
/* Write a function named getPrice
 It takes in one parameter called size which is a character and
 returns the price depending on the size
 When size is 'L' return 20.00
 When size is 'M' return 15.00
 When size is 'S' return 10.00
 For all other chars, return -1
 Use switch case statement
*/

#include <iostream>
#include <iomanip> // For the use of std::fixed and setprecision(2)
using namespace std;

double getPrice() {
	char size{};
	cout << "Enter size: ";
	cin >> size;

	double price = 1;

	switch (size) {
	case 'L':
		price = 20.00;
		break;
	case 'M':
		price = 15.00;
		break;
	case 'S':
		price = 10.00;
		break;
	default:
		price = -1;
	}
	return price;
}

int main() {
	cout << fixed << setprecision(2);
	cout << getPrice();
}
Last edited on
Thank you for responding. From my understanding of your explanation, you are saying I need to put:

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
/* QUESTION */
/* Write a function named getPrice
 It takes in one parameter called size which is a character and
 returns the price depending on the size
 When size is 'L' return 20.00
 When size is 'M' return 15.00
 When size is 'S' return 10.00
 For all other chars, return -1
 Use switch case statement
*/

#include <iostream>
using namespace std;

double getPrice(char size){
    cout << "Enter size: ";
    cin >> size;
    
    double price = 1;
    
    switch(size){
        case 'L':
            price = 20.00;
            break;
        case 'M':
            price = 15.00;
            break;
        case 'S':
            price = 10.00;
            break;
        default:
            price = -1;
    }
    return price;
}

int main(){
    char size;
    
    cout << "Enter size: ";
    cin >> size;
    
    cout << getPrice(size);
}



However, the cout statement is running twice. Is there a reason why?
Last edited on
Delete line 16 and 17 on your code, then it should run only once.
1
2
    cout << "Enter size: "; //line 16 Delete
    cin >> size; // line 17 Delete 


The reason is calling it twice is because it calls for the size in the main function and then once the getPrice(size) is called, then it ask the size from the getPrice(size) at line 16 and line 17.
Last edited on
I feel so dumb. I just saw that right now. Thank you so much for all your help
I feel so dumb.
Don't be. All of use makes mistake; anyways, happy coding!
Topic archived. No new replies allowed.