mercredi 23 janvier 2019

How can I set my variable in C++ to 0 and prevent it from getting random?

I am a beginner in programming. I was doing some simple applications. I was already done with my programme when I realised that my variable is getting random, even if I set it to 0. I was trying to figure it out why. My goal is to add 1 to the "cor" variable when the answer is matching the random generated number, the "else" section is working as it has to be. Maybe someone more experienced can help me.

`

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

using namespace std;

int number;
int ynumber[5];
int cor = 0;
int falsed = 0;

int main()
{
    cout << "Welcome to our lottery!" << endl; 

    cout << "We start in ..." << endl;
    Sleep(2000);


    for (int i = 3; i >= 0; i--)
    {
        system("cls");
        cout << i << endl;
        Sleep(1000);
    }

    system("cls");
    srand(time(NULL));

    for (int i = 0; i < 6; i++)
    {
        cout << "Type your " << i+1 << " number below" << endl;
        cin >> ynumber[i];

        number = rand() % 50 + 1;
        cout << "The picked number is: " << number << endl;
        Sleep(1000);

        if (ynumber[i] == number)
        {
            cout << "Same same!" << endl;
            cor = cor + 1;
        }
        else
        {
            cout << "Hope for better luck next time ;)" << endl;
            falsed = falsed + 1; 
        }
    }

    system("cls");

    cout << "Thank you for participating!" << endl << "Correct picked numbers: " << cor << endl << "Wrong picked numbers: " << falsed << endl << endl;

    system("pause");

    return 0;
}

`




Aucun commentaire:

Enregistrer un commentaire