cout没有在for循环外执行

时间:2015-05-22 06:27:26

标签: c++ arrays for-loop cout

我正在编写一个程序,输出用户输入和空格分隔的整数之和(不超过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键,它会打印五个数字的总和。

但是我需要它来读取空格分隔的值,而不是换行符分隔值。如何在不使用我尚未学习的材料(向量,指针......)的情况下修改它?

任何提示,提示或评论都将不胜感激!

7 个答案:

答案 0 :(得分:1)

有一个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);
}

请注意表达式noskipws中使用的cin

一些测试用例:

  • 这些案例工作正常:

    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个数字。

答案 1 :(得分:0)

1)您需要将数据转换为字符串
2)根据空间将其分成阵列
3)然后循环和总计或使用std::accumulate

答案 2 :(得分:0)

您可以将所有数字和空格分隔符包含在一个字符串中。 将每个空格更改为&#39; \ 0&#39;创建一系列单独的字符串。 您可以使用atoi将每个字符串转换为整数。

希望我帮忙,

答案 3 :(得分:0)

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

答案 4 :(得分:0)

很多其他人似乎正在处理空白,但我假设你的意思是你在不同的行上输入这些数字。如果您在一行中输入它们,这只能解决您问题的一半。

我看到的主要问题是你只输入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数组归零,具体取决于您使用的内容,因为无法保证所有单元格都已写入。

答案 5 :(得分:0)

当我遵守它时,我在第9行的cout语句中出错,我能够修复它。它编译之后,当我输入数字时,它做得很好。这需要永远,因为您需要100个数字才能进入数组,但当我将其更改为10个数字并输入10个数字时,它可以正常工作。

答案 6 :(得分:0)

我认为你只需要检查读取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()返回流中的下一个字符而不提取它。