Can you help me to solve the problem in 315th line pleasee

Pages: 12
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>

using namespace std;

void generateCoupon(int columnCount);
void generateCouponWithConstant(int columnCount, int constantNumber, int constantColumnCount);
void generateCouponWithDistance(int columnCount, int distance);
void generateCouponWithRange(int columnCount, int lowerLimit, int upperLimit, int numberCount);
int myRand();
int getRange(int number, int startIndex, int endIndex);

int main() {
srand(time(0));
bool EXIT,undefined,undefined2,undefined3,undefined4,undefined5,undefined6;
int option;
int columnCount,upperLimit,lowerLimit,distance,numberCount,constantNumber,constantColumnCount;


cout<<"########################################"<<endl;
cout<<"############COUPON GENERATOR############"<<endl;
cout<<"########################################"<<endl;
cout<<"Press 1 ------>Generate lotto coupon"<<endl<<"Press 2 ------>Generate lotto coupon with a constant number"<<endl;
cout<<"Press 3 ------>Generate lotto coupon with number range"<<endl<<"Press 4 ------>Generate lotto coupon with number distance"<<endl;
cout<<"Press 0 ------>Exit"<<endl;

while(true){
cout<<"Enter your option"<<endl;
cin>>option;
if(cin.fail()) {
cin.clear(); //To clear the error flag.
cin.sync(); //To clear the input stream.
cout<<"Undefined option!"<<endl;
}
else if(option<0 || option>4) {
cout<<"Undefined option!"<<endl; //To handle the input boundaries.
}
else break;

}



while(!EXIT) {
switch(option) {
case 0: {

EXIT=true;
break;
}
case 1: {
cout<<"You chose to generate lotto coupon."<<endl;
cout<<"Enter column count"<<endl;
cin>>columnCount;
generateCoupon(columnCount);
cout<<"\n";





EXIT=true;
break;
}
case 2 : {
cout<<"You chose to generate lotto coupon with a constant number."<<endl;
cout<<"Enter column count"<<endl;
cin>>columnCount;
while(!undefined) {
cout<<"Enter const. number"<<endl;
cin>>constantNumber;
if(constantNumber<1 || constantNumber>49) cout<<"Number must be between 1 to 49"<<endl;
else undefined=true; //If the input is convenient,loop will not be repeated.
}

while(!undefined2) {
cout<<"Enter const. column count"<<endl;
cin>>constantColumnCount;
if(constantColumnCount<1 || constantColumnCount>columnCount) cout<<"Const. clumn count cannot be greater than column count or smaller than 1"<<endl;
else undefined2=true; //If the input is convenient,loop will not be repeated.
}
generateCouponWithConstant(columnCount, constantNumber, constantColumnCount);
cout<<"\n";

EXIT=true;
break;
}

case 3 : {
cout<<"You chose to generate lotto coupon with number range."<<endl;
cout<<"Enter column count"<<endl;
cin>>columnCount;
while(!undefined3) {
cout<<"Enter lower limit of range"<<endl;
cin>>lowerLimit;
if(lowerLimit<1 || lowerLimit>49) cout<<"Must be between 1 to 49"<<endl;
else undefined3=true; //If the input is convenient,loop will not be repeated.
}

while(!undefined4) {
cout<<"Enter upper limit of range"<<endl;
cin>>upperLimit;
if(upperLimit<1 || upperLimit>49 || upperLimit<=lowerLimit) cout<<"Must be between 1 to 49 and greater than lower limit"<<endl;
else undefined4=true; //If the input is convenient,loop will not be repeated.
}
while(!undefined5) {
cout<<"Enter number count in range"<<endl;
cin>>numberCount;
if(numberCount<1 || numberCount>6) cout<<"Cannot be greater than 6 or smaller than 1"<<endl;
else undefined5=true; //If the input is convenient,loop will not be repeated.
}
generateCouponWithRange(columnCount, lowerLimit, upperLimit, numberCount);
cout<<"\n";

EXIT=true;
break;

}
case 4: {
cout<<"You chose to generate lotto coupon with number distance."<<endl;
cout<<"Enter column count"<<endl;
cin>>columnCount;
while(!undefined6) {
cout<<"Enter min. distance between numbers"<<endl;
cin>>distance;
if(distance<1 || distance>9) cout<<"Cannot be greater than 9 or smaller than 1"<<endl;
else undefined6=true; //If the input is convenient,loop will not be repeated.
}
generateCouponWithDistance(columnCount, distance);
cout<<"\n";

EXIT=true;
break;

}


default: {

cout<<"Undefined option!"<<endl;
cin.clear();
cin.sync();
break;
}
}
}

return 0;

}







void generateCoupon(int columnCount)
{
int i,j,k,n;
int numbers[6] = {};
bool equal;



for (k=0 ; k<columnCount ; k++) {
for (i=0 ; i<6 ; i++ ) {
do
{
n=myRand();
equal=true; //If equal's value didn't change,loop will not be repeated for the current value of i.
for(j=0 ; j<i ;j++ ) {
if(numbers[j]==n) { //To check if any equal numbers obtained.
equal=false; //In case of equality,loop will be repeated until all numbers have distinct values.
break; //No need to go all through every other number.
}
}
} while (!equal);
numbers[j]=n;
cout<<numbers[j]<<" ";
}
cout<<endl;
}
}

