Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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++ 使用stringstream和if else语句时出现字符串输入错误_C++_String_Loops_Parsing_Input - Fatal编程技术网

C++ 使用stringstream和if else语句时出现字符串输入错误

C++ 使用stringstream和if else语句时出现字符串输入错误,c++,string,loops,parsing,input,C++,String,Loops,Parsing,Input,我正在尝试编写一个大型代码的一部分,其中用户输入一个由2个数字组成的字符串,并从该字符串中解析每个数字以进行简单的加法(现在只想测试解析) 我使用stringstream进行解析,这似乎是可行的,但使用这段代码来计算多个案例是行不通的。请参阅下面的代码和输出 #include <iostream> #include <sstream> int main() { int t; std::cout << "Enter the number of test

我正在尝试编写一个大型代码的一部分,其中用户输入一个由2个数字组成的字符串,并从该字符串中解析每个数字以进行简单的加法(现在只想测试解析)

我使用stringstream进行解析,这似乎是可行的,但使用这段代码来计算多个案例是行不通的。请参阅下面的代码和输出

#include <iostream>
#include <sstream>


int main()
{
 int t;

 std::cout << "Enter the number of test cases: ";
 std::cin >> t;                                     
 std::cout << std::endl;                                

 if (t > 10 || t < 1)                                   
 {
  std::cout << "Invalid number of test cases!" << std::endl;                
  return 0;
 }
 else                                           
 { 

  for (int x = 0; x < t; x++)                               
  {   
  std::stringstream numbers;
  int num_1;
  int num_2;

  std::cout << "Enter the two numbers (separated by a space): " << std::endl;       
  std::string input;

  getline (std::cin, input);                                

  numbers << input;                                 

  numbers >> num_1 >> num_2;                                

  std::cout << num_1 << std::endl;                          
  std::cout << num_2 << std::endl;                          
  std::cout << num_1 + num_2 << std::endl;                      
  }
}

 return 0;

}

为什么第一个案例不接受输入?

将代码主要更改为:

std::cout << "Enter the number of test cases: ";
std::cin >> t; std::cin.ignore();
std::cout>t;std::cin.ignore();
当您
cin>>t
时,它读取您给它的数字,然后停止。您的下一个输入是一个
getline
,它读取到行的下一个结尾…这是您在测试用例数之后键入的。因此,您在第一次输入时使用了一个空行


cin.ignore()。利用streams explicit
bool操作符
。第二,您的代码过于复杂,为什么您要读入一个数字字符串,而只是将它们解析为数字?
您可以简单地执行此操作:使用任意数量的对象:
cin>>arg1>>arg2>>arg3>…

我已针对以下问题修改了您的代码:

#include <iostream>
#include <sstream>


int main()
{

int t;

std::cout << "Enter the number of test cases:\n"; 

if (std::cin >> t) { // succeeded in getting an int

    if (t > 10 || t < 1) { // failed in getting a valid number of test cases

        std::cout << "Invalid number of test cases!" << std::endl;
        return 1; // return value of 0 is reserved for a successful run, returning zero after an error doesn't make sense

    }
    else { // succeeded in getting a valid number of test cases

        for (int x = 0; x < t; x++)  {
            int num_1;
            int num_2;

            std::cout << "Enter the two numbers (separated by a space): " << std::endl;

            // why use a stringstream here when you can get the same affect by just retrieving
            // two integers from cin

            if (std::cin >> num_1 >> num_2) { // again always check for valid input 
                std::cout << num_1 << std::endl;
                std::cout << num_2 << std::endl;
                std::cout << num_1 + num_2 << std::endl;
            }
            else { // failed in getting two int's 
                //... 
                // handle error 
            }
        }
    }
} 
else { 
    //...
    // handle error 
}

return 0;

}
#包括
#包括
int main()
{
int t;
std::cout>t){//成功获取int
如果(t>10 | | t<1){//未能获得有效数量的测试用例

std::cout是因为您将运算符>>和std::getline()混合在一起。不要这样做。不要使用运算符>>来读取一行文本。只使用std::getline()来读取。是的,这就解决了它。但是我认为
std::cin.ignore(std::numeric_limits::max(),'\n'));
会更好,因为在测试用例数之后可以有空格。如果用户键入两个或多个空格分隔的值,仍然会有问题。谢谢Will。我现在有了更好的理解。我不知道
cin
不接受EOF
#include <iostream>
#include <sstream>


int main()
{

int t;

std::cout << "Enter the number of test cases:\n"; 

if (std::cin >> t) { // succeeded in getting an int

    if (t > 10 || t < 1) { // failed in getting a valid number of test cases

        std::cout << "Invalid number of test cases!" << std::endl;
        return 1; // return value of 0 is reserved for a successful run, returning zero after an error doesn't make sense

    }
    else { // succeeded in getting a valid number of test cases

        for (int x = 0; x < t; x++)  {
            int num_1;
            int num_2;

            std::cout << "Enter the two numbers (separated by a space): " << std::endl;

            // why use a stringstream here when you can get the same affect by just retrieving
            // two integers from cin

            if (std::cin >> num_1 >> num_2) { // again always check for valid input 
                std::cout << num_1 << std::endl;
                std::cout << num_2 << std::endl;
                std::cout << num_1 + num_2 << std::endl;
            }
            else { // failed in getting two int's 
                //... 
                // handle error 
            }
        }
    }
} 
else { 
    //...
    // handle error 
}

return 0;

}