Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/127.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 修改istringstream中的变量数_C++_Istringstream_Sstream - Fatal编程技术网

C++ 修改istringstream中的变量数

C++ 修改istringstream中的变量数,c++,istringstream,sstream,C++,Istringstream,Sstream,我需要根据先前输入确定的可修改数量的变量在一行中获取输入 如果在前面的函数中得到的输入是1,则代码如下 std::string istring; std::getline(std::cin, istring); std::istringstream stream(istring); while (stream >> a) { statement; } 是否有可能为while循环创建一个条件,该条件会根据您的输入而变化?因此,如果输入是示例5,则其行为如下 while (str

我需要根据先前输入确定的可修改数量的变量在一行中获取输入

如果在前面的函数中得到的输入是1,则代码如下

std::string istring;
std::getline(std::cin, istring);
std::istringstream stream(istring);
while (stream >> a)
{
   statement;
}
是否有可能为while循环创建一个条件,该条件会根据您的输入而变化?因此,如果输入是示例5,则其行为如下

while (stream >> a >> a >> a >> a >> a)
{
   statement;
}
我尝试了
而((流>>a)*number)
但这不起作用

特定的解决方案 首先回答您的具体问题:

我需要根据先前输入确定的可修改数量的变量在一行中获取输入

<> > C++中的解决方案是:<代码> RestIsGrime<代码>,<代码>,< <代码>循环>和<代码>向量< /代码>:

#include <iostream>
#include <string>
#include <sstream>
#include <vector>

int main() {

    unsigned int earlierInput = 5;  // this is the earlier input
    std::vector<int> numbers;  // The type of vector also controls what
                               // data type you want the user to give
                               // you. Because the stream will try to
                               // the user input into that type.

    while (numbers.size() != earlierInput) {

        // Here we ask the user for input and convert the input
        // to a stream, like that we can try to convert the user
        // input into the data type we want.
        std::string istring;
        std::cout << "Please type in " << earlierInput << " numbers: ";
        std::getline(std::cin, istring);
        std::istringstream reader(istring);

        // read as many numbers as possible.
        for (int number; reader >> number;) {
            numbers.push_back(number);
        }

        if (numbers.size() != earlierInput) {
            // if not enough numbers empty vector and print
            // error message
            std::cout << "Not the right amount of numbers!";
            std::cout << "Try again!" << std::endl;
            numbers.clear();
        }
    }

    // If we get here the user typed in the right amount of numbers
    std::cout << "You typed the following numbers:" << std::endl;
    for (const int& i : numbers) {
        std::cout << i << std::endl;
    }
}
#包括
#包括
#包括
#包括
int main(){
unsigned int earlierInput=5;//这是较早的输入
std::vector numbers;//向量的类型还控制什么
//您希望用户提供的数据类型
//你,因为溪流会试图
//用户输入到该类型中。
while(number.size()!=earlierInput){
//在这里,我们要求用户输入并转换输入
//我们可以尝试将用户转换为流
//输入我们想要的数据类型。
字符串istring;

std::您是否必须使用
istringstream
?因为您也可以创建一个字符串数组,如:
std::vector istring
,并在for循环中使用
std::getline
。每行中应该有多个输入变量。早些时候,程序会询问它将接收的信息的人数,因此如果该数字是例如3,那么每一行应该看起来像“1 2 3”或“3 4 5”以此类推。行数是任意的,但每行中变量的数量取决于从早期输入中确定的人数。我希望您在该语句中没有实际的
a
5次,而是有不同的变量?事实上,从我所知,程序的行为与呃它是(流>>a>>a>>a)或者(流>>a>>b>>c)…但这并不是问题所在me@SanderDeDyckersequence point参数在这里不适用。只要
a
只是一个变量,操作顺序就定义得很好。记住,
我想你误解了这个问题。你编写的代码取一个数字,然后检查数组大小是否等于该数值mber,但不必相等。唯一的条件是每次输入5个变量。输入一行的次数是任意的。因此,如果有5个人,在绝大多数情况下,数组不会有5个变量,而是5xN。编辑:我写的“唯一的条件是每次输入N个变量的输入”。N应该是5,以避免与我稍后编写的内容相混淆。我编辑了它。我认为更简单的解决方案是,如果代码与您之前发布的代码类似,但在while循环中添加了for循环,将输入添加到所属向量[I]spot,用于为特定的人添加所有输入。我想我知道现在可以做些什么来运行它,但这两种解决方案都很粗糙-仍然不知道如何使用输入流来实现这一点。但是thx很简单,输入流只能在空白处分割输入。所以实际上你可以对此做些什么。看看上面的while循环。它实际上在做你想做的事情。你仍然没有抓住要点。行数不应该等于输入的人数。人数是用来确定每行中输入了多少变量-每人一个。我试着运行代码,它所做的是输入5行w这里,每一行可以包含任意数量的变量,而它应该包含任意数量的输入行,其中每一行应该包含5个变量。
    #include <iostream>
#include <string>
#include <sstream>
#include <vector>

int main() {

    unsigned int amountOfInputs = 2;  // this is the amount of times
                                      // you want the user to type
                                      // something in
    unsigned int earlierInput = 5;  // this is the earlier input


    std::vector<std::vector<int>> allNumbers; // Now we need a vector in
                                // a vector to store the results.
                                // The type of the inner vector also
                                // controls what data type you want the
                                // user to give you.

    while (allNumbers.size() != amountOfInputs) {
        std::vector<int> numbers; // This is the vector as in the example
                                  // above.

        while (numbers.size() != earlierInput) {
            // Here we ask the user for input and convert the input
            // to a stream, like that we can try to convert the user
            // input into the data type we want.
            std::string istring;
            std::cout << "Please type in " << earlierInput << " numbers: ";
            std::getline(std::cin, istring);
            std::istringstream reader(istring);

            // read as many numbers as possible.
            for (int number; reader >> number;) {
                numbers.push_back(number);
            }

            if (numbers.size() != earlierInput) {
                // if not enough numbers empty vector and print
                // error message
                std::cout << "Not the right amount of numbers!";
                std::cout << "Try again!" << std::endl;
                numbers.clear();
            }
        }

        // If we get here the user typed in the right amount of numbers
        // and we can save them and clear the array for using it again.
        allNumbers.push_back(numbers);
        numbers.clear();
    }

    std::cout << "You typed the following numbers:" << std::endl;
    unsigned int round = 1;
    for (auto numbersOfRound : allNumbers) {
        std::cout << "For round " << round++ << ": ";
        for (auto i: numbersOfRound) {
            std::cout << i;
        }
        std::cout << std::endl;
    }
}