Foxhound5366
Honorary Master
Ok, so I thought I'd try rekindle the fun I had at University taking one year in Computer Science, so played around with learning C++ one evening.
Only one snag: my very first C++ program runs like a charm, but it doesn't work like it should! I decided to create a small program to generate six random lottery numbers ... only problem is I ran it three times in a row now, and each time it returned the exact same set of six numbers in the same order!
Can any real programmer here please explain to me in laymans terms what I'm doing wrong with this code? Even better, if you can show me exactly what to do to fix it, that'd be best.
Please don't laugh (too much) at this, I know it's probably messy as hell, but please bear in mind it was my very first program in years (and previously I had been taught Java at University):
Coded and run with 'Start without debugging' in Microsoft Visual Studio Community 2013 Update 4, on a Windows 8.1 laptop.
The 'interactive' bits I just built in for some fun, while figuring out how to use the commands in the first place.
Update: It's still repeating exactly the same set of numbers
Am I using the wrong kind of random number generator??
Only one snag: my very first C++ program runs like a charm, but it doesn't work like it should! I decided to create a small program to generate six random lottery numbers ... only problem is I ran it three times in a row now, and each time it returned the exact same set of six numbers in the same order!
Can any real programmer here please explain to me in laymans terms what I'm doing wrong with this code? Even better, if you can show me exactly what to do to fix it, that'd be best.
Please don't laugh (too much) at this, I know it's probably messy as hell, but please bear in mind it was my very first program in years (and previously I had been taught Java at University):
Coded and run with 'Start without debugging' in Microsoft Visual Studio Community 2013 Update 4, on a Windows 8.1 laptop.
Code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
//Programmed on 16 February 2015 as my second real C++ programme (after 'Hello World' of course!)
int lottoNums[6];
string userRequest;
cout << "WELCOME TO MY SUPER-HONEST LOTTERY" << endl;
cout << "V1.0 February 2015" << endl;
cout << endl;
string userName;
cout << "Wow, you're looking lucky already! What is your name?" << endl;
getline(cin, userName);
while(userName == "") {
cout << "Come now, don't be so shy, give us a name!" << endl;
getline(cin, userName);
}
cout << endl;
cout << "Why hello there, " << userName << ", here's to the six numbers that will change your life!" << endl;
cout << endl;
cout << "Instructions: Press [ENTER] to draw next ball; type 'exit' to exit." << endl;
cout << endl;
for (int i = 0; i < 6; i++)
{
cout << "And now, for lucky ball number " << i + 1 << "... *drum roll* " << endl;
lottoNums[i] = (rand() % 48) + 1;
cout << "Ball result: " << lottoNums[i] << endl;
if (i == 5) break;
getline(cin, userRequest);
if (userRequest == "exit") break;
}
if (userRequest == "exit") {
cout << endl;
cout << "Sorry to see you go so soon, " << userName << ", better luck next time!" << endl;
}
else {
cout << endl;
cout << "THAT'S IT! " << userName << ", you're all done." << endl;
cout << "Send some to me if you win big!" << endl;
cout << endl;
cout << "Your final lucky numbers are: | ";
for (int winningBalls = 0; winningBalls < 6; winningBalls++) {
cout << lottoNums[winningBalls] << " | ";
}
}
cout << endl;
cout << endl;
cout << "Press [ENTER] one last time to exit." << endl;
cout << endl;
getline(cin, userRequest);
return 0;
}
The 'interactive' bits I just built in for some fun, while figuring out how to use the commands in the first place.
Update: It's still repeating exactly the same set of numbers