Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/142.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++中的循环(COUT)_C++_Cout_Cin - Fatal编程技术网

C++中的循环(COUT)

C++中的循环(COUT),c++,cout,cin,C++,Cout,Cin,问题是,这是过度的cout。我怎么修理它 谢谢。更好的方法是使用std::stringstream注意:包括ssstream 这将取代while循环,您在询问号码后拨打电话,因为您已经知道如何拨打。以下是原始尝试的缩短版本。但是,与原件一样,它只检查单个字符 如果我将num1改为int,那么我需要检查输入是否如@Dieter-Lucking所提到的那样有效 int getNumber() { std::string line; int i;

问题是,这是过度的cout。我怎么修理它

谢谢。

更好的方法是使用std::stringstream注意:包括ssstream


这将取代while循环,您在询问号码后拨打电话,因为您已经知道如何拨打。

以下是原始尝试的缩短版本。但是,与原件一样,它只检查单个字符

如果我将num1改为int,那么我需要检查输入是否如@Dieter-Lucking所提到的那样有效

    int getNumber()
    {
        std::string line;
        int i;
        while (std::getline(std::cin, line))
        {
            std::stringstream ss(line);
            if (ss >> i)
            {
               if (ss.eof())
               {
                  break;
               }
            }
            std::cout << "Please re-enter your input as a number" << std::endl;
        }
        return i;  
     }

在staticx的解决方案上有一点变化,它将通过Dieter Lückk的echo |测试线

我使用istringstream并获取输入,直到不再有标准输入或获取有效输入。我把它全部推到一个模板化的Get函数中,该函数可以用于任何类型;您只需要给用户一个提示:

获取函数 完整代码:
为什么使用全局变量?通常的错误是:不检查格式化输入的结果,不维护流状态。注意:你应该能够复制控制台输出并将其集成到问题。CRASMHSTR,我是新的C++,所以我只是掌握了它的窍门。迪特,你能再解释一下吗?谢谢。也好不到哪里去:如果第一个getline失败,它将返回一个未初始化的i。@DieterLücking:它如何在cin上失败?只是好奇。。这对米乔来说并不明显,测试会成功的fail@DieterL尤克:在OP的计划过程中,什么时候会发生这种情况?我认为在循环失败之前,cin会停止。
    int getNumber()
    {
        std::string line;
        int i;
        while (std::getline(std::cin, line))
        {
            std::stringstream ss(line);
            if (ss >> i)
            {
               if (ss.eof())
               {
                  break;
               }
            }
            std::cout << "Please re-enter your input as a number" << std::endl;
        }
        return i;  
     }
#include <iostream>
using namespace std;

int main() {
    char num1;
    do {
        cout << "\nEnter a number: ";
        cin >> num1
    } while(!isdigit(num1));
}
template<typename T>
void Get(T& toSet, std::string prompt) // read from cin
{
    std::string nextIn;
    cout << prompt;
    getline(cin >> std::ws, nextIn);
    istringstream inStream(nextIn);
    while(cin && !(inStream >> toSet))
    {
        inStream.clear();
        cout << "Invalid Input. Try again.\n" << prompt;
        getline(cin >> std::ws, nextIn);
        inStream.str(nextIn);   
    }
    if (!cin)
    {
        cerr << "Failed to get proper input. Exiting";
        exit(1);
    }
}
int myNumber = 0;
Get(myNumber, "Please input a number:");
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

template<typename T>
void Get(T& toSet, std::string prompt) // read from cin
{
    std::string nextIn;
    cout << prompt;
    getline(cin >> std::ws, nextIn);
    istringstream inStream(nextIn);
    while(cin && !(inStream >> toSet))
    {
        inStream.clear();
        cout << "Invalid Input. Try again.\n" << prompt;
        getline(cin >> std::ws, nextIn);
        inStream.str(nextIn);   
    }
    if (!cin)
    {
        cerr << "\nFailed to get proper input. Exiting\n";
        exit(1);
    }
}

int main() 
{
    string name;
    int num1 = -1;
    cout << "\nHello!\n";
    Get(name, "\nTell me your name?:");
    cout << "\nWell well well, if it isn't "<< name << "!\n";
    Get(num1, std::string("\nEnter a NUMBER, ") + name + ": ");
    cout << "\nYou entered number: " << num1 << std::endl;

    return 0;
}