Hi everyone, I just started experimenting with C++ a few weeks ago due to a class I'm enrolled in for school. Needless to say I'm very new to programming and have had a few moments where I resisted the urge to launch my computer across the room. I'm asking everyone to bear with me in my help requests and to let me know if I'm being extra stupid. Hears my problem:
Im writing a program where I need the computer to generate 3 random numbers (between 0 and 9)and then prompt the user to enter there guess at what those numbers are. Everything has been smooth sailing for me so far but the only problem i have is when I run the program the computer generates -858993460 for all 3 numbers every time. Im sure im doing something dumb but if someone can take a look at my code and point me in the right direction that would be great.
Ill post just my driver.cpp file because thats where the problem is but if you require the other two files in the whole program just let me know.
Driver.cpp:
#include "Cash 3.h"
int Cash3::GetFirstNumber()
{
return firstNumber_; //returns the firstNumber
}
int Cash3::GetSecondNumber()
{
return secondNumber_; //returns the secondNumber
}
int Cash3::GetThirdNumber()
{
return thirdNumber_; //returns the thirdNumber
}
Normally you would call srand() only once and it is common to use the time() function as a seed because this is unpredictable and different every time the program is run:
1 2 3 4 5 6 7 8 9 10 11
#include <cstdlib>
#include <ctime>
int main()
{
std::srand(std::time(0));
int num = std::rand() % 10;
return 0;
}
I don't know why your random numbers are not in the required range, I can only assume that the values are not being set before they are being read of something is changing them.
Oh and we all end up throwing our computers at the wall from time to time... just make sure you work in a padded environment hehe ;o)
Hmm, thanks dude I did what you said but still the same problem. You said that possibly the variables are not being set to the range I need but I'm not sure where I would do that. Here are my the other two files to my program. Tell me if you see any errors in these cause I'm startin to get that urge to throw some stuff again. (Still workin on gettin the walls padded)
Cash3.cpp:
#include <iostream>
#include "Cash 3.h"
using namespace std;
int num1;
int num2;
int num3;
char decision;
char Y;
char y;
int main()
{
cout << "Welcome to Cash 3 Lottery!!" << endl;
cout << "Type Y If You Would Like to Play!" << endl; //Used to set the decision to Y
cin >> decision;
do
{
cout << "Please Your Enter 3 Numbers" << endl;
cin.get();
cin >> num1 >> num2 >> num3;
cout << "Your numbers" << num1 << num2 << num3 << endl;
cout << "The winning numbers are ";
if ( num1 == MyObject.GetFirstNumber())
{
if ( num2 == MyObject.GetSecondNumber() )
{
if (num3 == MyObject.GetThirdNumber() )
cout << "Congrats you won !!! Would you like to play again?\n";
}
}
else
cout << "You Loose HA HA HA!. Would you like to play again?\n"; //If any of the three are false
//These two lines (along with the #endif) prevent
//multiple inclusion of this file in the compilation process.
#ifndef CASH3_H
#define CASH3_H
#include <cstdlib> //size_t.
//The Cash3 class declaration.
class Cash3
{
public:
int GetFirstNumber(); //Returns the first lottery number.
int GetSecondNumber(); //Returns the second lottery number.
int GetThirdNumber(); //Returns the third lottery number.
void Draw(); //Randomly draws three lottery numbers from 0 to 9,
//storing each number in the corresponding variable below.
private:
size_t firstNumber_; //The first lottery number.
size_t secondNumber_; //The second lottery number.
size_t thirdNumber_; //The third lottery number.
};
#endif
The .h files was provided by my instructor not me. Let you know what you think.
Wait am I being dumb, you both obviously saw the problem but didn't I call the Cash3::Draw() right above my srand function when I wrote "void Cash3::Draw()" ?