C++循环 - 输入整数,直到用户退出
C++ Loops - inputting integers until user quits
好吧,我试图让用户尽可能多地输入整数,直到他们输入负数。 第一步是使用 ATOF 函数将字符串转换为数字(我做到了(,然后允许用户输入整数(我只设法做一次只是为了看看我是否可以正确使用 atof 函数。
任何帮助/提示都非常感谢给我正确的方向。
这是我到目前为止的代码:
#include <iostream>
#include <string>
int main() {
using namespace std;
char buffer[256];
char tempBuff[256] = {'n'};
double result;
int count = 0;
cout << "Testing " << endl;
cout << "Enter Any integers: ";
cin.getline(buffer,256);
for(int i = 0; i < strlen(buffer); i++)
{
if(isdigit(buffer[i]))
{
tempBuff[count] = buffer[i];
count++;
}
}
if (atof(tempBuff) > 0) {
result = atof(tempBuff) / 2;
}
cout << endl << "The integer you put was: " << tempBuff
<< " And dividing the integers "<< result << endl;
cin.ignore();
return 0;
}
atof
如何知道tempBuff
包含多少个有效数字?atof
函数仅接受 C 样式字符串作为其输入。否则,它无法知道有多少个字符是有效的。
您可以在调用atof
之前使用tempBuff[count] = 0;
。C 样式字符串以零字节结尾。
相关文章:
- 如何实现否定用户输入退出程序和打印列表?
- 如何在用户退出之前重复整个代码?
- 甲骨文金门用户退出 - EXIT_CALL_PROCESS_RECORD未通过
- C++中的奇怪异常代码,当用户输入为 23 时,退出值 1,073,740,940(代码:0xc0000374)
- C++:如果用户输入数字将其转换为单词,并且如果按回车键程序应退出,则使用开关大小写
- 如何在C++中"q"用户输入时退出 do-while 循环
- 我想让程序保持运行,直到用户输入 n 退出程序
- 我找不到第一个用户输入后立即退出程序的原因
- 如何在用户提示下在开关大小写实现中退出
- 仅当用户按返回键时如何退出
- 如果用户在使用返回 0 时输入 -1,程序不会退出;
- C++如何继续主,直到用户选择退出选择
- C++循环 - 输入整数,直到用户退出
- 如何获取用户输入以退出循环
- 如果用户输入q,如何退出
- 请求用户输入以便重新运行程序或退出
- 保持运行程序,直到用户输入退出
- Win32 c++ openprocess应该返回null,如果用户已经退出应用程序,但没有
- 如何跟踪电脑赢了,玩家赢了,直到用户退出
- 日志文件不可见,除非用户退出应用程序