Эта программа работает и работает правильно до конца. Когда пользователь спрашивает, хочет ли он продолжить или нет, он должен напечатать «Вы выбрали выход» после первой итерации, если он решит выйти. Проблема заключается в том, что при запросе продолжения или выхода, если пользователь выбирает выход, он возвращает основные инструкции.
не обращайте внимания на функцию продолжения, которую я закомментировал.
/*This program was created to
generate user-entered number of sets
of 6 random, non-repeating numbers.
Bradley Ruiz
N01441960
COP2220
Mr. Snedden
Project 2
*/
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <ctype.h>
#define OFFSET 1 //Defined Constants indicating the beginning
#define RANGE 53//and ending of range of random numbers to choose from.
void Flush(void);
/*int ContinueFunc(void){
int success = 0, numSets = 0;
char c;
printf("You have completed your random number generation.\n");
printf("Would you like to continue with more sets or quit?\n");
printf("Enter 'q' or 'Q' to quit or a new number of sets to generate.");
success = scanf(" %d" , &numSets); //scans user input and assigns the value to success.
if(success == 0){//checks if the value of numSets is the same as originally declared or if it has changed.
scanf(" %c", &c); //if it is still 0, then it scans for a character entry.
if(tolower(c) == 'q')
return -1; //exits the function
}//end if
else
return numSets;//if previous if statements are not true, it returns the numSets value.
}*/
int PrintRand(int COUNT) { //Declaring my PrintRand function taking in an integer variable named COUNT returning no value.
uint64_t nums = 0; //declaring local variables. 64bit variable to store all random numbers generated.
unsigned num = 0; //number that is always >= 0.
int i, j, success = 0, numSets = 0;
char c;
int duplicate = 0; //variable to check for repeating numbers.
if(COUNT > 0){
printf("Your %2d set(s) of 6 random numbers is:\n" , COUNT);
}//prints out the number of sets the user chose.
for (int cnt = 0; cnt < COUNT; cnt++) { //outer loop to repeat for the number of user entered sets.
for (i = 0; i < 6; i++) {//inner loop to generate 6 random numbers.
do {
num = (rand() % RANGE) + OFFSET; //generate a random number does a modulus operation by the max value of range and adds offset to ensure number is between 1-53.
for (duplicate = 0, j = 0; j < 6; j++)//for loop to check for duplicates.
// retrieving the nth number from nums
if (((nums >> (j * 8)) & 0xff) == num)
duplicate = 1;
if (duplicate) // if there is a repeating number, it will restart.
continue;
nums |= (uint64_t)(num << (i * 8));//if not it saves the value to nums.
break;//breaks out of the do while loop.
} while (1);
printf("%2d " , num);//prints the random number with a set width while the conditions are true.
}//end inner loop
printf("\n");
}//end outer loop
printf("\n");
printf("You have completed your random number generation.\n");
printf("Would you like to continue with more sets or quit?\n");
printf("Enter 'q' or 'Q' to quit or a new number of sets to generate.\n");
success = scanf(" %d" , &numSets); //scans user input and assigns the value to success.
if(success == 0){//checks if the value of numSets is the same as originally declared or if it has changed.
scanf(" %c", &c); //if it is still 0, then it scans for a character entry.
if(tolower(c) == 'q')
return -1; //exits the function
//end if
}
else
PrintRand(numSets);//continuation loop that calls itself if a user chooses another number of sets to generate.
}//end of PrintRand function.
int PrintInstructions(void){//Function to print instructions taking in no value and returning in an integer value of the user input.
int numSets = 0; //local variables for PrintInstructions function.
int success = 0;
char c;
for(;;){
printf("******************************************************************\n");
printf("Enter the amount of sets of 6 random numbers you want to generate.\n");
printf("Enter in 'q' or 'Q' to quit...\n");
printf("******************************************************************\n");
success = scanf(" %d" , &numSets); //scans user input and assigns the value to success.
if(success == 0){//checks if the value of numSets is the same as originally declared or if it has changed.
scanf(" %c", &c); //if it is still 0, then it scans for a character entry.
if(tolower(c) == 'q')
return -1; //exits the function
}//end if
else
return numSets;//if previous if statements are not true, it returns the numSets value.
}//end for
} //end PrintInstructions function.
int main() {
srand(time(NULL)); //seeds system time so that each set of random numbers is different.
int numSets = -99;
do{
numSets = PrintInstructions(); //calls print instructions function and sets the return value = to numSets variable.
if(numSets > 0){
PrintRand(numSets);
}//end if - statement that calls the Print Rand function if the numSets variable is greater than 0.
}//end do - Loop will run until user enters the quit value.
while(numSets != -1);// end do-while loop - Loop will run until numSets = -1 which is the quit value.
printf("You have chosen to quit");
return 0;
}
void Flush(){
char c;
while((c = getchar()) != '\n' && c != EOF);
return;
}