[error C2440] cannot convert from 'std::string' to 'char'

Hi guys, here my problem

user will enter the Letter of Grade ( A B C D and F ) , my small program will read if it valid input or not, count the times appearance of Letter and then output the Average Grade by Point ( A=4, B=3 ... etc).

i tried to control the 'wrong input' like number, case sensitive, not the valid letter ...

but something wrong in the BOLD LINE. My ideal is if the input is More than 1 character then, ask to input again, esle the input is One character, then assign it into the char Array.

hope understand, any help is very much appreciate //sr for my poor English

else array[i]=s; <<< here, line 25

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
#include <iostream>
#include <string>

using namespace std;

int main()
{
    int n, ta, tb, tc, td, tf;
    ta=tb=tc=td=tf=0;

    char array[100];
	string s;

    cout << "How many subjects total ? ";
    cin >> n;

    for(int i=1; i<=n; i++)
    {
        do
        {
            cout << "Enter subject" << i << "'s grade in LETTER : ";
            cin >> s;
			if(s.length()>1)
				break;
			else array[i]=s;
			if(isalpha(array[i]))
            {
                if(tolower(array[i])=='a'){ ta++; break; }

                if(tolower(array[i])=='b'){ tb++; break; }

                if(tolower(array[i])=='c'){ tc++; break; }

                if(tolower(array[i])=='d'){ td++; break; }

                if(tolower(array[i])=='f') break;
            }
        } while(1);
    }
    cout << "Average grade of " << n << " subjects is : " << (ta*4+tb*3+tc*2+td)*1.0/(ta+tb+tc+td+tf);
}
Last edited on
else array[i] = s[0];
If you want to get the first character of the string you would have to use the subscript operator (just like an array). For example:

char something = s[2]; //get the 3rd character of s
http://www.cplusplus.com/forum/beginner/18454/#msg94246

http://www.cplusplus.com/forum/beginner/18454/#msg94247

thanks PanGalactic and firedraco alot, my problem is solved :love u: :)

ah, by the way, any suggest to improve my solution ( use the function ) because i'm very new, so i dunt know how to put it in separate function

one a gain, thanks u guys a lot
Topic archived. No new replies allowed.