Trying to create lotto game, having problems with execution and compilation - c++

im trying to set up a lottery game that asks for input from the user 3 times and compares this to numbers drawn by the random array. if any of these user numbers match a number in the array, the program should print out the numbers that matched.
ive tried to use while loops in my main function to control the flow but im still having issues with compilation
#include<iostream>
#include <ctime>
#include "array.h"
using std::cout; using std::cin; using std::endl;
const int sizeofArray = 20;
void assign(int wins[], int sizeofArray);
void draw(int sizeofArray, int wins[]);
bool check(int num1, int wins[], int sizeofArray);
void entry(int& userNum);
void printOut(int wins[], int num);
int main() {
srand(time(nullptr));
int user[sizeofArray];
cout << "Good luck with this weeks lotto drawing!" << endl;
int wins[sizeofArray];
assign(wins, sizeofArray);
draw(sizeofArray, wins);
int userInput;
entry(userInput);
bool win = check(userInput, wins, sizeofArray);
if (win)
cout << "you have won!" << endl;
printOut(wins, sizeofArray);
}
void assign(int wins[], int sizeofArray) {
for (int i = 0; i < sizeofArray; ++i)
wins[i] = 0;
}
void draw(int sizeofArray, int wins[]) {
int number = 0;
while (number < sizeofArray) {
int number = rand() % 100 + 1;
if (!check(number, wins, sizeofArray)) {
wins[number] = number;
number++;
}
}
}
bool check(int num1, int wins[], int sizeofArray) {
for (int j = 0; j < sizeofArray; j++) {
if (wins[j] == num1)
return true;
}
return false;
}
void entry(int& userNum) {
int j;
cout << "Enter your lottery number guess: ";
cin >> userNum;
cout << "Your number was " << userNum << endl;
}
void printOut(int wins[], int num) {
cout << "Winning numbers in lottery are" << endl;
for (int j = 0; j < num; ++j) {
cout << wins[j] << " ";
}
}
i

Your draw method is going to loop infinitely since you declared the condition variable twice which causes the ++ operator to be applied on the wrong variable
void draw(int sizeofArray, int wins[]) {
int number = 0; // <--- HERE
while (number < sizeofArray) {
int number = rand() % 100 + 1; // <--- AND HERE
if (!check(number, wins, sizeofArray)) {
wins[number] = number;
number++; //This will increase the inner variable number not the outer one
}
}
}
to fix it just change the name of your variable
void draw(int sizeofArray, int wins[]) {
int counter = 0; <---
while (counter < sizeofArray) { <---
int number = rand() % 100 + 1;
if (!check(number, wins, sizeofArray)) {
wins[number] = number;
counter++; <---
}
}
}

Let's take a look at draw():
void draw(int sizeofArray, int wins[]) {
int number = 0; //First variable of name number
while (number < sizeofArray) {
//new variable with name number is introduced, it shadows the first one
int number = rand() % 100 + 1; //<-----------------------+
if (!check(number, wins, sizeofArray)) { // |
wins[number] = number; // |
number++; //Here you actually increase this variable-+
}
}
}
So called shadowing occurs here and the outer int number is never modified inside the loop. Just rename one of them and you should be fine.
If you want to read more about this, take a look here.

Related

I get nothing when I run the program. Just gives me my exe aplication and thats it. How Do I fix it?

