【问题标题】:Convert char to int in C++在 C++ 中将 char 转换为 int
【发布时间】:2015-10-09 20:08:32
【问题描述】:

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

例如,如果用户输入字母 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;
    }

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

【问题讨论】:

  • 为什么要避免使用switch 语句?这是一个有效且直接的解决方案。
  • 请参阅std::tolowerstd::toupper,因此您只需进行一半的比较。
  • 原因是因为我们必须为这个项目制作流程图,而我在制作流程图时感到困惑,所以我想做我能理解的东西,而不是把半生不熟的想法放在一起。
  • 由于您没有将{ 紧跟在for 语句之后,因此只会执行下一条语句4 次。
  • @ThomasMatthews 感谢您的更正,当我写在这里时,我放错了 {

标签: c++ char int type-conversion


【解决方案1】:

如果您查看 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;

另一种解决方案是使用地图:

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 范围内。

【讨论】:

    【解决方案2】:

    有几件事...

    字符 A, a = 4; 创建字符 VARIABLES(存储位置)并为变量 a 赋值 4。变量代表一个内存位置,一个存储信息的地方。它是此存储位置的人类可读表示。它不是一种翻译机制。 CHARACTER 'a' 是一个用 ascii 码表示的值,可以存储在变量中,十六进制值为 61 或十进制 97。变量 a 与字符值 'a' 不同。并将十进制 4 存储到字符变量中是将其设置为 EOT 字符。

    最好的办法是使用开关。它工作正常。

    最好在使用变量之前对其进行初始化。您的计数器可能会从零开始,但取决于编译器,它们可能包含随机值。在进入循环之前将它们设置为零。

    【讨论】:

      【解决方案3】:

      感谢大家的帮助。这就是我解决问题的方法。前段时间想发的,忘记发了。

      char userInput;
      
      // the for loop is to make sure program will only run 3 times
      for (int i=1; i<4;i++)
      {
       int count2 = 0;
       double count3 = 0.0;
       double gpa;
       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 validation.
            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;
        }
      
        if(userInput !='X' && userInput !='x')
        {
      int grade=func(userInput);
      
      // count2 will add the values of the userInput together
      count2+=grade;
      
      // count3 is a counter that holds the number of times the loop
      // as execute.
      count3++;
        }
      cout << fixed << showpoint << setprecision(2);
      // to get the grade point avarage you need to divide count3 by count2
        gpa = count3/count2;
      
       } while(userInput !='X' && userInput!='x');
      
       // the next few lines will display the information gathered
       cout << endl;
       cout << "Total Grade Points: " << count2 << endl;
       cout << "GPA: " << gpa << endl;
       cout << endl;
       cout << endl;
      
      }
      
       return 0;
      }
      
      
      int func(char userInput)
       {
      // grade is being set to zero so there is a less chance of getting wrong
      // data        
      int grade=0;
      // this will make the userInput into a capital letter
      userInput=toupper(userInput);
      // we are setting value to equal the ascii number of the chosen
      // letter
      int value=userInput; // if input='A' then value = 65
      // by subtracting 69 by the value it will help get us the point value
      // we need.
      grade=69-value;   // gpa=4
      // if the number value of grade becomes negative it will assign grade
      // to store the number 0
      if(grade<0)grade=0;
      return grade;
      }
      

      【讨论】:

        猜你喜欢
        • 2014-02-07
        • 2013-08-10
        • 2018-02-10
        • 2010-09-19
        • 1970-01-01
        • 1970-01-01
        • 2015-12-18
        • 2015-01-29
        相关资源
        最近更新 更多