我正在编写一个代码,该代码从用户那里为公司中的五个项目输入数量
I'm writing a code that takes a quantity input from the user for five items in a company
我的问题是当用户输入其中一个项目数量的十进制值时,程序会自动结束。另一方面,我需要它做的是显示一条消息,让用户知道他们犯了错误,并让他们选择重试。
我尝试使用cin.fail()函数没有任何运气。
cout<< "nEnter number of Whizbangs: ";
cin>> WhizbangsAmt;
//This screens out all the negative numbers
while(true)
{
if(WhizbangsAmt < 0)
{
cout<< "Number is negative!n";
cout<< "Enter number of Whizbangs: ";
cin>> WhizbangsAmt;
}
else
{
break;
}
}
您发布的代码难以理解。请考虑使用函数读取整数,而不是使用原始std::cin
函数。通过使用unsigned int
,您可以确定整数的第一个位,因此您可以选择代数符号。
unsigned int readInt(std::istream& stream) {
/*
* Function to get integers, that are entered by the user
*/
unsigned int input;
stream >> input;
return input;
}
相关文章:
- 创建LinkedList退出,返回代码为-11(SIGSEGV)
- Netbeans 10:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)
- 链接器命令失败,macOS 上的退出代码为 1(使用 -v 查看调用)
- 你好世界在 APUE 第 7 章退出,代码为 0
- VSCode C++终端进程已终止,退出代码为:1
- CMake 错误:链接器命令失败,退出代码为 1 和 cpp.o 文件
- clang:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用) - 体系结构的未定义符号 x86_64:
- 如何修复 clang: 错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)
- CLion、MinGW 和 SDL2:进程已完成,退出代码为 -1073741515 (0xC0000135)
- Xcode:链接器命令失败,退出代码为 1(使用 -v 查看调用)[C++]
- Android NDK.Build命令失败.未定义的引用.clang++:错误:链接器命令失败,退出代码为1
- 如何从支持linux/mac操作系统的第三方代码为我的c#项目创建dll
- 终端进程终止,退出代码为:1有什么方法可以修复它吗
- 运行自定义可执行文件,QProcess 立即退出,退出代码为 1
- 错误:链接器命令失败,退出代码为 1(使用 -v 查看调用):在 Macbook 上
- C++ XCODE ld:找不到体系结构x86_64 clang 的符号:错误:链接器命令失败,退出代码为 1(使用 -
- 为什么我的代码为同一代码生成不同的值
- clang:错误:链接器命令失败,C++代码中的退出代码为 1(使用 -v 查看调用)
- OpenGL 应用程序退出,退出代码为 -1073741515 (0xC0000135)
- 我正在编写一个代码,该代码从用户那里为公司中的五个项目输入数量