Code compiles fine with no errors.
But when running a program it just gives me exe program and program ends with no input or out put.
How Do I dix it. Help me out please.
Beginner programmer.
Code is not finished yet only with the output section.
What is my next steps.
Comments are appreciated.
..........................................................................................................----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Code down below
const int MONTH_QUANTITY = 12;
typedef double Rainfalls[MONTH_QUANTITY];
// Function prototypes
double findTotal(Rainfalls, double);
double findMonthlyAverage(Rainfalls, double);
double findHighest(Rainfalls, double);
double findLowest(Rainfalls, double);
void getUserInput(Rainfalls, double);
// Main function
int main(int argc, char** argv) {
Rainfalls monthArray;
void getUserInput(Rainfalls, double);
double findTotal(Rainfalls, double);
int monthlyHighest;
int monthlyLowest;
double monthlyAverage;
return 0;
}
void getUserInput(Rainfalls monthArray, double MonthlyRainfalls)
{
std::cout << "Please enter total rainfalls over a period of 12 month\n";
for(int rainfalls = 0; rainfalls < MONTH_QUANTITY; rainfalls++)
{
std::cout << "Month"<< (rainfalls + 1) << ": ";
std::cin >> monthArray[rainfalls];
while(monthArray[rainfalls] < 0)
{
std::cout << "Error! Negative numbers are not allowed";
std::cout << "Please enter amount of rainfall above or equal to 0\n";
std::cin >> monthArray[rainfalls];
}
}
}
double findTotal(Rainfalls totalMonths, double totalRainfalls)
{
double totalRainfall = 0;
for(int i = 0; i < MONTH_QUANTITY; i++)
totalRainfall += totalMonths[i];
return totalRainfall;
}
double findMonthlyAverage(Rainfalls monthlyAverage, double quantity)
{
double average = 0;
for(int rainfall = 0; rainfall < quantity; rainfall++)
average += monthlyAverage[rainfall];
return (average / quantity);
}
int findHighest(Rainfalls array, int max)
{
int highest = array[0];
for(int index = 1; index < max; index++)
{
if(array[index] > highest)
highest = array[index];
}
return highest;
}
int findLowest(Rainfalls array, int max)
{
int lowest = 0;
for(int index = 1; index < lowest; index++)
{
if(array[index] < lowest)
lowest = array[index];
}
return lowest;
}
There seems no statement to do anything (other than return 0;) in your main function:
int main(int argc, char** argv) {
Rainfalls monthArray;
void getUserInput(Rainfalls, double);
double findTotal(Rainfalls, double);
int monthlyHighest;
int monthlyLowest;
double monthlyAverage;
return 0;
}
It contains just declarations of variables and functions.
The next step will be write some code in your main() function to do some calculation that you want.

