在C++中将char转换为int
Convert char to int in C++
我正在编写一个GPA计算器程序,我想将char的值更改为不同的数字。
例如,如果用户输入字母a
或A
,则值将为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;
另一种解决方案是使用地图:
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
范围内。
有几件事。。。
char A,A=4;创建字符VARIABLES(存储位置(,并为变量a指定值4。变量表示一个内存位置,一个存储信息的地方。它是该存储位置的人类可读表示。它不是一种翻译机制。CHARACTER"a"是一个由ascii码表示的值,可以存储在变量中,十六进制值为61或十进制97。变量a与字符值"a"不是一回事。并且将十进制4存储到字符变量中是将其设置为EOT字符。
你最好使用开关。它运行良好。
最佳做法是在使用变量之前对其进行初始化。计数器可能一开始为零,但根据编译器的不同,它们可能包含随机值。在进入循环之前将它们设置为零。
感谢大家的帮助。这就是我解决问题的方法。我刚才还想把它贴出来,但忘了。
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;
}
- 是否可以从int转换为enum类类型
- 如何在C++中将一个无符号的 int 转换为两个无符号的短裤?
- 是什么导致了这种使用三进制而不是短整型的有符号int到无符号int转换
- 将字符串中的 int 转换为字母;
- 主.cpp:18:20:错误:从"int*"转换为"int"会失去精度 [-fa
- 将非常大的 int 转换为双倍,在某些计算机上会损失精度
- 如何将矢量<int>转换为字符数组?
- C++ 无法从 const int* 转换为 const_iterator
- 我不断收到错误 C2440'=':无法从"int *(__cdecl *)(int *,int *,int,int)"转换为"int *
- 如何将 int[4] 转换为 std::array<int,4>?
- 如何将data[i].int转换为vaible
- 将 int 转换为字符串,然后连接另一个变量以创建完整扩展名,然后将其转换为 const_char*
- 将私有矩阵形式 int 转换为双 C++
- 无法将参数 1 从 'int' 转换为 'int &'
- 将 int 转换为字符串后始终得到 0
- isspace 函数的性能警告,从 int 转换为布尔值
- 将大 int 转换为浮点数,而不舍入 c++
- 错误:无法将'uint8* {aka unsigned int*}'转换为"常量emxArray_uint8_T*"?
- 将未签名的INT转换为BCD
- 在 C++ 中将 int 转换为双精度