Home > front end >  Why does it print **** repeatedly?
Why does it print **** repeatedly?

Time:10-28

I am a c learner developer and i was building a random program which had to test gk by asking specific questions . So i added a while loop that would print "wrong answer" when the answer would be wrong . BUT it is printing the quote "wrong answer" infinitely whenever the actual answer is wrong . Here is the actual code:

#include <iostream>
int main() {
    int tries;
    std::string hamon;
    std::string spells;
    int you;

    std::cout << " This is the G.K Test.\n ";
    std::cout << " Start?\n ";
    std::cin >> hamon;

    if (hamon == "Yes") {
        std::cout << " Great . The first question is: Who created the Mona "
                     "Lisa painting?\n ";
        std::cin >> spells;

        if (spells == "Leonardo Da Vinci") {
            std::cout << " correct answer ";
        }

        while (spells != "Leonardo Da Vinci") {
            std::cout << "Wrong answer!";
            tries  ;
        }
    }

    std::cin >> you;
}

CodePudding user response:

So i added a while loop that would print "wrong answer" when the answer would be wrong .

This statement of yours is vague as it is not what you want to acheive. Your goal is to test for gk questions and that too random so, firstly if you want to keep repeating this game then you need to use while loop and use tries to define your stopping condition for while loop.

Now, you can write your code for printing your question to the stdout and taking user input through stdin.

It should be like this :

int tries = 3;
while (tries > 0) {
    <your code for question, answer>
    tries--;   
}    

so, this will give the user 3 tries to answer the question.

Also, you can try to have a array of string for your questions for having some randomization in your questions and use a iterator for choosing a random index using rand() and use that value as your question inside your while loop.

But the way you did use your while loop is certainly wrong way as it doesn't have the stopping condition and will keep on running infinitely when your answer is wrong which is what you see but not desirable.

let me know if you need any more help.

CodePudding user response:

Once you get to this:

while (spells != "Leonardo Da Vinci"){
  std::cout << "Wrong answer!";
  tries  ;  
}

if spells is not equal to "Leonardo Da Vinci", the condition is always true, so the while loop never quits.

change while to if is probably what you want. The loop you want needs to be around the whole function, not just that part.

CodePudding user response:

It seems like you have intended to use the following block of code to give you the output "Wrong answer!" when spells is not Leonardo Da Vinci.

while (spells != "Leonardo Da Vinci")
{
  std::cout << "Wrong answer!";
  tries  ;  
}

The problem here is that the condition comes out to be true always and as a result, the loop never quits.

You can try something like this

#include <iostream>
using namespace std;
int main()
{
int tries = 0;
string hamon, spells;

cout << " This is the G.K Test.\n ";
cout << " Start?\n ";
cin >> hamon;

if (hamon == "Yes")
{
    cout << " Great . The first question is: Who created the Mona "
                 "Lisa painting?\n ";
    cin >> spells;

    while (spells != "Leonardo Da Vinci")
    {
        if (spells == "Leonardo Da Vinci")
        {
            cout << " correct answer ";
            break;
        }
        else
        {
            tries  ;
            if(tries >= 3)
            {
                cout << "Wrong answer! You ran out of tries" << endl;
                break;
            }
            else
            {
                cout << "Wrong answer! Try again." << endl;
            }
        }
        cin >> spells;
    }
  }
}

In this we begin with using the most basic namespace, std and then we go on to declare the integer variable tries and the string variables hamon and spells.

Now when the user types "Yes", on running the program, the program starts asking as the G.K question as in the original program you wrote.

In this block of the code that I provided ,

 while (spells != "Leonardo Da Vinci")
{
    if (spells == "Leonardo Da Vinci")
    {
        cout << " correct answer ";
        break;
    }

I have made sure that when the user enters the answer correctly, it prints correct answer and the successfully terminates the program using break;

This is the step that you missed in the original program

 else
    {
        tries  ;
        if(tries >= 3)
        {
            cout << "Wrong answer! You ran out of tries" << endl;
            break;
        }

Now when the user enter an incorrect answer, we are giving him 3 tries to think of the right answer and are also terminating the loop at this point with the usage of break;

The original program in the question failed to terminate the while loop when the user enters an incorrect answer.

Hope this answer helps.

CodePudding user response:

Here you go:

#include <iostream>

using namespace std;

int main()
{
    string start;
    char spells[100] = { 0 };

    cout << " This is the G.K Test." << endl;
    cout << " Start?" << endl;
    cin >> start;
    
    int tries = 0;
    if (start == "Yes")
    {
        cout << " Great. The first question is: Who created the Mona Lisa painting?" << endl;
        cin.ignore();
        cin.getline(spells, 100);
        cout << spells << endl;
        if (strcmp(spells,"Leonardo Da Vinci") == 0)
        {
            cout << " correct answer ";
            return 0;
        }
       
        while (strcmp(spells, "Leonardo Da Vinci") != 0)
        {
            tries  ;
            if (tries >= 3)
            {
                cout << "Wrong answer! You ran out of tries" << endl;
                return 0;
            }
            else
            {
                cout << "Wrong answer! Try again." << endl;
            }
            cin.getline(spells, 100);
        }

        cout << " correct answer ";
    }
}

As others have mentioned, you are incrementing your tries variable but do not have a way to escape your while loop. You also have to account for the fact that cin will read input until it reaches a new line or spaces, which in the case of "Leonardo Da Vinci" we have spaces. So it can cause a problem when setting cin >> spell as spell can be set to only "Leonardo". Using cin.getline allows us to get the complete name including spaces.

  •  Tags:  
  • c
  • Related