I can`t call the bool function in c++ (closed)

Hey guys please help me on this I have tried calling my bool function in my main func but it wont even show the first cout of program and the compiler terminates the program here is my code
#include <iostream>
using namespace std;
bool puzzle(int size, int array[], int start)
{
cout <<"how many blocks you want for the puzzle? \n";
cin >> size;
for (int i = 0; i < size; i++)
{
cout << "enter your numbers in order for the blocks:\n";
cin >> array[i];
if (array[0] > size) { return false; };
if (array[0] == size) { return true; };
}
}
int main()
{
puzzle;
return 0;
}
Your function has parameters so you need to call them to make it work. In this case (an example):
int size = 5;
int array[5];
int start = 0;
puzzle(size, array, start);

My Bubblesort won't sort anything, and won't let me pass by reference

For my CS class we are making a program that reads in numbers, calculates the inflation rate from those 2 numbers, sends those numbers to an array, bubblesorts the array, and then prints that array out after it is sorted. However, I can't get my bubblesort to sort, either there is a compiler error when I use &, or there it just doesnt sort. Can anyone help? Thank you!
#include
using namespace std;
double InflationRate(float old_cpi, float new_cpi);
double inflateRate;
void getCPIValues(float &old_cpi, float &new_cpi);
float swap_values(float&i, float&j);
void bubbleSort(float a[], int number_used);
float old_cpi=-1;
float new_cpi=-1;
const int MAX_RATES=20;
float a[MAX_RATES]={};
int main() //C++ programs start by executing the function main
{
char Continue = 'y';
double total = 0;
int i=0;
do{
getCPIValues(old_cpi, new_cpi);
InflationRate(old_cpi, new_cpi);
cout<< "Inflation rate is "<<inflateRate<<endl;
total = total+inflateRate;
cout << "Try again? (y or Y): ";
i++;
a[i-1]= inflateRate;
cin >> Continue;
}while ((Continue == 'y')||(Continue == 'Y'));
cout<<"Average rate is "<<total/i<<endl;
int number_used= i-1;
for (int p; p<=number_used; p++)
{
cout<<a[p]<<endl;
}
return 0;
}
double InflationRate(float old_cpi, float new_cpi)
{
inflateRate = (new_cpi - old_cpi)/old_cpi*100;
return(inflateRate);
}
void getCPIValues(float &old_cpi, float &new_cpi)
{
cout<<"Enter the old and new consumer price indices: ";
cin>>old_cpi>>new_cpi;
if ((old_cpi<=0)||(new_cpi<=0))
{
do
{
cout<<"Error: CPI values must be greater than 0";
cin>>old_cpi>>new_cpi;
}while ((old_cpi<=0)||(new_cpi<=0));
}
}
float swap_values(float&i, float&j)
{
int temp = i;
i=j;
j=temp;
return(i, j);
}
void bubbleSort(float a[], int number_used)
{
for (int m = 0; m < number_used-1; m++)
for (int n = 0; n < number_used-m-1; n++)
if (a[n] > a[n+1])
swap_values(&a[n], &a[n+1]);
}

heapsort-code is not working

The following code is not working for heap sort. It looks ok to me. Can someone help me please? I have followed the pseudo code from CLRS, the sorted numbers are not being updated after the algorithm is traversed.
#include <iostream>
using namespace std;
void max_heapify(int *b, int i,int he_size)
{
int l,r,largest;
l=2*i;
r=(2*i+1);
if (l<=he_size && b[l]>b[i])
largest=l;
else largest=i;
if (r<=he_size && b[r]> b[largest])
largest=r;
if (largest!=i)
{
swap(b[i],b[largest]);
max_heapify(b,largest,he_size);
}
}
void build_max_heap(int *c,int h_size,int strlength)
{
for (int q=(strlength)/2;q==1;--q)
{
max_heapify(c,q,h_size);
}
}
void swap(int a, int b)
{
int c=b;
b=a;
a=c;
}
int main()
{
int length;
int heap_size;
cout<<"Enter the number of numbers to be sorted by heap sort"<<endl;
cin>>length;
int* a=NULL;
a=new int[length-1];
int temp;
cout<<"Enter the numbers"<<endl;
for(int i=0;i<length;i++)
{
cin>>temp;
*(a+i)=temp;
}
cout<<"The given numbers are:"<<endl;
for(int j=0;j<length;j++)
cout<<*(a+j)<<" "<<endl;
heap_size= length;
build_max_heap(a,heap_size,length);
for (int l=length;l==2;--l)
{
swap(a[1],a[length]);
heap_size=heap_size-1;
max_heapify(a,1,heap_size);
}
cout<<"The sorted numbers are:"<<endl;
for(int j=0;j<length;j++)
cout<<*(a+j)<<" "<<endl;
system("pause");
return 0;
}
The number of mistakes in your code is enormous. Sorry to say it.
void swap(int a, int b)
{
int c=b;
b=a;
a=c;
}
does nothing - a and b should be passed by link, not by value:
void swap(int &a, int &b)
{
int c=b;
b=a;
a=c;
}
for (int q=(strlength)/2;q==1;--q) is wrong. You meant for (int q=(strlength)/2;q>1;--q). Your loop is running only when q==1.
a=new int[length-1]; The size of array should be length, not length-1. And even though swap(a[1],a[length]); is wrong, because a[length] is out of array.
Also there are some mistakes in algorithm. I tried to rewrite as less code as possible.
Right code is:
#include <iostream>
using namespace std;
void sift_down(int *a, int start, int end) {
int root = start;
while (root * 2 + 1 <= end) {
int child = root * 2 + 1;
int sw = root;
if (a[sw] < a[child])
sw = child;
if (child + 1 <= end and a[sw] < a[child + 1])
sw = child + 1;
if (sw == root)
return;
else
swap(a[root], a[sw]);
root = sw;
}
}
void max_heapify(int *b, int count) {
int start = (count - 2) / 2;
while (start >= 0) {
sift_down(b, start, count - 1);
--start;
}
}
void swap(int &a, int &b) {
int c = b;
b = a;
a = c;
}
int main() {
int length;
int heap_size;
cout << "Enter the number of numbers to be sorted by heap sort" << endl;
cin >> length;
int *a = new int[length];
cout << "Enter the numbers" << endl;
for (int i = 0; i < length; i++) {
cin >> a[i];
}
cout << "The given numbers are:" << endl;
for (int j = 0; j < length; j++)
cout << a[j] << " ";
cout << endl;
heap_size = length;
max_heapify(a, heap_size);
--heap_size;
while (heap_size) {
swap(a[heap_size], a[0]);
--heap_size;
sift_down(a, 0, heap_size);
}
cout << "The sorted numbers are:" << endl;
for (int j = 0; j < length; j++)
cout << a[j] << " ";
cout << endl;
//system("pause");
return 0;
}

Function to stall for time with <Windows.h> and continue to other functions

My waitSeconds() functions takes an integer for the number of seconds to wait. I am using the to use Sleep(msec)and converting to seconds at this point I want to try doing it like this and know it's not elegant. However my programs does not execute the rest of my functions calls and I am head banging.
Ultimately, what I want to use this functions call for is to call it with my slowTriangle() function and distressCall() that loops forever with a pause of what the parameter of waitSeconds has been passed through. I hope this last part is making sense. Anyways thank you for any guidance any of you experienced members can provide.
#include <iostream>
#include <Windows.h>
using namespace std;
int dots(int count);
int dashes(int count);
void sendSOS();
void waitSeconds(int seconds2Wait);
int triangle(int rows);
int slowTriangle();
void distressCall();
int main()
{
dots(3);
dashes(3);
sendSOS();
cout << "\n\n ";
waitSeconds(1);
int triangle(4);
int slowTriangle();
void distressCall();
return 0;
}
int dots(int count) // counts DOWN the number of dots that the int is set as a parameter
{
for (; count >= 1; count--)
{
cout << "." ;
}
return 0;
}
int dashes(int count) // counts UP the number of dots that the int is set as a parameter
{
int i;
for (; count >= 1; count--)
{
cout << "-";
}
return 0;
}
void sendSOS()
{
dots(3);
dashes(3);
dots(3);
}
void waitSeconds(int seconds2Wait) //Sleeps for time specified
{
Sleep(1000 * seconds2Wait); //converts miliseconds to seconds
seconds2Wait = 2;
}
int triangle(int rows) //Prints a dot triangle
{
int i, j;
for (i = 1; i <= rows; ++i)
{
for (j = 1; j <= i; ++j)
{
cout << ". ";
}
cout << "\n";
}
return 0;
}
int slowTriangle(int rows) //Prints a dot triangle with sleep paramter passed in
{
int i, j, seconds2Wait;
for (i = 1; i <= rows; ++i)
{
for (j = 1; j <= i; ++j)
waitSeconds(3);
{
cout << ". ";
}
cout << "\n";
}
return 0;
}
void distressCall()
{
sendSOS();
waitSeconds(2);
}
The correct C++ answer is already in the comments: std::this_thread::sleep_for
The WinAPI method (Sleep) is also possible.
The real reason why your "functions" appear to fail is because int triangle(4) defines a new variable in main, initialized to 4. This variable hides the global triangle function.
#include <ctime>
void pause (unsigned int seconds)
{
time_t goal = seconds + time(0);
while (goal > time(0));
}
#include <iostream>
#include <ctime>
using namespace std;
int dots(int count);
int dashes(int count);
void sendSOS();
void waitSeconds(int seconds2Wait);
int triangle(int rows);
int slowTriangle(int rows);
void distressCall();
int main()
{
dots(3);
dashes(3);
sendSOS();
cout << "\n\n";
waitSeconds(1);
triangle(4);
slowTriangle(4);
distressCall();
return 0;
}
int dots(int count) // counts DOWN the number of dots that the int is set as a parameter
{
for (; count >= 1; count--)
{
cout << "." ;
}
return 0;
}
int dashes(int count) // counts UP the number of dots that the int is set as a parameter
{
for (; count >= 1; count--)
{
cout << "-";
}
return 0;
}
void sendSOS()
{
dots(3);
dashes(3);
dots(3);
}
void waitSeconds(int seconds2Wait) //Sleeps for time specified
{
time_t goal = seconds2Wait + time(0);
while (goal > time(0));
}
int triangle(int rows) //Prints a dot triangle
{
int i, j;
for (i = 1; i <= rows; ++i)
{
for (j = 1; j <= i; ++j)
{
cout << ". ";
}
cout << "\n";
}
return 0;
}
int slowTriangle(int rows) //Prints a dot triangle with sleep paramter passed in
{
int i, j;
for (i = 1; i <= rows; ++i)
{
for (j = 1; j <= i; ++j)
{
waitSeconds(3);
cout << ". ";
}
cout << "\n";
}
return 0;
}
void distressCall()
{
sendSOS();
waitSeconds(2);
}
Implemented pause function as suggested and it works as intended now. Kudos to our other friend who wrote the ctime pause function. I also fixed some of your syntax errors :)
If you want distressCall() to run forever make it like this:
void distressCall()
{
while(true)
{
sendSOS();
waitSeconds(2);
}
}
Strongly recommend that you implement an escape mechanism though :).