void generateCouponWithConstant(int columnCount, int constantNumber, int constantColumnCount)

{

int i,j,k,n,l;
int numbers[6] = {};
bool equal;


for(k=0; k<constantColumnCount ; k++) {
cout<<constantNumber<<" ";
for (i=0 ; i<5 ; i++ ) { //i<5 because constant numbers is already displayed,since there are 5 numbers left to generate and display.
do
{
n=myRand();
equal=true; //If equal's value didn't change,loop will not be repeated for the current value of i.
for(j=0 ; j<i ;j++ ) {
if(numbers[j]==n || numbers[j]==constantNumber) { //To check if any equal numbers obtained.
equal=false; //In case of equality,loop will be repeated until all numbers have distinct values.
break; //No need to go all through every other number.
}
}
} while (!equal);
numbers[i]=n;
cout<<numbers[i]<<" ";
}
cout<<endl;
}
for(l=0; l<(columnCount - constantColumnCount) ; l++) {
for (i=0 ; i<6 ; i++ ) {
do
{
n=myRand();
equal=true; //If equal's value didn't change,loop will not be repeated for the current value of i.
for(j=0 ; j<i ;j++ ) {
if(numbers[j]==n) { //To check if any equal numbers obtained.
equal=false; //In case of equality,loop will be repeated until all numbers have distinct values.
break; //No need to go all through every other number.
}
}
} while (!equal);
numbers[i]=n;
cout<<numbers[i]<<" ";
}
cout<<endl;

}


}

void generateCouponWithRange(int columnCount, int lowerLimit, int upperLimit, int numberCount)

{

int i,m,n,j,k,l;
int numbers[6] = {};
bool equal;


for(j=0; j<columnCount ; j++) {
for(k=0 ; k<numberCount ; k++) {
do
{
n=myRand() % (upperLimit - lowerLimit + 1) + lowerLimit; //To obtain a number between lowerLimit to upperLimit.
equal=true; //If equal's value didn't change,loop will not be repeated for the current value of i.
for(i=0 ; i<k ;i++ ) {
if(numbers[i]==n) { //To check if any equal numbers obtained.
equal=false; //In case of equality,loop will be repeated until all numbers have distinct values.
break; //No need to go all through every other number.
}
}
} while (!equal);
numbers[i]=n;
cout<<numbers[i]<<" ";
}
for(l=numberCount ; l<6 ; l++) {
do
{
m=myRand();
equal=true; //If equal's value didn't change,loop will not be repeated for the current value of i.
for(i=0 ; i<l ;i++ ) {
if(numbers[i]==m) { //To check if any equal numbers obtained.
equal=false; //In case of equality,loop will be repeated until all numbers have distinct values.
break; //No need to go all through every other number.
}
}
} while (!equal);
numbers[i]=m;
cout<<numbers[i]<<" ";
}
cout<<endl;
}




}

void generateCouponWithDistance(int columnCount, int distance)
{

int a[6] = {0};
int i,j,k;

for(j=0 ; j<columnCount ; j++) {
while(true) {
a[0]=myRand();
for(i=1 ; i<6 ; i++) {
a[i]=a[i-1] + myRand() % (10 - distance) + distance; //Any numbers in array will have a difference as at least the distance and 9 at maxium.
}
if(a[5]<=49) break; //In case of overflow,start again.
}
for(k=0 ; k<6 ; k++) {
cout<<a[k]<<" ";
}
cout<<endl;
}




}

int myRand()
{
int random=rand() % 500 + 499; //A value between 500 to 999 is generated.
int random2=pow(random, 3); // Cube of the number is obtained to have a 9 digit number.
int randomFinal= getRange(random2, 3, 7); //A sub number is generated using getRange function.
return (randomFinal % 49) + 1; //Obtained a random number between 1 to 49 using modulus operation.

}

int getRange(int number, int startIndex, int endIndex)

{
int i,x,number1,number2,number3,number4;
x=number;



for (i=0 ; x>10 ; i++) {

x=x/10; // To determine how many digits number has,it is divided until it becomes smaller than 10.
}


number1=number/pow(10, i - startIndex + 2);
number2=number1*pow(10, i - startIndex + 2);
number3=number - number2;
number4=number3/pow(10, i - endIndex + 1);
return number4;


}
It gives an error
compp,
I compiled your code and I didnt get any errors. I ran the program but came up with strange results. So I ran it through debug and came to this solution.

I have 2 suggestions for your program,

1. You should always initialize variables before testing them.
here you have Boolean variables declared

bool EXIT,undefined,undefined2,undefined3,undefined4,undefined5,undefined6;

and you are checking if these values are set to something:
while(!undefined)

but there isn't anything in between where the value was set to anything.

2. Dont be afraid of using your debugger to watch the variable values, step through your code.

