Jump to content

Number Generator Project C++

LegendKillerRG

Hey guys, I'm still quite new to programming(Beginner so go easy one me). Started C++ since school started and I'm also learning/taking Haskell course and so far, Haskell isn't my language to code. I just started but who knows, might be my favorite language I like later. So I was looking at the lottery today(Bought 5 each of the big four, hoping to get lucky) and thought that maybe I can create a simple number generator since I'm quite bored.. I'm planning to expand the generator bigger into something like a 'switch case' for MegaMillion/PowerBall/Daily 4/Daily 3/Hot Spot etc.. I'm still learning so offer suggestions or advice where ever you see fit. Here's what I gotten so far.

#include<iostream>
#include<cstdlib>	//Not sure why I added this
#include<ctime>		//Allows use of time()
#include<windows.h> //Allows the use of Sleep()

using namespace std;

void Time(int& userInput, int& userInput2){  //Number Generator
	for(int i=0;i<4;++i){
		if(userInput2>0){
		cout<<((rand()%userInput2)+1)<<" ";
		}
	}
	if(userInput>0){
		cout<<((rand()%userInput)+1)<<endl;
	}
	return;
}

int main(){
	int userInput=0;
	int userInput2=0;
	
	srand(time(0)); //Set algorithm based on time
	
	cout<<"Enter the amount of numbers for the first four number: ";
	cin>>userInput2;
	cout<<"Enter the amount of the last digit you want: ";
	cin>>userInput;
	
	/*if(userInput.empty()){
		cout<<"Enter a number."<<endl;
	}
	if(userInput2.empty()){
		cout<<"Enter a number."<<endl;
	}*/
	
	cout<<"Generating Numbers.."<<endl;
	
	Sleep(2000); //Need that delay to look cool when generating lol
	cout<<"Your numbers are: "<<endl;
	Time(userInput,userInput2); //Goes into void Time
	cout<<"Goodluck!"<<endl;
	
	
	return 0;
}

I'm currently in the process of seeing if there's a way to detect if userInput has an input or not like if userInput == empty. Anyone has any ideas to do that?

Link to comment
Share on other sites

Link to post
Share on other sites

9 minutes ago, M.Yurizaki said:

You can set userInput to value you know cannot be correct, like if you number range is between 0-50, set it to -1.

 

Otherwise you could have a simple state machine and use a switch-case statement.

Not sure how setting userInput to -1 helps with when user enters a space or press enter without entering anything.

 

2 minutes ago, Glenwing said:

if (userinput == "") {

}

 

Not sure if it works like that but it gives me error.

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, LegendKillerRG said:

Not sure how setting userInput to -1 helps with when user enters a space or press enter without entering anything.

I've forgotten to notice you're also forgetting an important step when dealing with any input: always qualify that your inputs are acceptable before continuing.

 

Checking if your inputs are numbers is part of that qualification process.

Link to comment
Share on other sites

Link to post
Share on other sites

9 minutes ago, LegendKillerRG said:

Not sure if it works like that but it gives me error.

Sorry, it's because your user inputs are ints not strings. You can try taking inputs as strings instead; as it is the program will probably crash if the user enters a non-integer as input.

Link to comment
Share on other sites

Link to post
Share on other sites

So after playing around with this a little, I found out that if you enter a non-number with cin >> [int data type], it returns 0. So if you do not need 0, this is fine. But otherwise, you should probably make this a string input and use the std::stoi function. However, it will throw an exception if you feed it a non-number, so you should throw in an exception check: http://www.cplusplus.com/reference/stdexcept/invalid_argument/

 

I'll leave you with these ideas, how you do your program is up to you. :)

Link to comment
Share on other sites

Link to post
Share on other sites

36 minutes ago, M.Yurizaki said:

So after playing around with this a little, I found out that if you enter a non-number with cin >> [int data type], it returns 0. So if you do not need 0, this is fine. But otherwise, you should probably make this a string input and use the std::stoi function. However, it will throw an exception if you feed it a non-number, so you should throw in an exception check: http://www.cplusplus.com/reference/stdexcept/invalid_argument/

 

I'll leave you with these ideas, how you do your program is up to you. :)

I know. I've been trying to find a way to go on about it but I can't seems to find something to stop the input of letters.. I'll probably work on it some more tomorrow, feeling a little tired and it's going to be night lol.

 

[code]
#include<iostream>
#include<cstdlib>	//Not sure why I added this
#include<ctime>		//Allows use of time()
#include<windows.h> //Allows the use of Sleep()

using namespace std;

void Time(int&userInput, int& userInput2){  //Number Generator
	for(int i=0;i<4;++i){
		if(userInput2>0){
		cout<<((rand()%userInput2)+1)<<" ";
		}
	}
	if(userInput>0){
		cout<<((rand()%userInput)+1)<<endl;
	}
	return;
}

