C++ 在做算术运算时给字符类型赋值?

C++ 在做算术运算时给字符类型赋值?,c++,C++,例如: char X = 3; char Y = 6; char Z = 9; int scoreAll, score1, score2, score3; cin >> score1 >> score2 >> score3; // user should enter X >> Y >> Z scoreAll = score1 + score2 + score3; cout << scoreAll // output s

例如:

char X = 3;
char Y = 6;
char Z = 9;
int scoreAll, score1, score2, score3;

cin >> score1 >> score2 >> score3; // user should enter X >> Y >> Z

scoreAll = score1 + score2 + score3;

cout << scoreAll // output should be 18
charx=3;
chary=6;
字符Z=9;
int scoreAll、score1、score2、score3;
cin>>得分1>>得分2>>得分3;//用户应输入X>>Y>>Z
scoreAll=score1+score2+score3;
试试看:


您可以使用
std::map
将字符/变量名映射为值。然后,用户可以插入字符X、Y和Z:

std::map<char,int> values;
value['X'] = 3;
value['Y'] = 6;
value['Z'] = 9;
char score1, score2, score3;

//Here it would be advisable to check cin status/success
cin >> score1 >> score2 >> score3;

cout << value[score1] + value[score2] + value[score3] << std::endl;
std::映射值;
值['X']=3;
值['Y']=6;
值['Z']=9;
字符分数1,分数2,分数3;
//此处建议检查cin状态/成功
cin>>得分1>>得分2>>得分3;

cout因为您想要的是将用户输入的数值转换为char,但被读取为char,所以最简单的方法是将其转换为int

int main()
{
   char x, y, z;
   std::cin >> x >> y >> z;
   int result = atoi(x) + atoi(y) + atoi(z);
}

atoi将从数字字母转换为数字,即使它们看起来一样,“1”也不是1。

这是一种可怕的转换方式

诚实地说,你应该使用C++的容器,比如地图

#include <map>
#include <stdio.h>
#include <iostream>
#include <string>

int main()
{
  std::map<std::string, int> map;
  map["X"] = 3;
  map["Y"] = 6;
  map["Z"] = 9;

  std::string res = "";
  std::cin >> res;

  for (std::map<std::string, int>::iterator it = map.begin(); it != map.end(); ++it)
  {
    if (it->first == res)
    std::cout << it->second << std::endl;
  }
}
#包括
#包括
#包括
#包括
int main()
{
地图;
地图[“X”]=3;
map[“Y”]=6;
地图[“Z”]=9;
std::string res=“”;
标准:cin>>res;
对于(std::map::iterator it=map.begin();it!=map.end();++it)
{
if(it->first==res)
标准::cout second>res;
如果(res==打印机(X))

std::您可以使用switch.int getVal(char x){switch(x){case'x':返回3;case'Y':返回6;case'z':返回9;}return-1;//输入无效}没有办法让用户输入一个字符或字符串,然后用它来引用变量的名称,如果这是你的意思的话。你需要自己去做。源代码中的名称和程序中的值是分开的。你的问题很清楚。试着提供一些概念性的例子来说明你的意思。是的,那会有用的。去做吧别忘了检查
cin
的状态/成功。@安东尼奥不必担心反对票,因为你做到了,非常感谢:)这不是他们想要的(尽管问题陈述得很糟糕)。他们想要用户驱动的“变量”它工作+C++标准建议使用STD::String No,它不起作用。在C++ 98和C++ 03中,字符串文本到代码> char */COD>转换为不赞成;因为C++ 11是非法的。此外,C++标准不象你所说的那样“推荐使用STD::String”。
int main()
{
   char x, y, z;
   std::cin >> x >> y >> z;
   int result = atoi(x) + atoi(y) + atoi(z);
}
#include <map>
#include <stdio.h>
#include <iostream>
#include <string>

int main()
{
  std::map<std::string, int> map;
  map["X"] = 3;
  map["Y"] = 6;
  map["Z"] = 9;

  std::string res = "";
  std::cin >> res;

  for (std::map<std::string, int>::iterator it = map.begin(); it != map.end(); ++it)
  {
    if (it->first == res)
    std::cout << it->second << std::endl;
  }
}
#include <string>
#include <iostream>
#include <stdio.h>

#define PRINTER(name) printer(#name, (name))

std::string printer(char *name, int value) {
  std::string res (name);
  return res;
}

int main()
{
  char X = 3;
  char Y = 6;
  char Z = 9;

  std::string res = "";
  std::cin >> res;

  if (res == PRINTER(X))
    std::cout << (int)X << std::endl;
}