C ++ - cin.get()要求用户按两次输入

时间:2013-11-28 19:19:26

标签: c++ if-statement get

在我的代码中,我添加了if语句,如果用户意外按下“回车”键,则会捕获空字符串。如果没有这段代码,程序将崩溃,下面是屏幕截图的链接:

http://gyazo.com/59cfe26c76043022b807d533f932838d

由于我添加了代码,如果用户按下“回车”键并且不会再崩溃,程序运行正常 - 但是,代码现在使用户必须在程序向前移动之前按两次“enter”键

以下是整个代码,cin.get()位于第30行,我将使用/*对其进行评论,以便您更轻松。

#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <string>       // std::string
#include <locale>       // std::locale, std::isalpha
#include <iostream>     // std::cout
using namespace std;

int main(int nNumberofArgs, char* pszArgs[])
{
    std::locale loc;        // This enables isalpha to be used
    std::string original;
    std::string new_word;
    bool alphaString = true; // A boolean data type variable assigned to true 
    std::string pyg = "ay";
    std::cout << "(c) Copyright 2013 by Neeraj Morar. All Rights Reserved.\n";
    std::cout << "\n"; 
    std::cout << "Welcome to the English to PigLatin translator!\n";
    std::cout << "You can exit the program at any time by closing the window.\n";
    std::cout << "\n";
    while (true) {
        std::cout << "Type a word you wish to translate:\n";
        std::getline(std::cin, original); // This allows the computer to capture the whole of the string input
        /*if (cin.get() == '\n')
        {
            std::cout << "Please enter a word. (Remember to type in only ONE word!)\n";
            std::cout << "\n" << std::endl;
            alphaString = false;
            continue;
        }*/
        for (std::string::iterator it=original.begin(); it!=original.end(); ++it) // For loop testing string input (only used for the isalpha function)
        {
            if (! std::isalpha(*it,loc) && original.length() > 0) // If statement to test if string input is alpha characters is false
            {
                std::cout << "Please enter a valid word. (Remember to type in only ONE word!)";
                std::cout << "\n" << std::endl;
                alphaString = false; // Boolean variable set to false for if statement and outputs above string if non-alpha characters are found
                break;
            }
        }
        if (alphaString) // Another if statement if string validates as true
        {
            std::transform(original.begin(), original.end(), original.begin(), ::tolower); // Converts string to lower case
            std::string first;
            first = original[0]; // [0] selects the first character of a string in the variable 'original'
            if (first == "a" || first == "e" || first == "i" || first == "o" || first == "u") // This if statement queries whether the first letter of a string consists of a vowel
            {
                new_word = original + pyg; // This variable is a combination of the users' input and a variable called 'pyg' which consists of the string "ay"
                std::cout << "\n";
                std::cout << "Translation: " << new_word << "\n";
                std::cout << "\n";
            }
            else // This else statement is for strings that do not have a vowel as their first letter
            {
                new_word = original.substr(1) + original[0] + pyg; // This variable is a combination of the original word starting from the 2nd character, the first letter of the string added on and then the var "pyg" added on at the end
                std::cout << "\n";
                std::cout << "Translation: " << new_word << "\n";
                std::cout << "\n";
            }
        }
    }

    system("PAUSE");
    return 0;
}

1 个答案:

答案 0 :(得分:2)

程序因此而崩溃

first = original[0];

显然,如果original是一个空字符串,那么这是一个超出界限的访问。

如果您担心空字符串,为什么不测试呢?

for (;;)
{
    std::getline(std::cin, original); // This allows the computer to capture the whole of the string input
    if (!original.empty()) // check for empty string
         break;            // quit loop if not empty
    std::cout << "Please enter a word. (Remember to type in only ONE word!)\n";
    std::cout << "\n" << std::endl;
}