I hope this helps.
Last edited on
But as you write I initialize undefined? You mean I should write
undefined = true; ?
Exactly. you need to add that to somewhere in your code,
maybe right before your first cout<< statement.

something like this would speed it up:
EXIT=undefined=undefined2=undefined3=undefined4=undefined5=undefined6=false;
I will try this thank you so much
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>

using namespace std;

void generateCoupon(int columnCount);
void generateCouponWithConstant(int columnCount, int constantNumber, int constantColumnCount);
void generateCouponWithDistance(int columnCount, int distance);
void generateCouponWithRange(int columnCount, int lowerLimit, int upperLimit, int numberCount);
int myRand();
int getRange(int number, int startIndex, int endIndex);

int main() {
srand(time(0));
bool EXIT=undefined=undefined2=undefined3=undefined4=undefined5=undefined6=true;
int option;
int columnCount,upperLimit,lowerLimit,distance,numberCount,constantNumber,constantColumnCount;


cout<<"########################################"<<endl;
cout<<"############COUPON GENERATOR############"<<endl;
cout<<"########################################"<<endl;
cout<<"Press 1 ------>Generate lotto coupon"<<endl<<"Press 2 ------>Generate lotto coupon with a constant number"<<endl;
cout<<"Press 3 ------>Generate lotto coupon with number range"<<endl<<"Press 4 ------>Generate lotto coupon with number distance"<<endl;
cout<<"Press 0 ------>Exit"<<endl;

while(true){
cout<<"Enter your option"<<endl;
cin>>option;
if(cin.fail()) {
cin.clear(); //To clear the error flag.
I did what you said but still i'm getting an erros , i think im goint to get crazy:(
I didnt say remove the declarations of the booleans,
try to make it look like this:

1
2
3
4
5
6
7
8
9
10
    srand(time(0));
    bool EXIT,undefined,undefined2,undefined3,undefined4,undefined5,undefined6;
    int option;
    int columnCount,upperLimit,lowerLimit,distance,numberCount,constantNumber,constantColumnCount;


    EXIT=undefined=undefined2=undefined3=undefined4=undefined5=undefined6=false;  //this is the added line


    cout<<"########################################"<<endl;



But you mentioned you got errors, are they syntax errors? post them up if they're really diffrent.
Last edited on

int random=rand() % 500 + 499; //A value between 500 to 999 is generated.
double random2=pow(random, 3); // Cube of the number is obtained to have a 9 digit number.
int randomFinal= getRange(random2, 3, 7); //A sub number is generated using getRange function.



ERRORS
316 C:\Users\xxx\Desktop\cpp ödev3.cpp call of overloaded `pow(int&, int)' is ambiguous

317 C:\Users\xxx\Desktop\cpp ödev3.cpp [Warning] passing `double' for converting 1 of `int getRange(int, int, int)'

337 C:\Users\xxx\Desktop\cpp ödev3.cpp call of overloaded `pow(int, int)' is ambiguous


339 C:\Users\xxx\Desktop\cpp ödev3.cpp call of overloaded `pow(int, int)' is ambiguous



I always get these erros. And the random part is red :( I mean there is an error.By the way, thank you so much for your help
double random part
You'll need to cast whatever is causing the ambiguity.

one clue, in your myRand() function, you are using random2 as an double, but you pass it to getRange as an int. you can cast the variables passing to pow to see if that clears the error.

maybe something like this for your myRand() function:

1
2
3
4
5
6
7
8
9
int myRand()
{
    int random, random2, randomFinal;

    random=rand() % 500 + 499; //A value between 500 to 999 is generated.
    random2= (int)pow( (int)random, 3); // Cube of the number is obtained to have a 9 digit number.
    randomFinal= getRange(random2, 3, 7); //A sub number is generated using getRange function.
    return (randomFinal % 49) + 1; //Obtained a random number between 1 to 49 using modulus operation.
}
Last edited on
since pow is already a function which is in the cmath library i cant write int
random2= (int)pow((int)random, 3);
will cast the return value of POW to an int instead of a double.

what do you have for the lines of code for line 315-317 on your program? maybe i'm not seeing the rest of the functions declarations and return types.
317 is :
int random2=pow(random, 3);

337: number2=number1*pow(10, i - startIndex + 2);


the error of 337 is 337 C:\Users\xxx\Desktop\cpp ödev3.cpp call of overloaded `pow(int, int)' is ambiguous
again
This is where things get tricky, and it could be becuase of your compiler. do you know if you can change compiler options to be less strict?
on my mingw compiler i'm not getting any complaints on how you declared these calls.

you'll have to selectively cast the parameters passed to POW.
(double,int) seems to be what you're looking for,

1
2
317 is :
int random2=pow((double)random, 3);


337: number2=number1*pow((double)10, (int)(i - startIndex + 2));
I use dev++ and ı dont know how to change compiler , but now i'm checking
When i did what you have said, i get an another error:D which is cannot convert int to double since we try to make 10 double
When yuo execute the program, does it working correctly?
Pages: 12