Associanting strings to values

Hello, I'd like to associate strins to values. I need to do a program that asks the user a grade but he inputs it as string and this string equals a value. And then do a average of all the grades, so I need to associate the string a certain value.
The strings the user can input are N,M,S,B,E. wich equal to 1, 2, 3 ,4,5 respectively.
I tried doing a if ciclem to every single grade, example:
1
2
3
if(grade1="N") grade1=1; 
\\ and so on


but since there alot of grades that wouldn't be right, would it?
So I'm asking if there is any way I can associate those strings to a certain value previously, so when the users inputs such strings the program already knows what they mean?
Thanks in advance.
You should look at the map container: http://www.cplusplus.com/reference/map/map/
Last edited on
I still wasn't able to find out how to do it, i read that article you posted and saw some more videos, but after associating a string to the number i always have to put map["string"] but I don't know what the user previously inputed, so I can't do the average.
What I was trying to suggest is that the map container would be a great way to associate the strings to your numbers. In other words:

1
2
3
4
5
6
7
8
9
10
/* Code to build your map -- do this before getting the user's input:    */

map< string, int >MyMap;

/*    Pseudo code:    */

MyMap.insert( pair< string, int >( ( "n", 1 );

/*    (Likewise for all of the pairs)    */


(NOTE: This part is new):

Now, start a loop and read your input from the user, saving each string into a list:

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

list< int >UserGrades;

/*    Get input from the user:   (I'll leave that up to you) */

string Input = "";

int Grade = 0;

/*    Use your map to lookup the string:    */

map< string, int )::iterator itr;

itr = MyMap.find( Input );

if( MyMap.end() != itr )
    {
    Grade = itr->second;

    /*    Now add this grade to your list:    */

    UserGrades.push_back( Grade );

    }    /* if( MyMap.end() != itr )    */


Finally, look through your list and calculate your grade.

1
2
3
4
5
6
7
map< int, string >::iterator mit;

mit = LORowTypeStrings.find( iType );

if( LORowTypeStrings.end() != mit )
    TypeString = mit->second;
Last edited on
As this question is posted in the beginners section I'm unsure how much you've learnt about the standard containers, algorithms, and the like.

Hello, I'd like to associate strins to values.

OK...

The strings the user can input are N,M,S,B,E. wich equal to 1, 2, 3 ,4,5 respectively.

OK...

Actually, it looks like you want to associate letters (single char strings) to values?

but since there alot of grades that wouldn't be right, would it?

Are there more grades than N,M,S,B,E? If there aren't, 5 is not a lot!

But anyway, as the values of N,M,S,B,E are sequential, a cheap approach (cheaper than std::map) would be to use a std::string and use its find() method to obtain the index of the grade letter (0,1,2,...) which will be one less than the grade value (if the letters are correctly ordered.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <string>
using namespace std;

...

const string grades  = "NMSBE"; // 1,2,3,4,5

...

// you should ensure the string you're searching for is just a single char
size_t pos = grades.find("M");

int grade_value = 0; // not found

if(string::npos != pos)
    grade_value = (1 + pos); // add 1 as indices into string are 0-based

...


Then, if grade_value is 0, you know somethings gone wrong (an invalid grade letter has been entered.) Otherwise you can use the value obtained.

Andy

PS You do know your code fragment was rather dodgy?

1
2
if(grade1="N") grade1=1; 
\\ and so on


- comparison requires == not = (which is assignment)
- grade1 cannot be both a string and an integer!.
Last edited on
@AndyWestKin: You're right: I got carried away. I usually try to stay at a poster's learning level -- I hope I haven't discouraged anyone who read my answers.
Hello again, sorry for bothering you guys again,
I tried your approach @AndyWestKin and I got this
1
2
3
4
5
6
7
8
9
	string str1, str2, str3, str4, str5;
	string::size_type position;
	float media;
	str1 = "NSMBE";
	cout << str1.find("N") +1  << endl;
	cout << str1.find("M") + 1 << endl;
	cout << str1.find("M") + 1 << endl;
	cout << str1.find("B") + 1 << endl;
	cout << str1.find("E") + 1 << endl;

Now this still doesnt mean N equals 1 does it? How can I calculate the average going on from here?~
Really sorry for all my dumb questions..
If you set the result of str1.find("N") + 1 to a variable you can then use the value do to your sums.

Andy
Yes @AndyWestKin, but since I don't know what the user previously inputed I don't get how this is possible. I mean if the user inputs N as grade1 and M as grade2 how will he know the values of those if all I did was set the result of str1.find("N") + 1 to a variable?
I'm really confused, sorry again..
Are you saying you don't know how to do basic arithmetic in C++ ??

Andy
Last edited on
Not that but let's imagine:

I do int n=str1.find("N") +1

But how will I be able to do the average following the user input, I don't know if he inserted an N, an M or what ever.
Topic archived. No new replies allowed.