简体   繁体   中英

Why is my code causing problem while running the "while loop" in C++? in the if else satement it keeps printing the wrong output constantly

#include <iostream>
#include <string>
#include <cstring>
using namespace std;



int main()           //geting input part
{
 string wantedWord, name1, name2;
 cout<<"Player one, please enter your name: ";
 cin >> name1;
 cout<<"Player two, please enter your name: ";
 cin >> name2;
 cout << "OK " << name1 << " and "<< name2<<". Let's start the game!/n";
 cout << name1 << ", please input the word you want " << name2 << "to guess: ";
 cin >> wantedWord;

 // find the leght of the word
 int len = wantedWord.size();


 // check whether the string is alphabetical or not
 char w;    // w = the charater on that specific index 
 int n = 0;   // n = index 
 while ( n < len && n >= 0)
 {
    w = wantedWord[n];
    if ((int(w) >= 65 && int(w) <= 90) || ( int(w) >= 97 && int(w) <= 122))
    {
        n += 1;
        cout << w << endl;
    }
    else 
    {
        cout << "Invalid word! Try again." << endl;    //this gives me an infinite loop why?
        cout << name1 << ", please input the word you want " << name2 << "to guess: ";
        cin >> wantedWord;
    }
    
 }

}

My problem is accuring at the "// check whether the string is alphabetical or not" part. When I enter "CS201" as an invalid input, it works fine at first and asks me to enter another word. But then I enter a valid word such as "banana" but again it says "Invalid word. Try again." when it's supposed to accept the input as valid? Why am I getting this error. I'm very new to C++ so if it's not a good question sorry for that.

I have made a few corrections to your code.

  • Added return 0 in the end with a brace.
  • Replaced /n by \n.(Irrelevant in this context but important.)
  • Resetting the value of n to zero as soon as a new input is taken inside the else statement. Since you didn't reset the value of n to zero you were getting error if first attempt was invalid input(as you said in the code).
#include <iostream>
#include <string>
#include <cstring>
using namespace std;

int main() //geting input part
{
    string wantedWord, name1, name2;
    cout << "Player one, please enter your name: ";
    cin >> name1;
    cout << "Player two, please enter your name: ";
    cin >> name2;
    cout << "OK " << name1 << " and " << name2 << ". Let's start the game!\n";
    cout << name1 << ", please input the word you want " << name2 << "to guess: ";
    cin >> wantedWord;

    // find the leght of the word
    int len = wantedWord.size();

    // check whether the string is alphabetical or not
    char w;    // w = the charater on that specific index
    int n = 0; // n = index
    while (n < len && n >= 0)
    {
        w = wantedWord[n];
        if ((int(w) >= 65 && int(w) <= 90) || (int(w) >= 97 && int(w) <= 122))
        {
            n ++;
            cout << w << endl;
        }
        else
        {
            cout << "Invalid word! Try again." << endl; //this gives me an infinite loop why?
            cout << name1 << ", please input the word you want " << name2 << "to guess: ";
            cin >> wantedWord;
            n=0;
        }
    }
    return 0;
}

Hope it was helpful. If you are unable to understand please let me know in comment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM