开发者

C++ Unwanted infinite while loop

开发者 https://www.devze.com 2022-12-21 05:16 出处:网络
I get an infinite loop when I use the following code in C++ and I don\'t understand why. I suspect the problem is within the input_words() function. Here is the code:

I get an infinite loop when I use the following code in C++ and I don't understand why. I suspect the problem is within the input_words() function. Here is the code:

#include<iostream>
using namespace std;

string input_words(int maxWords) {
    int nWord = 0;
    string words[maxWords];
    string aWord = "";
    while (aWord != "Quit" && nWord < maxWords) {
        cout << "Enter a number ('Quit' to stop): ";
        getline (cin, aWord);
        words[nWord] = aWord;
        nWord++;
    }
    return *words;
}

int num_words (string words[], int maxWords) {
    int numWords = 0;
    for (int i=0; i<maxWords; i++) {
       开发者_如何学JAVA if (words[i] == "Quit") {
            break;
        }
        numWords++;
    }
    return numWords;
}

int main() {

    const int MAX_WORDS = 100;
    string words[MAX_WORDS] = input_words(MAX_WORDS);

    int lenWords = num_words(words, MAX_WORDS);
    cout << "\nThere are " << lenWords << " words:\n";

    for (int i=0; i<MAX_WORDS; i++) {
        if (words[i] == "Quit") {
            break;
        }
        cout << words[i] << "\n";
    }
    return 0;
}

More specifically, I can't exit even when I type 'Quit' when prompted for a word. How could I solve this? I know this is noob code :) I'm just starting on C++


I modified the function in such a way:

string input_words(int maxWords) {
    cout << "started" << endl;
    int nWord = 0;
    string words[maxWords];
    string aWord = "";
    while (aWord != "Quit" && nWord < maxWords) {
        cout << "Enter a number ('Quit' to stop): ";
        getline (cin, aWord);
        words[nWord] = aWord;
        nWord++;
    }
    cout << "finished" << endl;
    return *words;
}

After inputting Quit, it prints "finished", and then "started" again. Your code is calling the function several times.

The problem is that the function returns only one string. so the line

string words[MAX_WORDS] = input_words(MAX_WORDS);

seems to call the function input_words MAX_WORDS times.

A good way would be to switch to vector<string>:

vector<string> input_words(int maxWords) {
    vector<string> words;
    string aWord;
    while (aWord != "Quit" && nWord < maxWords) {
        cout << "Enter a number ('Quit' to stop): ";
        getline (cin, aWord);
        words.push_back(aWord);
    }
    return words;
}

...
vector<string> words = input_words(MAX_WORDS);


I tried the following test program, it works:

{0,506}$> cat testcin.cpp && make testcin && ./testcin.exe
#include <iostream>
using namespace std;

int main()
{
    const int maxWords = 5;
    int nWord = 0;
    string words[maxWords];
    string aWord = "";
    while (aWord != "Quit" && nWord < maxWords) {
        cout << "Enter a number ('Quit' to stop): ";
        getline (cin, aWord);
        words[nWord] = aWord;
        nWord++;
    }    
}

make: `testcin' is up to date.
Enter a number ('Quit' to stop): test
Enter a number ('Quit' to stop): Quit

[Vlad@rabbit] [20:53:53] [~/c++]
{0,507}$> 


The problem, I think is in your main where you return the result of input_words() which is a string to initialize words in main() which is of type string[]. It's definitely this problem.

Rewritten to use vector:

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

vector<string> input_words(int maxWords) {
    int nWord = 0;
    vector<string> words;
    string aWord = "";
    while (aWord != "Quit" && nWord < maxWords) {
        cout << "Enter a number ('Quit' to stop): ";
        getline (cin, aWord);
        words.push_back(aWord);
        nWord++;
    }
    return words;
}

int num_words (vector<string> words) {
    // return words.size();

    int numWords = 0;
    vector<string>::iterator it = words.begin();
    for (; it != words.end(); it++) {
        if (*it == "Quit") {
            break;
        }
        numWords++;
    }
    return numWords;
}

int main() {

    const int MAX_WORDS = 100;
    vector<string> words = input_words(MAX_WORDS);

    int lenWords = num_words(words);
    cout << "\nThere are " << lenWords << " words:\n";

    vector<string>::iterator it = words.begin();
    for (; it != words.end(); it++) {
        if (*it == "Quit") {
            break;
        }
        cout << *it << endl;
    }
    return 0;
}

Forget the following, the C++ getline() strips the '\n' automatically.

Have you checked to see if your getline() words have newlines at the end of them? That is,

"Quit" != "Quit\n".
0

精彩评论

暂无评论...
验证码 换一张
取 消