c++ - 在 C++ 中将 char 转换为 int

标签 c++ char int type-conversion

我正在编写一个 GPA 计算器程序,我想将一个字符的值更改为不同的数字。

例如,如果用户输入字母 aA,则该值将为 4。这就是我的程序的样子。如果我使用开关盒,我知道如何让它工作,但我想这样做。

      char userInput;
      char A, a = 4;  // i want to change the value of A, a to 4
      char B, b = 3;  // i want to change the value of B, b to 3
      char C, c = 2;  // i want to change the value of C, c to 2
      char D, d = 1;  // i want to change the value of D, d to 1
      char F, f = 0;  // i want to change the value of F, f to 0
      int count2 = 0;
      int count3 = 0;
      double gpa;

// the for loop is to make sure program will only run 3 times
for (int i=1; i<4;i++)
{
  cout << "Test #" << i << ":" << endl;
  cout << endl;

  // the do while loop is being used to ensure that the user gets to
  // input at least once.
  do
  {
   cout << "Enter a Letter Grade (enter 'X' to exit): ";
   cin >> userInput;
       // the while loop is only being used for input valiation.
     while (userInput!='A' && userInput!='a' && userInput!='B' &&
            userInput!='b' && userInput!='C' && userInput!='c' &&
            userInput!='D' && userInput!='d' && userInput!='F' &&
            userInput!='f' && userInput !='X' && userInput !='x')
          {
           cout << "\n  Invalid letter grade, please try again.\n";
           cout << "\n  Enter Letter Grade (enter 'X' to exit):";
           cin >> userInput;
          }
    //line number 80 will add the values of the userInput together.     
    count2+=userInput;

// line 83 is a counter that holds the number of times the loop
// as excuted 
    count3++;
      // line 88 will get a grade point average by dividing count3
// by count2
   cout << fixed << showpoint << setprecision(2);
   gpa = count2/count3;


      } while(userInput !='X' && userInput!='x');


     cout << "Total Grade Point: " << count2 << endl;
     cout << "GPA: " << gpa << endl;
    }

如果我的问题太模糊,请告诉我,以便我澄清。

最佳答案

如果您查看 ASCII 表,您会发现字母只是数字。

http://www.asciitable.com/

您可以使用简单的减法计算偏移量:

'a' - 'a' == 0
'b' - 'a' == 1
'c' - 'a' == 2

等等。要将其转换为 GPA 成绩,您可以进行简单的转换:

int deltaA = (int)('a' - 'a'); // explicit cast to int is not really needed
int max = 4;
int grade = max - deltaA;

替代解决方案是使用 map :

std::map<char, int> grades;
grades['a'] = 4;
grades['b'] = 3;
grades['c'] = 2;
...
int score = grades['a']; // score == 4

最好坚持使用大写或小写字母。您可以使用 int std::tolower(int ch)int std::toupper(int ch) 函数转换它们。将 char 放入 int 是可以的 - 两者都是整数并且 int 具有更宽的范围并且 char 将适合。

反过来 - 没那么容易。 int 的范围比 char 大,您应该在转换回之前检查您的 int 值是否在 char 范围内。

关于c++ - 在 C++ 中将 char 转换为 int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33046348/

相关文章:

c++ - 数组类型的元素类型不完整,无法进行模式匹配

c - 我在这里做错了什么?数组决定不保留其值

c - JNI 将 Char* 二维数组传递给 JAVA 代码

c - 仅使用 getchar() 从输入中获取整数并转换为 int

Java递归-从数组中逆序整数,它是如何工作的?

c - scanf ("%d"中的错误格式说明符,unsigned short int) 在 gets(pointer) 更改 char 指针的值之后,但为什么呢?

c++ - 如何在 MFC 中调试不断失去焦点的窗口(例如对话框)?

c++ - Visual Studio 2010 中好的 C++ 主题的集合?

C程序: Convert string of chars representing numbers to ints

c++ - C++ 中的浮点运算问题是否有解决方案?