C ++中的循环(Cout)

时间:2014-03-13 18:49:30

标签: c++ cout cin

#include <iostream>
#include <string>
#include <cctype>
using namespace std;
char hold;
string name;
char num1;
char num2;

int main() {
    cout << "Hello!\n";
    cout << "Tell me your name?: ";
    cin >> name;
    cout << "Well well well, if it isn't "<< name << "!\n";
    cout << "Enter a NUMBER " << name << ": ";
    cin >> num1;
    while(!isdigit(num1)) {
        cout << "Enter a NUMBER " << name << ": ";
        cin >> num1;    
    }
    cin >> hold;
    system("pause");
    return 0;
}

enter image description here

问题是,这是对cout的过度负责。我该如何解决?

感谢。

3 个答案:

答案 0 :(得分:0)

更好的方法是使用std::stringstream(注意:包含sstream

    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;  
     }

这取代了你的while循环,你在询问了一个数字之后拨打了电话,因为你已经知道该怎么做了。

答案 1 :(得分:0)

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

如果我将num1更改为int,那么我需要检查输入是否有效,如@Dieter Lucking所述。

#include <iostream>
using namespace std;

int main() {
    char num1;
    do {
        cout << "\nEnter a number: ";
        cin >> num1
    } while(!isdigit(num1));
}

答案 2 :(得分:0)

有关staticx解决方案的一些变化,它将通过DieterLücking的echo "" | test行。

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

Get()函数

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:");

完整代码:

Live Demo

#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;
}
相关问题