【发布时间】:2012-01-24 07:40:21
【问题描述】:
这可能是一个简单的问题,仅供我个人参考。
i 要求用户有 2 个输入。
当用户按下回车键或用户输入超过 3 个输入时,C++ 控制台如何知道用户缺少 1 个输入?
感谢您的回答。
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
using namespace std;
int main()
{
char hord;
int x;
char userInput[256] ;
int value;
cout<<"Please enter a ./Even mode number: ";
cin >> hord >> userInput;
if(hord == 'd')
{
value = atoi(userInput);
cout <<"Selected Base 10" << endl;
if (value % 2) /*or *///(x & 1)
printf("%d is an odd number d\n", value);
else
{
printf("%d is an even number d\n", value);
}
}
else if(hord =='h')
{
sscanf_s(userInput, "%x", &x);
if (x % 2)
{/*or *///(x & 1)
printf("%d is an odd number h \n", x);
}
else
{
printf("%d is an even number h\n", x);
}
}
else
{
printf("Incorrect mode given ");
}
cin.get();
_getch();
return 0;
}
【问题讨论】:
-
我不明白你的问题。你能举一个应该被接受/拒绝的输入的例子吗?另外,可以更具体地了解您的问题是什么?您的代码中是否有错误或其他错误?
-
一个小提示:不要混用
cout和printf!他们使用不同的缓冲系统,这些缓冲区将不匹配,因此输出可能看起来很奇怪。坚持一种方法(在 C++ 中推荐使用cout。)
标签: c++ visual-c++ logic