cout没有在for循环之外执行

cout not executing outside a for loop

本文关键字:执行 循环 for cout      更新时间:2023-10-16

我正在编写一个程序,该程序输出用户输入的空格分隔整数(不超过100(的总和。我应该把这些值读入一个数组,这样输入"1 2 3"就会产生"6"。

这就是我目前所拥有的:

#include <iostream>
using namespace std;
int main() {
int i= 0;
int total = 0;
int input[100];
cout << "Please enter a series of integers, space separated, that you would
enter code here`like to calculate the sum of: " << endl;
for(; i < 100; i++)
{
    cin >> input[i];
    total += input[i];
}
cout << "The sum of these values is: " << total << endl;
getchar();
return 0;
}

尽管它是编码的,但它不会打印总数。如果我在for循环的末尾cout,然后编译并输入1 2 3,它将打印1 3 6。这正是我所期望的。

此外,当我将数组大小设置为5并运行它(按原样编码(时,我发现如果在每个值后面按enter键,它确实会打印出五个数字的总和。

但我需要它来读取空格分隔的值,而不是换行分隔的值。我如何在不使用我还没有学会的材料(矢量、指针…(的情况下修改它?

任何提示、提示或批评都将不胜感激!

有一个std::noskipws允许显式获取空白分隔符。为了检查分隔符(可以通过多个分隔符(,我编写了以下函数:

bool wait_for_number(istream& is) {
    char ws;
    do {
        ws = is.get();
        if(!is.good())
            throw std::logic_error("Failed to read from stream!");
        if(ws == 'n')
            return false;
    } while(isspace(ws));
    if(isdigit(ws))
        is.putback(ws);
    else if(ws != 'n')
        throw std::logic_error(string("Invalid separator was used: '") + ws + "'");
    return true;
}

你需要额外的条件:

bool hasnumbers = true;
for(; hasnumbers && i < 100; i++) {
    int number;
    cin >> noskipws >> input[i];
    total += input[i];
    hasnumbers = wait_for_number(cin);
}

注意表达式中使用的noskipwscin

一些测试用例:

  • 这些案例运行良好:

    echo '2' | ./skipws > /dev/null
    echo '1 2' | ./skipws > /dev/null
    echo '1 2 ' | ./skipws > /dev/null
    echo '1 2 3' | ./skipws > /dev/null
    echo '1   3' | ./skipws > /dev/null
    
  • 这种情况会导致"无法从流中读取!":

    echo '' | ./skipws > /dev/null
    echo ' ' | ./skipws > /dev/null
    echo ' 1 2' | ./skipws > /dev/null
    
  • 这种情况导致"使用了无效的分隔符"错误:

    echo '1XXX3' | ./skipws > /dev/null
    

顺便说一句,你可以使用vector<int>,它很容易重新分配,所以你的程序不会局限于100个数字。

我认为您只需要检查读取int后的下一个字符是否是空格。如果没有结束循环:

#include <iostream>
using namespace std;
int main()
{
    int total = 0;
    int input[100];
    cout << "Please enter... blah blah blah... the sum of: " << endl;
    for(int i = 0; i < 100; ++i)
    {
        cin >> input[i];
        total += input[i];
        // space means more to come else exit loop (break)
        if(cin.peek() != ' ')
            break;
    }
    cout << "The sum of these values is: " << total << endl;
    getchar();
    return 0;
}

输出:

Please enter... blah blah blah... the sum of: 
1 2 3
The sum of these values is: 6

函数cin.peek((返回流中的下一个字符,而不提取它。

您可以将所有数字和空格分隔符编码为一个字符串。将每个空格更改为"\0"以创建一系列单独的字符串。可以使用atoi将每个字符串转换为整数。

希望我能帮上忙,

1(您需要将数据放入字符串
2( 根据空间将其拆分为数组
3( 则循环并合计或使用CCD_ 5。

cin不能通过空格将所有值组合在一起,您必须在每个值后面输入。。。如果你想得到所有的数字,你可以把它作为一个字符串,然后把它转换成整数。。。事实上,当你只输入一个由空格分隔的数字字符串时,cin代码还没有完成,它需要99个其他输入,你无法到达cout

很多其他人似乎都在处理空白,但我认为你的意思是在单独的行中输入了这些数字。如果你在一行输入它们,这只会解决一半的问题。

我看到的主要问题是,您只输入了3个数字,但运行for循环100次。如果你输入100个数字,你会得到一个总数。很可能,这不是你想要的行为。我建议一种更好的处理方法:

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
int main()
{
    int total = 0;
    int input[100];
    std::string buffer = "";
    for(int i = 0; i < 100; i++)
    {
        cin >> buffer;
        if(buffer != "e")
        {
            input[i] = atoi(buffer.c_str());
            total += input[i];
        }
        else
        {
            i = 100;
        }
    }
    cout << "The sum of these values is: " << total << endl;
    getchar();
    return 0;
}

我不会说这是理想的,但它如预期的那样有效。您可能还想将int数组归零,这取决于您使用它的目的,因为不能保证所有单元格都已写入。

当我执行它时,我在第9行的cout语句中出现了一个错误,我得以修复。它是在那之后编译的,当我输入数字时,它做得很好。这将需要很长时间,因为您需要100个数字才能输入数组,但当我将其更改为10个数字并输入10个数字时,效果很好。