I'm working on an assignment where I have to create a program that:
display header for (voterCount = 0; voterCount < numVoters; voter++)
use random number generator to:
generate bYear (restricted between 1900 and 2000)
generate bMonth
generate bDay (limit 1 and 31)
I have the first 4 down:
#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;
int main()
{
int numVoters;
char name[35]; // Variable that stores "Name" of user added max char to be 25
const int MIN_MONTH = 1;
const int MAX_MONTH = 12;
int bMonth;
cout << "Please enter your first name: ";
cin.getline(name,35); // used getline because without getline I kept getting a single char
cout << "Hello " << name << endl;
cout << "Please enter amount of voters: ";
cin >> numVoters;
while (numVoters > 0)
{
cout << "You entered: " <<numVoters << endl;
}
cout << "ERROR! ERROR! WRONG DATA TYPE PLEASE RUN THE PROGRAM
AGAIN";
return 0;
}
I don't know what to do next I've tried to continue by using what I see online but when I do I get a never-ending loop of # of voters
#include < iostream >
#include < cstdlib >
#include < time.h >
using namespace std;
int main() {
int numVoters;
char name[35]; // Variable that stores "Name" of user added max char to be 25
const int MIN_MONTH = 1;
const int MAX_MONTH = 12;
int bMonth;
cout << "Please enter your first name: ";
cin.getline(name, 35); // used getline because without getline I kept getting a single char
cout << "Hello " << name << endl;
cout << "Please enter amount of voters: ";
cin >> numVoters;
while (numVoters > 0) {
cout << "You entered: " << numVoters << endl;
}
cout << "ERROR! ERROR! WRONG DATA TYPE PLEASE RUN THE PROGRAM AGAIN";
unsigned seed = time(0); // gets current computer time
srand(seed); // seeds the random number gen
for (numVoters = 0; numVoters < 0; numVoters++)
{
bMonth = rand() % (MAX_MONTH - MIN_MONTH + 1) + MIN_MONTH;
cout << "Random month: " << bMonth << endl;
}
return 0;
}
Help with #5-7
while (numVoters > 0)
{
cout << "You entered: " <<numVoters << endl;
}
cout << "ERROR! ERROR! WRONG DATA TYPE PLEASE RUN THE PROGRAM
AGAIN";
The code above is saying, that as long as numVoters is more than 0, endlessly output the numVoters. What you need is an if else
statement, as you want to say if the number of voters is above 0, output the number of voters and continue to do tasks 5,6, and 7. Otherwise, you want to output error. In pseudocode:
if (numVoters > 0) {
output numVoters
do tasks 5,6,7
}
else {
output error
return 0
}
Or you could just instead do if numVoters is equal or less than 0 output error and quit the program. Place code for task 5,6,7 after the if loop, so it executes if there is no error only:
if (numVoters =< 0) {
output error
return 0
}
//code for 5,6,7 after and outside of the if loop
Also (voterCount = 0; voterCount < numVoters; voter++)
, seems to me to be (int voterCount = 0; voterCount < numVoters; voterCount++)
.
In general, I think you misunderstand while and for loops. You should read up on them to understand where you went wrong.
As someone stated your while loop is infinite if a valid value is given
gettimeofday() information may be found here http://man7.org/linux/man-pages/man2/gettimeofday.2.html
rand_r() information can be found here https://linux.die.net/man/3/rand_r
Keep these handy, these are good references for future coursework!
#include <iostream>
#include <cstdlib>
#include <time.h>
#include <sys/time.h>
using namespace std;
const int MAX_LENGTH = 35;
const int MAX_MONTH = 12;
const int MAX_DAY = 31;
const int MAX_YEAR = 100;
void getName(char name []);
void sayName(char name []);
int getVoters();
int main() {
int numVoters;
char name[MAX_LENGTH]; // Variable that stores "Name" of user added max char to be 25
struct timeval tv;
getName(name);
sayName(name);
numVoters = getVoters();
unsigned int seed = time(0); // gets current computer time
gettimeofday(&tv,NULL);
int day = 0;
int month = 0;
int year = 0;
int voterNumber = 1;
for (int i = numVoters;i > 0;i--)
{
seed = (unsigned int)((tv.tv_sec+tv.tv_usec)*i);
month = rand_r(&seed)%MAX_MONTH + 1;
day = rand_r(&seed)%MAX_DAY + 1;
year = rand_r(&seed)%(MAX_YEAR+1) + 1900;
cout<<"Voter #"<<voterNumber++<<" DD/MM/YYY DOB: ";
cout<<day<<"/"<<month<<"/"<<year<<endl;
}
return 0;
}
void getName(char name[])
{
cout<<"Please enter your first name: ";
cin.getline(name,35);
}
void sayName(char name [])
{
cout<<"Hello "<<name<<endl;
}
int getVoters()
{
int voters = -1;
cout<<"Please enter amount of voters: ";
cin>>voters;
if(voters < 0)
{
cout << "ERROR! ERROR! WRONG DATA TYPE PLEASE RUN THE PROGRAM AGAIN";
}
else if(voters == 0)
{
cout<<"Either you entered 0 or an invalid date type";
}
else
{
cout << "You entered: " << voters << endl;
}
return voters;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.