Converting Time how can I improve my code? [closed] - c
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Just started programming so any suggestion or constructive criticism would be much appreciated. I'm pretty sure there are ways to shorten this I just don't know how, especially the use of functions.
#include <stdio.h>
#include <stdlib.h>
int input, check = 1, ans;
int menu ();
int convert24(int hour, int min, int sec, int meridiem);
int convert12(int hour, int min, int sec, int meridiem);
int result(int hour, int min, int sec, int meridiem);
void processAnother();
int main()
{
do
{
menu();
processAnother();
system("cls");
}while (ans = 'y');
}
int menu ()
{
int hour, min, sec, meridiem;
printf("[1] Convert to 24-hour notation\n");
printf("[2] Convert to 12-hour notation\n");
printf("[3] Quit\n");
scanf("%d", &input);
switch(input)
{
case 1 :
{
printf("Enter hours (1-12):minute(1-59):seconds(1-59): ");
scanf("%d %d %d", &hour, &min, &sec);
printf("[1] AM or [2] PM: ");
scanf(" %d", &meridiem);
return convert24(hour, min, sec, meridiem);
break;
}
case 2 :
{
printf("Enter hours(1-24):minute(1-59):seconds(1-59): ");
scanf("%d %d %d", &hour, &min, &sec);
printf("[1]AM or [2]PM: ");
scanf(" %d", &meridiem);
return convert12(hour, min, sec, meridiem);
break;
}
default :
{
printf("Goodbye :)");
exit(0);
}
}
}
int convert24(int hour, int min, int sec, int meridiem)
{
if ((hour > 12 || hour < 0))
{
printf("\nError! Invalid Hour\n");
check = 0;
}
if ((min >= 60 || min <0))
{
printf("\nError! Invalid Minute\n");
check = 0;
}
if ((sec >= 60 || sec <0))
{
printf("\nError! Invalid Second\n");
check = 0;
}
if (meridiem > 2 || meridiem < 0)
{
printf("\nError! Invalid Meridiem\n");
check = 0;
}
if (check == 1)
{
if (meridiem == 1)
{
if (hour == 12)
{
hour = 0;
result(hour, min, sec, meridiem);
}
else
{
result(hour, min, sec, meridiem);
}
}
else if (meridiem == 2)
{
if (hour == 12)
{
hour = 12;
result(hour, min, sec, meridiem);
}
else
{
hour+=12;
result(hour, min, sec, meridiem);
}
}
}
processAnother();
}
int convert12(int hour, int min, int sec, int meridiem)
{
if ((hour > 24 || hour < 0))
{
printf("\nError! Invalid Hour\n");
check = 0;
}
if ((min >= 60 || min <0))
{
printf("\nError! Invalid Minute\n");
check = 0;
}
if ((sec >= 60 || sec <0))
{
printf("\nError! Invalid Second\n");
check = 0;
}
if (meridiem > 2 || meridiem < 0)
{
printf("\nError! Invalid Meridiem\n");
check = 0;
}
if (check == 1)
{
if (meridiem == 1)
{
if (hour <= 12)
{
result(hour, min, sec, meridiem);
}
else
{
hour -= 12;
result(hour, min, sec, meridiem);
}
}
else if (meridiem == 2)
{
if (hour <= 12)
{
result(hour, min, sec, meridiem);
}
else
{
hour -= 12;
result(hour, min, sec, meridiem);
}
}
}
processAnother();
}
int result(int hour, int min, int sec, int meridiem)
{
char *x;
int y;
if(meridiem == 1)
{
x = "AM";
}
else if (meridiem == 2)
{
x = "PM";
}
if (input == 1)
{
y = 24;
}
else if (input == 2)
{
y = 12;
}
printf("The %d-hr Notation is : %d:%d:%d %s\n",y, hour, min, sec, x);
return;
}
void processAnother()
{
printf("Process another [y/n]");
scanf("%c", &ans);
return;
}
Thank you very much.
ans, is used as a char but declared as a int, it will work but it's ugly, also don't abuse global variable, input, check and ans can be declared somewhere else and some of them dont even need to be declared.
On convert12() and convert24() you're doing the same thing on the 20 first line, write a function that return 0 if they're an error and 1 otherwise.
I usualy put the main() function at the bottom of my file it allow me to not prototype every other function on the file
Instead of passing hour, min, sec and meridiem everytime you call most of your functions, create a struct with every element inside and pass a pointer of it.
main() should be declared like this main(void) see here for more information
for this one i'm gona use multiple steps
first:
int convert12(int hour, int min, int sec, int meridiem)
{
if ((hour > 24 || hour < 0))
{
printf("\nError! Invalid Hour\n");
check = 0;
}
if ((min >= 60 || min <0))
{
printf("\nError! Invalid Minute\n");
check = 0;
}
if ((sec >= 60 || sec <0))
{
printf("\nError! Invalid Second\n");
check = 0;
}
if (meridiem > 2 || meridiem < 0)
{
printf("\nError! Invalid Meridiem\n");
check = 0;
}
if (check == 1)
{
if (meridiem == 1)
{
if (hour <= 12)
{
result(hour, min, sec, meridiem);
}
else
{
hour -= 12;
result(hour, min, sec, meridiem);
}
}
else if (meridiem == 2)
{
if (hour <= 12)
{
result(hour, min, sec, meridiem);
}
else
{
hour -= 12;
result(hour, min, sec, meridiem);
}
}
}
processAnother();
}
can become:
int convert12(my_time_t *time, int input)
{
if (are_time_correct(time, input))
{
if (time->meridiem == 1)
{
if (time->hour <= 12)
{
}
else
{
time->hour -= 12;
}
}
else if (time->meridiem == 2)
{
if (time->hour <= 12)
{
}
else
{
time->hour -= 12;
}
}
}
result(time, input);
processAnother();
}
I followed my previous advice and moved the call of result at the end
I know it won't work like this but you can now write it like that:
int convert12(my_time_t *time, int input)
{
if (are_time_correct(time, input))
{
if (time->meridiem == 1 && time->hour > 12)
{
time->hour -= 12;
}
else if (time->meridiem == 2 && time->hour > 12)
{
time->hour -= 12;
}
}
result(time, input);
processAnother();
}
just before we tested if meridiem was between 1 and 2 so we can write this function like this
int convert12(my_time_t *time, int input)
{
if (are_time_correct(time, input) && time->hour > 12)
{
time->hour -= 12;
}
result(time, input);
processAnother();
}
do the same process for convert24()
If you followed all of my instruction you should have something like this:
#include <stdio.h>
#include <stdlib.h>
// time_t aleardy exist so I used my_time_t instead
typedef struct my_time_s
{
int hour;
int min;
int sec;
int meridiem;
} my_time_t;
void processAnother(void)
{
char ans;
printf("Process another [y/n]");
// note the space at the start
// that means to discard all whitespace
// exit if ans different than 'y'
if (scanf(" %c", &ans) <= 0 || ans != 'y')
exit(0);
}
// don't prototype your fuctions with int if they return nothing
void result(my_time_t *time, int input)
{
// use variable with explicit name
char *meridiem;
int notation;
if(time->meridiem == 1)
{
meridiem = "AM";
}
else if (time->meridiem == 2)
{
meridiem = "PM";
}
// it's the same as old notation if you don't know the << operator search what bitshift is on the internet
// y = 6 << input <==> y = 6 * 2 ^ input
// so if input = 1 : y = 6 * 2 ^ 1 = 12
// if input = 2 : y = 6 * 2 ^ 2 = 24
notation = 6 << input;
printf("The %d-hr Notation is : %d:%d:%d %s\n", notation, time->hour, time->min, time->sec, meridiem);
return;
}
int are_time_correct(my_time_t *time, int input)
{
int check = 1;
// double parenthesis are not needed
// (6 << input) can be equal at 12 or 24 in function of input see above for more informations
if (time->hour > (6 << input) || time->hour < 0)
{
printf("\nError! Invalid Hour\n");
check = 0;
}
if (time->min >= 60 || time->min < 0)
{
printf("\nError! Invalid Minute\n");
check = 0;
}
if (time->sec >= 60 || time->sec < 0)
{
printf("\nError! Invalid Second\n");
check = 0;
}
// i don't think the meridiem can be below 0 so I replace (time->meridiem < 0) by (time->meridiem < 1)
if (time->meridiem > 2 || time->meridiem < 1)
{
printf("\nError! Invalid Meridiem\n");
check = 0;
}
return (check);
}
int convert24(my_time_t *time, int input)
{
if (are_time_correct(time, input) && time->hour == 12)
{
// if time->meridiem == 1 :
// time->hour = 12 * (1 - 1) = 0
// if time->medriem == 2 :
// time->hour = 12 * (2 - 1) = 12
time->hour = 12 * (time->meridiem - 1);
}
result(time, input);
// processAnother(); is not needed you already do it on the main loop
return (0);
}
int convert12(my_time_t *time, int input)
{
if (are_time_correct(time, input) && time->hour > 12)
{
time->hour -= 12;
}
result(time, input);
// processAnother(); is not needed you already do it on the main loop
return (0);
}
int menu(void)
{
int input;
my_time_t time;
// string concatenation
printf("[1] Convert to 24-hour notation\n"
"[2] Convert to 12-hour notation\n"
"[3] Quit\n");
if (scanf(" %d", &input) <= 0)
{
printf("\nWrong input\n");
input = 3;
}
switch(input)
{
case 1 :
{
printf("Enter hours (1-12):minute(1-59):seconds(1-59): ");
if (!scanf(" %d", &time.hour) <= 0 ||
!scanf(" %d", &time.min) <= 0 ||
!scanf(" %d", &time.sec) <= 0)
{
printf("\nWrong input\n");
return (1);
}
printf("[1] AM or [2] PM: ");
if (!scanf(" %d", &time.meridiem) <= 0)
{
printf("\nWrong input\n");
return (1);
}
return convert24(&time, input);
break;
}
case 2 :
{
printf("Enter hours(1-24):minute(1-59):seconds(1-59): ");
if (scanf(" %d", &time.hour) <= 0 ||
scanf(" %d", &time.min) <= 0 ||
scanf(" %d", &time.sec) <= 0)
{
printf("\nWrong input\n");
return (1);
}
printf("[1]AM or [2]PM: ");
if (scanf(" %d", &time.meridiem) <= 0)
{
printf("\nWrong input\n");
return (1);
}
return convert12(&time, input);
break;
}
default :
{
printf("Goodbye :)");
exit(0);
}
}
return (0);
}
// function without parameters are declared with void
int main(void)
{
// a while loop is simpler imo
while (1)
{
// if menu() return 1 it means the user enter a wrong input
// in that case no need to call processAnother()
if (!menu())
{
processAnother();
}
// DON'T USE SYSTEM NEVER EVER SEE HERE FOR MORE INFO https://stackoverflow.com/questions/19913446/why-should-the-system-function-be-avoided-in-c-and-c
// anyway some shell (such as mine) don't reconise `cls` and use clear instead
system("cls");
}
return (0);
}
you also didn't test if scanf returned something, they're some case where you program will segfault
Related
Incorrect calculations of averages
I have this super simple code for calculating averages of given even and odd numbers, until the user gives 0. (I would use for loop but we can't). I'm having a really strange problem with program rounding results like 25/2 is 2.00000. Sorry if this question is stupid but I just can't find a problem. What am I doing completely wrong? #include <stdio.h> #include <stdlib.h> void funkcja() { int sumaNiep = 0; int sumaPa = 0; int userInput = 1; int i = 0; while(userInput != 0) { //wprow zmiennej printf("%d. Podaj calkowita liczbe: ", i+1); scanf("%d", &userInput); //jesli parzysta if (userInput % 2 == 0) { sumaPa += userInput; } else { sumaNiep += userInput; } i++; } double sredniaNiep = sumaNiep/(i-1); double sredniaPa = sumaPa/(i-1); printf("\nsrednia parzysta %d / %d : %lf", sumaPa, i, sredniaPa); printf("\nsrednia parzysta %d / %d : %lf", sumaNiep, i, sredniaNiep); } int main() { funkcja(); }
The problem is that you do an integer division at the end. You should break out of the loop if the user enters 0 and make at least one operand a double when you do the division. You also need to count the number of evens and odds: #include <stdio.h> #include <stdlib.h> void funkcja() { int sumaNiep = 0; int sumaPa = 0; int userInput = 1; int iPa = 0; int iNiep = 0; int i = 0; while(1) { printf("%d. Podaj calkowita liczbe: ", ++i); if(scanf(" %d", &userInput) != 1 || userInput == 0) break; // break out // jesli parzysta if(userInput % 2 == 0) { sumaPa += userInput; ++iPa; // count evens } else { sumaNiep += userInput; ++iNiep; // count odds } } if(iPa) { // avoid div by zero double sredniaPa = (double)sumaPa / iPa; // double div printf("srednia parzysta %d / %d : %lf\n", sumaPa, iPa, sredniaPa); } if(iNiep) { // avoid div by zero double sredniaNiep = (double)sumaNiep / iNiep; // double div printf("srednia parzysta %d / %d : %lf\n", sumaNiep, iNiep, sredniaNiep); } }
The problem was I divided by the number of all digits (odd and even) to calculate both averages. Here is improved code: #include <stdio.h> #include <stdlib.h> void funkcja() { int sumaNiep = 0; int sumaPa = 0; int userInput = 1; int i_p = 0, i_np = 0; while(userInput != 0) { //wprow zmiennej printf("%d. Podaj calkowita liczbe: ", i_p+i_np+1); scanf("%d", &userInput); //jesli parzysta if (userInput % 2 == 0) { sumaPa += userInput; if (userInput != 0) { i_p++; } } else { sumaNiep += userInput; i_np++; } } if (i_np != 0) { double sredniaNiep = sumaNiep/(i_np); printf("\nSrednia nieparzysta %d / %d : %lf", sumaNiep, i_np, sredniaNiep); } if (i_p != 0) { double sredniaPa = sumaPa/(i_p); printf("\nSrednia parzysta %d / %d : %lf", sumaPa, i_p, sredniaPa); } } int main() { funkcja(); }
How can I fix my problem about terminating the program instead of repeat it from the start using while loop? C Language
I have new problem about while-loop! When I input the l_res as y it means that the condition of while loop is still TRUE, instead repeat it from the start, it terminated the program. → enter image description here int main() { //VARIABLES char f_res[4]; char l_res = 'y'; int a,b,c,x; while(l_res == 'y') { printf("Enter 3 digit capacitor Code: "); scanf("%d %d %d",&a,&b,&c); x = (a*10)+b; printf("What is the unit of the value?(Pico-picofarad, Nano-Nanofarad): "); scanf("%s", f_res); if(strcmp(f_res, "Pico") == 0) { if(c == 0) { x = x; } else { while(c <= 5 && c > 0) { c--; x = x*10; } } printf("\nCapacitor value is: %d pF\n\n",x); } else if(strcmp(f_res, "Nano") == 0) { float x = ((a*10)+b)*0.001; if(c == 0) { x = x; } else { while(c <= 5 && c > 0) { c--; x = x*10; } } printf("\nCapacitor value is: %.3f pF\n\n",x); } else { printf("\nINVALID Input!\n\n"); } printf("Do you want to enter other capacitor Code?( y-Yes, n-No): "); scanf(" %c", l_res); } return 0; } How can I fix this?
one of the output is showing (null) result on a simple struct program (no Array of Struct)
I've created a simple program for displaying college student data and counting the accumulation of score into a grade (A,B,C,D,E) and using struct (Im prohibited to create with Array of Struct), so the problem is one of the output which is "Grade" is giving (null) result when printed with %s and completely blank result when printed with %c. The type of the "Grade" data is char by the way. Here is the complete code. #include<stdio.h> #include<string.h> #include<stdlib.h> #include <ctype.h> struct student { char nim[11]; char name[100]; char subjectCode[5]; int sks; char grade; }studentScore[100]; bool cekKarakter(char input[]) { for(int x = 0; x < strlen(input); x++) { if(isdigit(input[x])) return false; } return true; } bool cekNumeric(char input[]) { for(int x = 0; x < strlen(input); x++) { if(input[x] >= 48 && input[x] <= 57) return false; } return true; } int main() { int n; printf("Input number of Student Data: "); scanf("%d", &n); fflush(stdin); printf("\n\n"); for(int i = 1; i <= n; i++) { do { printf("NIM [Hanya numerik][10 Digit]: "); gets(studentScore[i].nim); fflush(stdin); } while(strlen(studentScore[i].nim) != 10 || cekNumeric(studentScore[i].nim)); printf("\n"); do { printf("Name [Hanya karakter]: "); gets(studentScore[i].name); fflush(stdin); } while(strlen(studentScore[i].name) < 5 || strlen(studentScore[i].name) > 30 || cekKarakter(studentScore[i].name) == false); printf("\n"); do { printf("Subject Code [Must 5 length]: "); gets(studentScore[i].subjectCode); fflush(stdin); } while(strlen(studentScore[i].subjectCode) != 5); printf("\n"); do { printf("SKS [Min 2|Max 8]: "); scanf("%d", &studentScore[i].sks); fflush(stdin); } while(studentScore[i].sks < 2 || studentScore[i].sks > 8); printf("\n"); int score[5]; int WeightGrade = 0; printf("Input 5 College Subject Score:\n\n"); for(int z = 0; z < 5; z++) { do { printf("Input Score[%d][Must be between 0 and 100]: ", z + 1); scanf("%d", &score[z]); } while(score[z] < 0 || score[z] > 100); WeightGrade += score[z]; } if(WeightGrade / 25 == 4) { studentScore[i].grade = 'A'; } else if(WeightGrade / 25 >= 3 && WeightGrade / 25 < 4) { studentScore[i].grade = 'B'; } else if(WeightGrade / 25 >= 2 && WeightGrade / 25 < 3) { studentScore[i].grade = 'C'; } else if(WeightGrade / 25 >= 1 && WeightGrade / 25 < 2) { studentScore[i].grade = 'D'; } else if(WeightGrade / 25 == 0) { studentScore[i].grade = 'E'; } } printf("\nStudent Data\n"); for(int i = 1; i <= n; i++) { printf("NIM: %s\nName: %s\nSubject Code: %s\nSKS: %d\nGrade: %s\n", studentScore[i].nim, studentScore[i].name, studentScore[i].subjectCode, studentScore[i].sks, studentScore[i].grade); } getchar(); return 0; } This is the image of the program when it's running (null) output result using %s as printed
In your code, WeightGrade is actually the sum of all grades, so for your example - WeightGrade=370. 370/25 doesn't fall within any of the options, so studentScore[i].grade doesn't get any value. To prevents this cases, always input an else clause that shows an error Since you have 5 values, you should divide by 5*25=125, as in WeightGrade/125 Also for printf, you shouldn't use %s on char value, you should use %c
Error displaying in "Guess my number" game
I am learning C programming and now I am trying to code this program called "Guess my number" whereby player 1 will pick a number in a range, and then player 2 will guess the number. If the number of guesses exceed 10 tries, player 1 will win. However, the code program breaks after 10 tries and does not display the "Player 1 wins". Anybody can help me in this? Thanks. #include <stdio.h> #include <stdbool.h> #define boolean int main() { int enternumber = -1; int count = 0; int maxguesses = 10; int guessing; int i; bool currentguesses = false; while (1) { count += 1; printf("Player 1, enter a number between 1 and 1000\n"); scanf("%d", &enternumber); if (enternumber > 1000) { printf("That number is out of range\n"); } else { printf("That number is in the range\n"); break; } } printf("Player 2, you have %d tries remaining\n", maxguesses); for (i = 0; i < maxguesses; i++) { printf("Enter your guess\n"); scanf("%d", &guessing); if (enternumber == guessing) { printf("Player 2 wins.\n"); break; } else { printf("Too %s.\n", enternumber < guessing ? "high" : "low"); } } return 0; if (count == maxguesses) { printf("Player 1 wins"); } }
You should put your return 0 statement after checking if count == maxguesses. #include <stdio.h> #include <stdbool.h> #define boolean int main() { int enternumber = -1; int count = 0; int maxguesses = 10; int guessing; int i; bool currentguesses = false; while (1) { count += 1; printf("Player 1, enter a number between 1 and 1000\n"); scanf("%d", &enternumber); if (enternumber > 1000) { printf("That number is out of range\n"); } else { printf("That number is in the range\n"); break; } } printf("Player 2, you have %d tries remaining\n", maxguesses); for (i = 0; i < maxguesses; i++) { printf("Enter your guess\n"); scanf("%d", &guessing); if (enternumber == guessing) { printf("Player 2 wins.\n"); break; } else { printf("Too %s.\n", enternumber < guessing ? "high" : "low"); } } if (count == maxguesses) { printf("Player 1 wins"); } return 0; }
Why is my rand() function not working properly
So I am writing a gambling program for my c programming class. Essential its a menu for a dog track and you have to place a wager, select a dog, and the program chooses a winner and tells you if you won (how much) or lost. I use the ran() function but a specific dog that I set to win 10% of the time wins more than any other dog (including one that should win 40% of the time). /*Programmer: John S. Bolen*/ /*Date: 3/1/19*/ /* User Will Choose to gamble, View Results, Or Quit program. There are 9 Dogs to Chose from with a set %chance to win and a set Payout Rate */ #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include <time.h> #include <ctype.h> #define SIZE 1000 #define PAUSE system("pause") #define CLS system("cls") #define FLUSH nothingFlush() #define SIZE 1000 void nothingFlush() { char nothing; while (scanf("%c", ¬hing) == NULL); } typedef struct { char name[100]; float payout; float winOdds; } DOG; //Prototype Functions void displayAllRaces(DOG dog[], int winners[], int counter, int countOne, int countTwo, int countThree, int countFour, int countFive, int countSix, int countSeven, int countEight, int countNine); void displayMenu(); void getBet(float *bet); char getChoice(); float getOdds(); void pickDog(DOG dog[], int *counter, int winners[], float *bet, int *countOne, int *countTwo, int *countThree, int *countFour, int *countFive, int *countSix, int *countSeven, int *countEight, int *countNine); main() { char userChoice; DOG dog[SIZE]; int counter = 0; int countOne = 0, countTwo = 0, countThree = 0, countFour = 0, countFive = 0, countSix = 0, countSeven = 0, countEight = 0, countNine = 0; int winners[SIZE] = { 0 }; float bet = 0; do { userChoice = getChoice(); switch (userChoice) { case 'G': // Gamble getBet(&bet); pickDog(dog, &counter, winners, &bet, &countOne, &countTwo, &countThree, &countFour, &countFive, &countSix, &countSeven, &countEight, &countNine); break; case 'R': //Results displayAllRaces(dog, winners,counter, countOne, countTwo, countThree, countFour, countFive, countSix, countSeven, countEight, countNine); break; case 'L': //Leave printf("\n\tThank you for visiting the Dog Track\n"); break; default: printf("\n\tERROR! -- Enter A Valid Selection...\n"); break; }//End Switch PAUSE; } while (userChoice != 'L'); } void displayAllRaces(DOG dog[], int winners[], int counter, int countOne, int countTwo, int countThree, int countFour, int countFive, int countSix, int countSeven, int countEight, int countNine) { int i = 0; CLS; printf("\n\tRace Results: \n"); printf("\tDog\t\t\t\t\tNum of Wins\n"); if (counter > 0) { printf("\t%s\t\t\t%i\n", dog[0].name, countOne); printf("\t%s\t\t\t%i\n", dog[1].name, countTwo); printf("\t%s\t\t%i\n", dog[2].name, countThree); printf("\t%s\t\t%i\n", dog[3].name, countFour); printf("\t%s\t\t%i\n", dog[4].name, countFive); printf("\t%s\t\t%i\n", dog[5].name, countSix); printf("\t%s\t\t%i\n", dog[6].name, countSeven); printf("\t%s\t\t\t%i\n", dog[7].name, countEight); printf("\t%s\t\t\t%i\n", dog[8].name, countNine); } else { printf("\n\t\n"); } } void displayMenu(){ CLS; printf("\n\t==================================================\n"); printf("\n\t== MAIN MENU ==\n"); printf("\n\t==================================================\n"); printf("\n\t[G]amble\n"); printf("\n\t[R]esults\n"); printf("\n\t[L]eave\n"); printf("\n\tEnter Your Selection: "); } void getBet(float *bet) { float result = 0; printf("\n\tEnter the amount you want to gamble: "); printf("\n\t(Bet has to be between $1 and $1000)\n"); scanf_s("%f", &result); FLUSH; if(result < 1 || result > 1000){ printf("\n\tERROR-- Please enter a valid amount\n"); printf("\n\tEnter the amount you want to gamble: "); printf("\n\t(Bet has to be between $1 and $1000)\n"); scanf_s("%f", &result); FLUSH; } printf("\n\tThank you for placing your wager.\n\tGood luck!\n"); *bet = result; PAUSE; } char getChoice() { char result; displayMenu(); scanf_s("%c", &result); FLUSH; result = toupper(result); return result; } float getOdds() { float odds[9] = { 40, 10, 8, 6, 1, 4, 8, 10, 13 }; return odds[9]; } void pickDog(DOG dog[], int *counter, int winners[], float *bet, int *countOne, int *countTwo, int *countThree, int *countFour, int *countFive, int *countSix, int *countSeven, int *countEight, int *countNine) { int i = 0; int choice; float betMoney = 0; int winner = 0; CLS; printf("\n\tPick a dog from the list:"); strcpy(dog[0].name, "\n\t1.Gandalf the Great"); strcpy(dog[1].name, "\n\t2.Fenrir Greyback"); strcpy(dog[2].name, "\n\t3.Marley the Magnificent"); strcpy(dog[3].name, "\n\t4.Clifford the Big Red Dog"); strcpy(dog[4].name, "\n\t5.Petey the Little Rascal"); strcpy(dog[5].name, "\n\t6.Courage the Cowardly Dog"); strcpy(dog[6].name, "\n\t7.Old Yeller Before the Bullet"); strcpy(dog[7].name, "\n\t8.Pongo the Dalmatian"); strcpy(dog[8].name, "\n\t9.Nymeria Stark\n"); for (i = 0; i < 9; i++) { printf("%s", dog[i].name); } printf("\n"); scanf_s("%i", &choice); FLUSH; do { if (choice < 1 || choice > 9) { printf("\n\tPlease, try again...\n"); scanf_s("%i", &choice); } } while (choice < 1 || choice > 9); betMoney = *bet; winner = 1 + rand() % (100 - 1 + 1); if (winner <= 40) { winner = 1; (*countOne)++; } else if (winner <= 50) { winner = 2; (*countTwo)++; } else if (winner <= 48) { winner = 3; (*countThree)++; } else if (winner <= 64) { winner = 4; (*countFour)++; } else if (winner <= 65) { winner = 5; (*countFive)++; } else if (winner <= 69) { winner = 6; (*countSix)++; } else if (winner <= 77) { winner = 7; (*countSeven)++; } else if (winner <= 87) { winner = 8; (*countEight)++; } else if (winner <= 100) { winner = 9; (*countNine)++; } winners[*counter] = winner; (*counter)++; if (choice == winner) { printf("\n\tYou picked a WINNER!\n"); if (winner == 1) { (*bet) = betMoney * 2; printf("You win $%.2f\n", *bet); } if (winner == 2) { (*bet) = betMoney * 5; printf("You win $%.2f\n", *bet); } if (winner == 3) { (*bet) = betMoney * 10; printf("You win $%.2f\n", *bet); } if (winner == 4) { (*bet) = betMoney * 15; printf("You win $%.2f\n", *bet); } if (winner == 5) { (*bet) = betMoney * 50; printf("You win $%.2f\n", *bet); } if (winner == 6) { (*bet) = betMoney * 20; printf("You win $%.2f\n", *bet); } if (winner == 7) { (*bet) = betMoney * 10; printf("You win $%.2f\n", *bet); } if (winner == 8) { (*bet) = betMoney * 5; printf("You win $%.2f\n", *bet); } if (winner == 9) { (*bet) = betMoney * 3; printf("You win $%.2f\n", *bet); } } else { printf("\n\tYou picked a LOSER!\n"); (*bet) = (*bet) - betMoney; } } I have included the entire code for my program here (so you can test it if you wish), but only need help in fixing my rand() function. As you can see I want to randomly chose a number between 1-100. If the number is <= 40 then its dog one 40% chance to win, if the number is <= 50 (meaning its <= 50 but >40)then dog 2 wins with a 10% chance to win. I have run the program numerous times and dog 2 always seems to have the most wins after I run it x amount of times. Any help or suggestions would be greatly appreciated!