>chrOne>>chrTwo; 如果(chrOne+chrTwo>maxChr) { cout,c++,char,int,add,C++,Char,Int,Add" /> >chrOne>>chrTwo; 如果(chrOne+chrTwo>maxChr) { cout,c++,char,int,add,C++,Char,Int,Add" />

在c+中添加两个单字符+; 我正在尝试用C++制作自动售货机。我只是想给它添加一些验证,这样它就不会崩溃。我首先要求用户输入他们选择的前两个字母。我知道我不能阻止他们输入超过一个字符。我创建了一个do while循环,以确保第一个字符和第二个字符不大于maxChar。我没有发现语法错误,但我没有得到正确的答案。我知道char和int是不同的,但是我如何将char转换成int呢?任何帮助都将不胜感激 #include <cstdlib> #include <iostream> #include <iomanip> #include <string> #include <sstream> #include <set> #include <cctype> #include <locale> const int maxChr = 3; char chrOne,chrTwo; do { cin >>chrOne>>chrTwo; if(chrOne + chrTwo > maxChr) { cout <<"you have too many characters" "please try again" << endl; } while (chrOne + chrTwo > maxChr); } #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 常量int maxChr=3; char-chrOne,chr2; 做 { cin>>chrOne>>chrTwo; 如果(chrOne+chrTwo>maxChr) { cout

在c+中添加两个单字符+; 我正在尝试用C++制作自动售货机。我只是想给它添加一些验证,这样它就不会崩溃。我首先要求用户输入他们选择的前两个字母。我知道我不能阻止他们输入超过一个字符。我创建了一个do while循环,以确保第一个字符和第二个字符不大于maxChar。我没有发现语法错误,但我没有得到正确的答案。我知道char和int是不同的,但是我如何将char转换成int呢?任何帮助都将不胜感激 #include <cstdlib> #include <iostream> #include <iomanip> #include <string> #include <sstream> #include <set> #include <cctype> #include <locale> const int maxChr = 3; char chrOne,chrTwo; do { cin >>chrOne>>chrTwo; if(chrOne + chrTwo > maxChr) { cout <<"you have too many characters" "please try again" << endl; } while (chrOne + chrTwo > maxChr); } #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 常量int maxChr=3; char-chrOne,chr2; 做 { cin>>chrOne>>chrTwo; 如果(chrOne+chrTwo>maxChr) { cout,c++,char,int,add,C++,Char,Int,Add,行if(chrOne+chrtoo>maxChr)不检查用户是否输入了两个以上的字符,因此据我所知,您的说法是错误的。如果您只需要一个字符,您可以在一个字符串中输入一个字符,并对此进行检查,以查看用户输入了多少字符。您正在使用 cin >>chrOne>>chrTwo; 假设用户输入了两个以上的字符,即荒谬的问题 即使这样,变量上也只会存储前两个字符,即 chrOne='a' chrTwo='b' 请澄清您打算做什么…当循环看起来像: do { } while ()

if(chrOne+chrtoo>maxChr)
不检查用户是否输入了两个以上的字符,因此据我所知,您的说法是错误的。如果您只需要一个字符,您可以在一个字符串中输入一个字符,并对此进行检查,以查看用户输入了多少字符。

您正在使用

cin >>chrOne>>chrTwo;
假设用户输入了两个以上的字符,即荒谬的问题

即使这样,变量上也只会存储前两个字符,即

chrOne='a'
chrTwo='b'

请澄清您打算做什么…

当循环看起来像:

do
{

} while ();
(您的while位于端部支架之前)

如果您只想得到两个字符(假设您只想得到0-9,因为您要求的是有问题的数字):

#包括
int main()
{
charin1,in2;
做{

std::cout好的,我明白你想做什么,但你做得不好……你总是只看前两个字符!


这一行
chrOne+chrTwo
不是您所期望的。 ASCII中的A与65相同,
B
=66,依此类推。事实上,你是在对两个数字求和。65+66=131,大于3



我不知道StackOverflow的格式是否不好,但是
while(…)
应该在
之后
。此代码不应编译。

因此…
'A'+'B'
肯定会大于3。您是否将字符值相加以确定用户输入了多少字符???我很困惑。您的意思是验证输入了多少字符或输入了哪些字符?是否要让用户输入2个值“我没有得到正确的答案”-你得到了什么答案?你认为什么答案应该是正确的?你输入什么?这个问题是TAGGET C++。改变<代码> GETCARE()/<代码> <代码> CIN < /代码> @完成。现在使用<代码> CIN。
#include <iostream>

int main()
{

  char in1,in2;
  do {
      std::cout << "please make a selection"
      cin.get(in1); 
      cin.get(in2);
      in1 -= '0'; //converts a char to the digit that represents it - needs checking though
      in2 -= '0';
      //at this point, you have grabbed both the inputs from the cmdline.
      //you'll need to ensure that these are valid.
  } while (!(in1 >= 0 && in1 <= 9 && in2 >= 0 && in2 <= 9)); //change as needed e.g. if you have a 5*6 machine, reduce the '9's to 5 and 6

  //you now have your input.
}