void Test1(int&userInput){
	while(userInput<1){
		cout<<"Enter another number for last digit: ";
		cin>>userInput;
	}
	return;
}

void Test2(int&userInput2){
	while(userInput2<1){
		cout<<"Enter another number for first four digit: ";
		cin>>userInput2;
	}
	return;
}

void Test4(int&userInput){
	if(isalpha(userInput)){
		cout<<"Enter a digit number: ";
		cin>>userInput;
	}
	return;
}

void Test3(int&userInput2){
	if(isalpha(userInput2)){
		cout<<"Enter a digit number: ";
		cin>>userInput2;
	}
	return;
}

int main(){
	int userInput=-1;
	int userInput2=-1;
	
	srand(time(0)); //Set algorithm based on time
	
	cout<<"Enter the amount of numbers for the first four digit: ";
	cin>>userInput2;
	Test3(userInput2);
	Test2(userInput2);
	cout<<"Enter the amount of the last digit you want: ";
	cin>>userInput;
	Test4(userInput);
	Test1(userInput);
	
	cout<<"Generating Numbers.."<<endl;
	
	Sleep(2000); //Need that delay to look cool when generating lol
	cout<<"Your numbers are: "<<endl;
	Time(userInput,userInput2); //Goes into void Time
	cout<<"Goodluck!"<<endl;
	
	
	return 0;
}
[/code]

So far, this is how far I've gotten. I'm trying to figure out how to stop letters/words input for test 3/test 4 but isalpha doesn't work that well.. Unless I'm using it wrong lol.. I'm creating a lot of void, I think it's a very useful feature.

Link to comment
Share on other sites

Link to post
Share on other sites

11 minutes ago, LegendKillerRG said:

So far, this is how far I've gotten. I'm trying to figure out how to stop letters/words input for test 3/test 4 but isalpha doesn't work that well.. Unless I'm using it wrong lol.. I'm creating a lot of void, I think it's a very useful feature.

Get the input as a string, then use std::all_of and isdigit to make sure it's all numbers then convert to an int with std::stoi.

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

22 hours ago, LegendKillerRG said:

I'm currently in the process of seeing if there's a way to detect if userInput has an input or not like if userInput == empty. Anyone has any ideas to do that?

"UserInput" or "UserInput2" cannot be empty.

When you try to read integers from std::cin...

cin>>userInput2;
//...
cin>>userInput;

... 2 things can happen. Either a valid integer is extracted from the stream and assigned to the variable, or the read fails and the stream state goes bad, as can be evidenced from this example code:

int x;  //x is uninitialised, contains whatever "number" happened to be in that memory location but is not "empty"
if (std::cin >> x) 
{
    std::cout << "success, x = " << x << "\n";
}
else
{
    std::cout << "failed!\n";
    //Value of x unknown (pre c++11) or...
    //0 (c++11 and above)...
    //but should be ignored anyway since the read failed and the stream state is bad.
}

//Explanation:
//operator >> returns a reference to the stream it was called on, in this case "std::cin".
//A stream can be evaluated into a bool that is false if the stream state is bad.

So:

-Either the user entered something that is not a valid integer and the stream state goes bad, you need to test the stream to detect this, as shown above.

-Or the user entered a valid integer, which is assigned to the variable, all you need to do then is check if the value is in a valid range with normal integer comparison.

 

As a sidenote, you should use C++11's <random> and forever forget about rand().

 

Link to comment
Share on other sites

Link to post
Share on other sites

22 hours ago, M.Yurizaki said:

So after playing around with this a little, I found out that if you enter a non-number with cin >> [int data type], it returns 0. So if you do not need 0, this is fine.

Depends...

Before c++11, the variable was left untouched if the read failed, since c++11 it is set to 0.

Best to simply check the stream state tough as per my previous post as it should be considered unsafe to work with any data gotten from a stream that signals the operation failed.

Link to comment
Share on other sites

Link to post
Share on other sites

24 minutes ago, Unimportant said:

-Or the user entered a valid integer, which is assigned to the variable, all you need to do then is check if the value is in a valid range with normal integer comparison.

Worth noting that if the user enters something like "100g" the stream will still be valid and give you the correct value of 100 but the 'g' will still be left in the stream waiting to break your next cin call.

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

38 minutes ago, fizzlesticks said:

Worth noting that if the user enters something like "100g" the stream will still be valid and give you the correct value of 100 but the 'g' will still be left in the stream waiting to break your next cin call.

Indeed, one could clear the stream before any input operation to get rid of any leftover garbage like the 'g' in your example with:

std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  
//Ignore all char's in std::cin until '\n', which was the last time enter was pressed.
//numeric_limits is used to get the maximum (implementation defined) streamlength.

//And, of course, if the stream state went bad the error status must be reset:
std::cin.clear();

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×