epidrollic
epidrollic

Reputation: 53

No counting of tries in a guessing game c++

I am encountering an error with an guessing game that i am trying to make in c++. This is the following structure:

  1. A pseudo generator generated a number between 1-50
  2. The user is going to guess which number is going the pseudo generator generated and if the user is wrong it is going to count the tries the user did.
  3. If the user guessed right on the first time, the user gets 10 points, if it is on the 3rd time. the user gets 5 points. If the user had done up to 10 tries, the user gets 1 point.

The issue lays in upon incrementing, it does not increment. i have created while loops with do statements and including conditions. But i have resaearched on other articles on stack overflow, but i am having a hard time to implement it in my code as i am a beginner and really into c++.

An example article: Program to figure out number between 1 and 100 won't ever guess "100" because of rounding

// Zagros
//Roulette
// Grundläggande c++ programmering

#include <iostream>
#include <cstdlib>
#include <ctime>


using namespace std;

bool error = true;
string namn = "";
int nummer;
int points;
int tries = 0;

int main()
{        
    cout << "Välkommen till gissa tal" << endl;

    cout << "vad är ditt namn?" << endl;
    cin >> namn;

    cout << "Välkommen \t" << namn << endl;

    cout << "Följande instruktioner är:" << endl
         << "(1)Du inskriver ett tal och sedan slumpas ett ovetande tal"
         << " och jämför om ditt tal är korrekt med det tal som slumpas."
         << endl
         << " Om det slumpade tal på första försök, blir rätt. Får du 10 "
         << "poäng, blir talet rätt på andra eller tredje försöker får du"
         << " 5 poäng. Om talet blir rätt under 10 försök så får du 1 poäng"
         << endl;

    cout << "Du har sedan alternativet att vinna kvitt eller dubbelt,,"
         << " vilket programmet generar 5 tal. Du har då ett försök att"
         << " gissa rätt tal av de 5 talen vid rätt gissning får du"
         << " dubbla poäng, vid fel gissning får du de kvarstående poängen du har"
         << endl;

    while (error)
    {
        do
        {                
            cout << "Vänligen skriv in ett nummer" << endl;
            cin >> nummer;

            if (nummer <= 50)
            {
                cout << "Tack för ditt nummer \t"
                     << "Du valde följande nummer"
                     << endl
                     << nummer
                     << endl;
                error = false;                    
            } else if (nummer > 50)
            {                    
                cout << endl
                     << "vänligen skriv in ett korrekt nummer" << endl;
                error = true;
            }
        } while(error);
    }
    // algorithm starting
    srand( (unsigned int)time(NULL));

    cout << "kommer slumpa nu" << endl;

    int slump = (rand() % 50) + 1;

    if (slump != nummer)
    {
        cout << "tyvärr blev det inte rätt" << endl;
        cout << slump << endl;
        tries = 0;
        cout << "number of tries" << tries << endl;
        error  = true;
    } else if(slump == nummer)
    {
        cout << "det blev helt rätt" << endl;
        tries = 0;
        error = false;
    }

    while(true);
    while(error);
}

Upvotes: 0

Views: 326

Answers (1)

Yucel_K
Yucel_K

Reputation: 698

couple of issues.

cin >> namn won't work because namn is a string. unless if you include header file #include <sring> even then cin won't recognise the multiple words in a string. A better approach would be using getline(cin, namn);

you have while loops that don't do anything in the code and will force you in an infinite loop eventually.

else if (slump == nummer) 
  {
    cout << "det blev helt rätt" << endl;
    tries = 0;
    error = false;
  }** while (true);** //what does this do? this will eventually halt the program in infinite loop

and your last while loop again same as above will force you into an infinite loop.

while (error);

overall all these problems can be identified if you used your debugger and search for the error messages you get. I highly suggest you to start doing that. you will get faster results.

Upvotes: 1

Related Questions