Trying to generate series of 10 random numbers between 25-75 for like lottery ticket. allowing the computer to pick random numbers every time...
I know i'm missing something like a for loop even though i have the do-while loop set up perfectly fine. its just the expression and condition that i am having trouble with.
#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>
usingnamespace std;
int main()
{
constint upBoundary=75;
constint lowBoundary=25;
constint numSeries=10;
int num, randNum;
string runAgain;
srand(static_cast<unsignedint>(time(0))); // seed random number generator
cout << "Welcome to Guess My Number " << endl;
cout << "Computer it's time for you to give me some random numbers " << endl;
do
{
if(randNum>=lowBoundary||randNum<=upBoundary||randNum++)
{
randNum=(lowBoundary+(rand()% (upBoundary-lowBoundary+1))); // pick random numbers between max and min
cout<< "Would you like to try this again? (Yes or No)" <<endl;
cin>> runAgain;
}
else
{
cout << " Please try again pick a number between" << lowBoundary << " - " << upBoundary << endl;
}
randNum=(lowBoundary+(rand()% (upBoundary-lowBoundary+1))); // pick random numbers between max and min
cout<< "Would you like to try this again? (Yes or No)" <<endl;
cin>> runAgain;
}
while(runAgain=="Yes"|| runAgain=="yes"|| runAgain=="y");
return randNum;
}
Line 25; First time through the loop, randNum is uninitialized. You're testing garbage.
What's the point of incrementing randNum in your condition? You're incrementing garbage.
line 44: main should return 0, not randNum.
I don't see anything in your code that has to do with 10 random numbers.
@integralfx I am not understanding half of that.... I am a beginner and still taking C++ course... i was told to use
to require at least 3 symbolic constants for
max=75
min=25
number of random numbers= 10
using:
lower boundary + ( rand() % (upper boundary – lower boundary + 1))
and
srand(0) however I'm using Xcode so i have to use:
srand(static_cast<unsigned int>(time(0)))
int main()
{
srand(static_cast<unsignedint>(time(0)));
constint upBoundary = 75, lowBoundary = 25, numSeries = 10;
// Unused variable?
//int num;
int randNum = rand() % 75 + 1; // Initialise randNum
string runAgain;
cout << "Welcome to Guess My Number " << endl;
cout << "Computer it's time for you to give me some random numbers " << endl;
/*
How does the user input their number?
*/
do
{
/*
|| (logical or) means that if any of the conditions evaluate to true,
the body of the statement will execute.
Since you haven't initialised randNum, who knows when this statement
will execute.
*/
if(randNum >= lowBoundary || randNum <= upBoundary /*|| randNum++*/)
{
/*
Please don't use extra parentheses for no reason. It makes the
code hard to read.
This is the order the expression below will be evaluated in:
1. (upBoundary - lowBoundary + 1)
2. rand() % above expression
3. lowBoundary + above expression
*/
randNum = lowBoundary + rand() % (upBoundary - lowBoundary + 1);
cout << "Would you like to try this again? (Yes or No)" <<endl;
cin >> runAgain;
}
else
cout << "Please try again pick a number between " << lowBoundary << " - " << upBoundary << endl;
randNum = lowBoundary + rand() % (upBoundary - lowBoundary + 1);
cout << "Would you like to try this again? (Yes or No)" << endl;
cin >> runAgain;
} while(runAgain == "Yes"|| runAgain == "yes" || runAgain == "y");
return 0; // returning 0 indicates success
}
Maybe you could have a function that generates 10 pseudo-random numbers and stores them in an array.
for the comments on them... it helps me better to understand the structure and what needed it to be done... however the user input, doesn't means user guessing the number correct? i want to have the computer pick 10 numbers for me.....
for the rest i gotta write the statement and expressions now...
I haven't learned the array yet.... I'm familiar with if statements, if else statements, switch statement...
if (randNum<75||randNum>=65)
{
cout<< "there are " << randNum << " in 65-75 range " << endl;
}
if(randNum<64||randNum>=55)
{
cout<< "there are " << randNum << " in 55-64 range" << endl;
}
if(randNum<54||randNum>=45)
{
cout<< "there are " << randNum << " in 45-54 range" << endl;
}
if(randNum<44||randNum>=35)
{
cout<< "there are " << randNum << " in 35-44 range" << endl;
}
if(randNum<34||randNum>=25)
{
cout<< "there are " << randNum << " in 25-34" << endl;
}
else
{
cout<< "I'm assuming this is all the numbers that have been checked out so far ";
}
Your conditions should be &&, not ||.
Also check your conditions (<, >=) because they to not match the ranges you stated.
e.g. The first if will NOT include 75.
You're trying to do two things at one time that need to be done separately.
You're going to need to count how many are in each range as you generate them.
Then at the end you need to report the total of how many were in rach range.
PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post. http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.