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!.