簡體   English   中英

C ++中的循環(Cout)

[英]Over looping (Cout) in C++

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

在此處輸入圖片說明

問題是,這使偵查團陷入了循環。 我如何解決它?

謝謝。

更好的方法是使用std::stringstream (注意:include 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循環,並且您在詢問了號碼后就進行了呼叫,因為您已經知道該怎么做。

以下是原始嘗試的簡化版本。 但是,與原始字符一樣,它僅檢查單個字符。

如果我將num1更改為int,則需要檢查輸入是否有效,如@Dieter Lucking所述。

#include <iostream>
using namespace std;

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

staticx解決方案的一些變化,它將傳遞DieterLücking的echo "" | test 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:");

完整代碼:

現場演示

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM