dimanche 1 septembre 2019

Second string Rand() function is not working

I need to know how to fix this second string based rand() function. The second rand() string function does not work I just changed the variables hoping it would not return true. Is there a simple fix for this issue? Does it have to do with a bug I have no control over I just wanted to assign psw to word2 so that maybe there will be a different result here I have no Idea....

#include <iostream>
#include <string>
#include <time.h>
#include <cstdlib>

using namespace std;
int lives = 3;
int guess;
int guess2;
int answer = 0;
int answer2 = 0;
int i;
int score = 0;
char letter, letter2;
string word = "fallout";
string word2 = "psw";
int main()
{
    srand(time(NULL));

    cout << "Welcome to the guessing game!" << endl;
    cout << "*****************************" << endl;
    system("PAUSE");
    system("cls");

    answer = rand() % 2 + 1;
    lives = 3;
    do {

        cout << "What is a number between 1 and 2? Can you guess it in\n" << endl << lives << endl << "tries?" << endl;
        cin >> guess;

        if (guess == answer)
        {

            cout << "You won!!" << endl;
            score++;


        }
        else if (lives == 0)
        {
            cout << "Your score" << endl << score;

            system("PAUSE");
            return 0;


        }
        else
        {
            cout << "Incorrect try again!" << endl;
            lives--;
            system("PAUSE");
            system("cls");
        }




    } while (guess != answer);

    cout << "You won your score is" << score << endl;
    system("PAUSE");
    system("cls");

    answer = rand() % 3 + 1;
    lives = 3;
    do {

        cout << "What is a number between 1 and 3? Can you guess it in" << endl << lives << "tries?" << endl;
        cin >> guess;


        if (guess == answer)
        {
            cout << "You won!!" << endl;
            score++;


        }
        else if (lives == 0)
        {
            cout << "Your score" << endl << score;

            system("PAUSE");
            return 0;


        }
        else
        {
            cout << "Incorrect try again!" << endl;
            lives--;
            system("Pause");
            system("cls");
        }




    } while (guess != answer);
    cout << "You won your score is" << score << endl;
    system("PAUSE");
    system("cls");


    answer = rand() % 5 + 1;
    lives = 3;
    do {

        cout << "What is a number between 1 and 5? Can you guess it in\n" << endl << lives << "tries?" << endl;
        cin >> guess;


        if (guess == answer)
        {
            cout << "You won!!" << endl;
            score++;


        }
        else if (lives == 0)
        {
            cout << "Your score" << endl << score;

            system("PAUSE");
            return 0;


        }
        else
        {
            cout << "Incorrect try again!" << endl;
            lives--;
            system("cls");
        }




    } while (guess != answer);
    cout << "You won your score is " << score << endl;
    system("PAUSE");
    system("cls");

    answer = rand() % word.length();
    lives = 3;
    do
    {

        cout << "Select the correct letter in the word '" << word << "': ";
        cin >> guess;
        if (guess == letter)
        {


            cout << "You Won!" << endl;
            score++;

        }
        else if (lives == 0)
        {
            cout << "The correct answer is:" << endl;
            cout << word[answer];

        }
        else
        {
            cout << "Incorrect Try Again" <<
                lives--;

        }




    } while (guess != letter);
    cout << "You won your score is " << score << endl;
    system("PAUSE");
    system("cls");

This is where I am stuck if someone could help me here this code runs and it automatically returns true.

    answer2 = rand() % word2.length();
    lives = 3;
    do
    {

        cout << "Select the correct letter in the word '" << word2 << "': ";
        cin >> guess2;
        if (guess2 == letter2)
        {


            cout << "You Won!" << endl;
            score++;

        }
        else if (lives == 0)
        {
            cout << "The correct answer is:" << endl;
            cout << word2[answer2];

        }
        else
        {
            cout << "Incorrect Try Again" <<
                lives--;

        }




    } while (guess2 != letter2);
    cout << "You won your score is " << score << endl;
    system("PAUSE");
    system("CLS");



}

Aucun commentaire:

Enregistrer un commentaire