How To Multiply Any String Variable (e.g "A") With An Integer Variable (e.g "3")

I have a program that prompts users to input a grade scored in a particular course e.g "A" and the equivalent of grade "A" is 5. Now, I don't know how to multiply the "A" inputed by the user with another int variable say "50".

This is what I mean:
1
2
3
4
5
6
7
8
string gradescore;
int creditunit;
float GP;
cout<<"Enter your exam grade "; //A, B, C, D, E, or F.
cin>>gradescore;
cout<<"What is the course credit unit? ";
cin>>creditunit; //Maybe 2, 3, 4, etc.
GP = gradescore * creditunit;


But the compiler is quarrying, saying:
[Error] no match for 'operator*'

Please somebody help!!!!
You can't multiply a string with an integer. Even if Hitler came back from the dead, he couldn't do it.

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
#include <iostream>
using namespace std;

int main()
{
	char gradescore; //better to use a char than a string
	int creditunit;
	float GP;
	
	cout<<"Enter your exam grade "; //A, B, C, D, E, or F.
	cin>>gradescore;
	
	cout<<"What is the course credit unit? ";
	cin>>creditunit; //Maybe 2, 3, 4, etc.
	
	switch (toupper(gradescore)) //converts gradescore to upper (a to A, b to B)
	{
		case 'A': 
			GP = creditunit * 5; break;
		case 'B':
			GP = creditunit * 4; break;
		case 'C':
			GP = creditunit * 3; break;
		case 'D':
			GP = creditunit * 2; break;
		case 'E': 
			GP = creditunit; break;
		case 'F':
			GP = 0; break;
	}
	
	cout << "GP: " << GP;
	
	return 0;
}


It's pretty self explanatory, but ask if you have questions. :)
Last edited on
sasauke wrote:
You can't multiply a string with an integer. Even if Hitler came back from the dead, he couldn't do it.

You can do it in Python, but the result would not be what the OP expect. ;)
Use * opérator overload and cast char to int then recast to char
Thanks #Sasauke, the Wisest; for your reply. Just that Advanced Selection, especially the 'Switch_Case' sometimes make me feel dizzy and sleepy that was why I was thinking of something more smarter.

Thanks #Peter; for your reply.

Thanks #Ericool; for your reply but would appreciate better if you explain further...I'm a learner on the wheel.
Topic archived. No new replies allowed.