Ok, so I'm a computer science student (so I'm still learning) and I'm trying to program a drink maschine thingy. The thing is, it only executes the first if- block. I can't find my mistake and would really appreciate some help, so that I can fix the mistake and know what I did wrong.
I made some comments in the code, so it's better understandable for non German speakers, since the main text (which is mostly unimportant) is in german.
Here is the complete code for reverence
#include <stdlib.h>
#include <stdio.h>
void Fkt(int);
int main(void) {
int x; /*Not important in this part of code */
char G; /*The drink you choose will be G*/
float p; /* p = price */
printf("*** Getraenke Automat *** \n*W) Wasser (0.5 Euro)* \n*B) Bionade (1 Euro)*\n*O) Orangensaft (2 Euro)*\n***********************\n\nBitte ein Getränk auswaeheln "); /*Basically just asking what drink you want with the prices */
scanf("%c", &G); /* User picks drink*/
if ( G == 119 || 87) { /*This if block works perfectly fine */
printf("\n Bitte Zahlen Sie 0.5 Euro ");
scanf("%f", &p);
if ( p == 0.5) { /*If you pay the right amount of money */
printf("\n Entnehmen Sie Ihr Getränk \n");
}
else { /*For the wrong amount of money*/
printf("\nGeben Sie den richtigen Betrag ein!\n");
}
}
else if ( G == 66 || 98) { /* This one is stated as "Will never be executed */
printf("\n Bitte Zahlen Sie 1 Euro ");
scanf("%f", &p);
if ( p == 1) {
printf("\n Entnehmen Sie Ihr Getränk \n");
}
else {
printf("\nGeben Sie den richtigen Betrag ein!\n");
}
}
return 0;
}
void Fkt(int x){
printf("\n------------\n");
}
I tried to give every drink a different pice (Lemonade 1 Euro, Water 0.5 Cent) but no matter what input is given it always only gives me the price of the first drink.
I don't know if I made a small silly error or just did it completly wrong.
So here is my problem area:
if ( G == 119 || 87) { /*This if block works perfectly fine */
printf("\n Bitte Zahlen Sie 0.5 Euro ");
scanf("%f", &p);
if ( p == 0.5) { /*If you pay the right amount of money */
printf("\n Entnehmen Sie Ihr Getränk \n");
}
else { /*For the wrong amount of money*/
printf("\nGeben Sie den richtigen Betrag ein!\n");
}
}
else if ( G == 66 || 98) { /* This one is stated as "Will never be executed */
printf("\n Bitte Zahlen Sie 1 Euro ");
scanf("%f", &p);
if ( p == 1) {
printf("\n Entnehmen Sie Ihr Getränk \n");
}
else {
printf("\nGeben Sie den richtigen Betrag ein!\n");
}
I think you might want statements like if ( G == 66 || G == 98) instead of what you have. I don't think the code as written does what you think it does.
When you do G == 66 || 98 it will first evaluate G == 66 (which will evaluate to true or false) and then apply that value to 98 via the || operator, which will always return true (|| is usually applied to boolean values). It will not check whether G is 66 or 98...
The problem you have here is because you are saying
if ( G == 119 || 87)
Here, it tests if G is equal to 119, and if 87 is not 0.
The following code might help you understand how C works when using int as bool.
int a = 5;
if (a)
{
printf("Not zero\n");
} else {
printf("Zero\n");
}
a = 0;
if (a)
{
printf("Not zero\n");
} else {
printf("Zero\n");
}
The code above will print
Not zero
Zero
You should probably try
if ( G == 119 || G == 87) { /*This if block works perfectly fine */
printf("\n Bitte Zahlen Sie 0.5 Euro ");
scanf("%f", &p);
if ( p == 0.5) { /*If you pay the right amount of money */
printf("\n Entnehmen Sie Ihr Getränk \n");
}
else { /*For the wrong amount of money*/
printf("\nGeben Sie den richtigen Betrag ein!\n");
}
}
else if ( G == 66 || G == 98) { /* This one is stated as "Will never be executed */
printf("\n Bitte Zahlen Sie 1 Euro ");
scanf("%f", &p);
if ( p == 1) {
printf("\n Entnehmen Sie Ihr Getränk \n");
}
else {
printf("\nGeben Sie den richtigen Betrag ein!\n");
}
Related
This is my current programm in which i take 3 different (or the same) values of N Natural Numbers and calculate their checksum, the usage of long datatype is a must as i also need to be able to calculate the checksum of values that exceed the MAX of int. I cant change the datatype. Now I also need to catch when the user enters a a non natural numbers e.g -23, 2.3 ... Ive made if statements that catch if a negative number and a number that exceeds the MAX of long is entered, the actual problem is that when i enter decimal numbers it skips my if conditions and prints out the printf functions but not any of the other functions, ive tried catching the decimal number with x % 1 !=0 which does not work because the actual value of x doesnt seem to be stored as a decimal but rather a whole number , ive confirmed this in various places in my programm by printing the value of x.
Im really new to C barely 2 weeks into studying and havent really grasped everything, but i really just cant seem to find the problem in my Programm.
I know my code looks like spaghetti code.
P.S Excuse my awful english
#include <stdio.h>
#include <stdlib.h>
int checksum(long q){
long countingVariable = 0;
while(q > 0)
{
countingVariable += q%10; // steht für sx = s + q%10
q/=10; // steht für q = q/10
}
return countingVariable;
}
int main(void) {
long x,y,z;
long long max_long = 2147483647;
printf("Bitte geben sie ihre erste Zahl ein:\n"); // enter first number
scanf("%ld",&x);
long sx = quersumme_1(x); // checksum of x first number
if ( x > 0 && x < max_long && x % 1 != 0){ // check if positive and natural number
printf("Bitte geben Sie ihre zweite Zahl ein:\n"); //enter second number
scanf("%ld",&y);
if (y > 0 && y < max_long){
long sy = quersumme_2(y); //checksum of y second number
printf("Bitte geben sie ihre dritte Zahl ein:\n");// enter third number
scanf("%ld",&z);
if (z > 0 && z < max_long){
long sz = quersumme_3(z); // checksum of z 3rd number
if (sx>sy && sx>sz && sy>=sz){
printf("Die Quersumme s der Zahl %ld ist %ld\n",x,sx);
printf("Die Quersumme s der Zahl %ld ist %ld\n",y,sy);
printf("die Quersumme s der Zahl %ld ist %ld\n",z,sz);
} else if (sx>sy && sx>z && sz>sy){
printf("Die Quersumme s der Zahl %ld ist %ld\n",x,sx);
printf("die Quersumme s der Zahl %ld ist %ld\n",z,sz);
printf("Die Quersumme s der Zahl %ld ist %ld\n",y,sy);
} else if (sy>sx && sy>sz && sx>=sz){
printf("Die Quersumme s der Zahl %ld ist %ld\n",y,sy);
printf("Die Quersumme s der Zahl %ld ist %ld\n",x,sx);
printf("die Quersumme s der Zahl %ld ist %ld\n",z,sz);
} else if (sy>sx && sy>sz && sz>sx){
printf("Die Quersumme s der Zahl %ld ist %ld\n",y,sy);
printf("die Quersumme s der Zahl %ld ist %ld\n",z,sz);
printf("Die Quersumme s der Zahl %ld ist %ld\n",x,sx);
} else if(sz>sx && sz>sy && sx>=sy){
printf("die Quersumme s der Zahl %ld ist %ld\n",z,sz);
printf("Die Quersumme s der Zahl %ld ist %ld\n",x,sx);
printf("Die Quersumme s der Zahl %ld ist %ld\n",y,sy);
} else if(sz>sx && sz>sy && sy>sx){
printf("die Quersumme s der Zahl %ld ist %ld\n",z,sz);
printf("Die Quersumme s der Zahl %ld ist %ld\n",y,sy);
printf("Die Quersumme s der Zahl %ld ist %ld\n",x,sx);
} else if (sx==sy && sx==sz && sy==sz){
printf("Die Quersumme s der Zahl %ld ist %ld\n",x,sx);
printf("Die Quersumme s der Zahl %ld ist %ld\n",y,sy);
printf("die Quersumme s der Zahl %ld ist %ld\n",z,sz);
}
// the big if tree is just a sorting "algorithm" it sorts the values of checksums
}if (z < 0){ //check for negative number z
printf("Falsche Eingabe: Minus Zahl"); //error
exit(0);
}if (z > max_long){ // cant exceed Max value of long
printf("Falsche Eingabe: Zahl overflowed long");// error
exit(0);
}
}if (y < 0){ //check for negative number y
printf("Falsche Eingabe: Minus Zahl"); // error
exit(0);
}if (abs(y) > max_long){ // cant ewxceed max value of long
printf("Falsche Eingabe: Zahl overflowed long"); /error
exit(0);
}
}if (x < 0){ //check for negative numbers
printf("Falsche Eingabe: Minus Zahl"); // error
printf("%ld",x);
exit(0);
}
if (x > max_long){ // cant exceed max of long
printf("Falsche Eingabe: Zahl overflowed long"); // error
exit(0);
}
return 0;
}
The best is to use fgets() read the line of user input into a string and then parse the string.
Let us say you are stuck with scanf() (too bad).
First, check return value
// scanf("%ld",&y);
if (scanf("%ld",&y) != 1) {
printf("Non-numeric input, end-of-file or input error");
exit(0);
}
Now read the next character
unsigned char ch;
if (scanf("%c",&ch) != 1) {
printf("Failed to read next character");
exit(0);
}
if (!isspace(ch)) {
printf("Unexpected character following a number %d %c\n", ch, ch);
exit(0);
}
Now check for range
if (y < min_long || y > max_long) {
printf("Out of range %ld\n", y);
exit(0);
}
Simplifications exist.
Again better to use fgets().
A robust, but unchecked, example:
#define LINE_SZ 100 // Max expected line size
char buf[LINE_SZ * 2]; // Let us read even up to 2x expected
if (fgets(buf, sizeof buf, stdin) == NULL) {
printf("Nothing read\n", y);
exit(EXIT_FAILURE);
}
errno = 0;
char *endptr;
long y = strtol(buf, &endptr, 0);
if (buffer == endptr) {
printf("Non-numeric input \"%s\"\n", buf);
exit(EXIT_FAILURE);
}
if (errno || y < long_min || long_max) {
printf("Input \"%s\" outside [%ld %ld] range\n", buf, long_min, long_max);
exit(EXIT_FAILURE);
}
// Skip trailing white-space
while (isspace((unsigned char)*endptr)) {
endptr++;
}
if (*endptr) {
printf("Trailing junk in \"%s\"\n", buf);
exit(EXIT_FAILURE);
}
printf("Success %ld\n", y);
If you cannot use fgets() to read a line into a string, try
char buf[100];
if (scanf(" %99[^\n]", buf) == 1) {
// OK, now process the string
A long type can not store a decimal value, and scanf will always try to match what the user entered to the type of value you told it to expect.
In your case, %ld means that if user enters a decimal number, scanf will ignore the '.' and everything after it, and only put digits before '.' in y.
There is one way to deal with this in C:
Read a string from the user and check the characters in it to make sure they are all digits (or that there is no decimal point or whatever else you need to check).
Then you can use atol to convert the string to long.
In any case, you should check the value scanf returns.
This usually is not taught in beginner classes, but scanf returns how many items from the user it matched to your expectations and stored in the variables you provided.
In your case it should return 1 if a valid integer was entered.
This method won't tell you if the user entered a decimal number or a word instead of a natural number, but you will know if the input was valid or not.
so i've written some example code and was wondering why i get a multiple of the same outputs
when i printf my result after having unwanted inputs.
does anybody has an idea?
#include <stdio.h>
int eingabe, ergebnis;
int quer(int result)
{
result = eingabe*eingabe;
return result;
}
int check(void)
{
if (eingabe > 10000)
{
printf("\nDie eingegebene Zahl ist zu groß!\n\n");
main();
}
else if (eingabe < 0)
{
printf("\nDie eingegebene Zahl ist zu klein!\n\n");
main();
}
}
int main()
{
printf("Bitte geben Sie eine Zahl von 0-10000 ein: ");
scanf("%d", &eingabe);
check();
ergebnis = quer(eingabe);
printf("%d² = %d\n", eingabe, ergebnis);
}
an output example would look like this:
Bitte geben Sie eine Zahl von 0-10000 ein: -3
Die eingegebene Zahl ist zu klein!
Bitte geben Sie eine Zahl von 0-10000 ein: 11111
Die eingegebene Zahl ist zu groß!
Bitte geben Sie eine Zahl von 0-10000 ein: 3
3² = 9
3² = 9
3² = 9
Recursively calling main is not a good idea (it’s forbidden by the standard IIRC). In your case, however, it seems to work like a normal function: when you call it yourself, its end does not end the program, but returns control instead. So when main calls check that calls main, that inner instance returns, check returns and the printf is executed again.
Since you have called main in your check function, it justs keeps on going back and forth endlessly. It is never advisable to call main function
Thank you for all your help and suggestions!
i've tried to rewrite some of my code to implement them.
alltough i have one last question how id be possible to ask for input again after the check failed..
#include <stdio.h>
int eingabe, ergebnis;
int quer(int result)
{
result = eingabe*eingabe;
return result;
}
int check(void)
{
if (eingabe > 10000)
{
printf("mache falsch\n");
return 0;
} else if (eingabe < 0)
{
printf("mache falsch\n");
return 0;
} else
{
printf("mache richtig\n");
return 1;
}
}
int main()
{
int checked;
printf("Bitte geben Sie eine Zahl von 0-10000 ein: ");
scanf("%d", &eingabe);
checked = check();
if (checked == 1) {
ergebnis = quer(eingabe);
printf("%d² = %d\n", eingabe, ergebnis);
} else if (checked == 0){
printf("Zahl nicht im Wertebereich!\n");
return 0;
}
}
#include <stdio.h>
void aeins(){
int x;
unsigned int y;
double z;
printf("Geben sie einen ganze Zahl ein: ");
scanf("%d", &x);
printf("Geben sie eine natürliche Zahl ein: ");
scanf("%u", &y);
printf("Geben sie eine reelle Zahl ein: ");
scanf("%lf", &z);
printf("Die dritte Potenz von %d ist %d", x, x*x*x);
printf("Die dritte Potenz von %u ist %u", y, y*y*y);
printf("Die dritte Potenz von %lf ist %lf", z, z*z*z);
}
void azwei(){
printf("Geben sie einen Character ein: ");
char c = getchar();
printf("Das nachfolgende Zeichen lautet: %c und der ASCII-Wert ist: ", c+1, c+1);
}
int main (void){
int a;
int b = 1;
while(b){
printf("Welche Aufgabe soll gezeigt werden? ");
printf("\n(1) Aufgabe 1 \n(2) Aufgabe 2\n");
scanf("%d", &a);
switch(a){
case 1: aeins();
b = 0; break;
case 2: azwei();
b = 0; break;
default: printf("Falsche Eingabe!\n"); break;
}
}
}
This is my Program and this is my output:
Welche Aufgabe soll gezeigt werden?
(1) Aufgabe 1
(2) Aufgabe 2
2
Geben sie einen Character ein: Das nachfolgende Zeichen lautet: und der ASCII-Wert ist:
Process returned 0 (0x0) execution time : 2.172 s
Press any key to continue.
As you can see my program is ignoring the getchar command. I have tried it with a scanf command, but it won't work too. In the function aeins works everything. I would say I am a beginner to intermediate c programmer if this helps you.
scanf has the interesting property that it leaves the newline character which terminated input in the input buffer, so you need to consume that before proceeding to any other input.
Just add a getchar() call after your scanf("%d", &a); and you should be good.
You can fix issue by using scanf(" %c",&c); or you need to write 1 more getchar() to consume newline character left from scanf("%d", &a);
Question:
The program should have menu to allow the user perform the following functions:
semester subject planning
entering of subjects' grade
display of subjects' information for the semester
The program should only terminate when the user select to quit the program.
Please help me solve this problem.
When I run this C programming code it will appear this error:
Error 16 error C2143: syntax error : missing ';' before 'type' g:\mini project testing\cgpacalculation\cgpacalculation\cgpacalculation4.c 190
Error 17 error C2143: syntax error : missing ';' before 'type' g:\mini project testing\cgpacalculation\cgpacalculation\cgpacalculation4.c 224
Please help me correct my coding:
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <Windows.h>
//declaration
void userdetail(void);
void mainmenu(void);
void menu(void);
void menu_two(void);
void lines(void);
void getSubject(void);
void getCalculation(void);
void about(void);
float gradesToGP(char grades);
//display
void main()
{
mainmenu();
}
//definition
void mainmenu(void)
{
int select;
lines();
printf("\t\t\t CGPA Calculation \n");
lines();
printf("Enter \"1\" - Student Detail and Subject Information\n");
printf("Enter \"2\" - About\n");
printf("Enter \"0\" - Exit\n");
lines();
printf("Enter your choice :");
scanf("%d", &select);
lines();
if (select == 1)
{
userdetail();
getSubject();
}
else if (select == 2)
{
about();
getch();
menu();
}
else if (select == 0)
{
system("cls");
printf("\t\t THANK YOU FOR USING THIS PROGRAM =D \n");
getch();
}
else
{
printf("\a\a WRONG INPUT! \n");
lines();
getch();
system("cls");
mainmenu();
}
}
void menu(void)
{
int choice;
printf("Enter \"1\" - Back to Main Menu\n");
printf("Enter \"0\" - Exit\n");
lines();
printf("Enter your choice :");
scanf("%d", &choice);
if (choice == 1)
{
system ("cls");
mainmenu();
}
else if (choice == 0)
{
system("cls");
printf("\t\t THANK YOU FOR USING THIS PROGRAM =D \n");
getch();
}
else
{
printf("\a\a WRONG INPUT! \n");
getch();
system("cls");
menu();
}
}
void lines(void)
{
printf("**********************************************************************");
}
void userdetail(void)
{
char name[100][100], id[100][10];
printf("Enter your full name :\n");
scanf("%s", &name);
printf("Enter your student ID :\n");
scanf("%s", &id);
}
void getSubject(void)
{
int loop,numSubject, credit[100];
float GP[100], TargetCGPA[100];
char SubjectCode[100][40], grade[100];
char name[100][100];
char id[100][40];
int sumCredit = 0;
double sumCreditxGP = 0;
system("cls");
lines();
printf("Enter total subject :");
scanf("%d", &numSubject);
lines();
for (loop = 0; loop <= numSubject-1; loop++)
{
printf("Subject %d \n", loop+1);
printf("Enter subject code :");
scanf("%s", &SubjectCode[loop]);
printf("Credit hour :");
scanf("%d", &credit[loop]);
printf("Enter your grade :");
scanf("%s", &grade[loop]);
GP[loop] = gradesToGP(grade[loop]);
lines();
}
printf("Enter your targeted CGPA for this semester :");
scanf("%f", &TargetCGPA);
lines();
printf("Press \" ENTER \" or any button");
getch();
system("cls");
menu_two();
void menu_two(void);
{
int choice;
printf("Enter \"1\" - CGPA Calculation\n");
printf("Enter \"0\" - Exit\n");
lines();
printf("Enter your choice :");
scanf("%d", &choice);
if (choice == 1)
{
system ("cls");
getCalculation();
}
else if (choice == 0)
{
system("cls");
printf("\t\t THANK YOU FOR USING THIS PROGRAM =D \n");
getch();
}
else
{
printf("\a\a WRONG INPUT! \n");
getch();
system("cls");
menu_two();
}
}
void getCalculation(void);
{
system("cls");
lines();
printf("Student Name : %c\n", name);
printf("Student ID : %c\n", id);
lines();
printf("No. Subject Code Credit Hour Grade Grade Point");
lines();
for (loop = 0; loop <= numSubject-1; loop++)
{
printf("\n%d %s\t %d\t%s\t%.2f\n", loop+1, SubjectCode[loop], credit[loop], grade[loop], GP[loop]);
}
for (loop = 0; loop <= numSubject-1; loop++)
{
sumCredit = sumCredit + credit[loop];
sumCreditxGP = sumCreditxGP + credit[loop] * GP[loop];
}
lines();
printf("Total credit hour is = %d\n\n", sumCredit);
printf("Total credit hour X grade point is = %.2f\n\n", sumCreditxGP);
printf("CGPA is = %.2f", sumCreditxGP / sumCredit);
lines();
getch();
menu();
}
}
void about(void)
{
system("cls");
lines();
printf("\n\t\t\tMini Project\n");
lines();
printf("Develop by: Shah Rezza Bin Jasni\n");
printf("Institution: Universiti Tenaga Nasional\n\n\n");
lines();
printf("COPYRIGHT 2014");
lines();
}
float gradesToGP(char grades)
{
if (grades == 'A+')
{
return(float)4.00;
}
else if (grades == 'A')
{
return(float)4.00;
}
else if (grades == 'A-')
{
return(float)3.67;
}
else if (grades == 'B+')
{
return(float)3.33;
}
else if (grades == 'B')
{
return(float)3.00;
}
else if (grades == 'B-')
{
return(float)2.67;
}
else if (grades == 'C+')
{
return(float)2.33;
}
else if (grades == 'C')
{
return(float)2.00;
}
else if (grades == 'C-')
{
return(float)1.67;
}
else if (grades == 'D+')
{
return(float)1.33;
}
else if (grades == 'D')
{
return(float)1.00;
}
else if (grades == 'E')
{
return(float)0.00;
}
else
{
return(float)0.00;
}
}
It looks like you put the function menu_two inside another function. Your Visual C++ compiler doesn't accept local functions.
Same problem with getCalculation. According to your declarations, those should be in global scope.
You have few problems with your code-
menu_two();
} // here you need to close it
void menu_two(void) // remove the semicolon here
Because Local functions are not acceptable. Don't include any function inside another function.
void getCalculation(void) // remove semicolon here
Make the above two functions as a Global function. any try to pass the necessary information to that functions and try.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am also having the "undefined reference to ERPSGame()" error as well as the "Id returned 1 exit status". Did I declare something wrong? I am using Orwell Dev C++.
What seems to be the problem? Is it my compiler ?
Check out the whole code:
/* GameBox BETA v0.2 */
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
void ERPS();
void Menu();
void BJack();
void Log();
int ERPSGame();
void ERPSInstruct();
int main() {
char Choice;
system("cls");
printf("\t\t Welcome to Mark and Leroy's GameBox BETA v0.2 \n\n\n");
printf("--------------------------------------------------------------------------------\n");
printf("\t _________________________________________________________\n");
printf("\t ||_____________________________________________________|| \n");
printf("\t || \t\t|| \n");
printf("\t || \t\t|| \n");
printf("\t || \t\t|| \n");
printf("\t ||\t Please press 'A' to continue to the menu\t||\n");
printf("\t ||\t \t||\n");
printf("\t ||\t Otherwise, press 'F' to exit the program\t||\n");
printf("\t ||_____________________________________________________|| \n");
printf("\t ||_____________________________________________________||\n");
printf("--------------------------------------------------------------------------------\n\n");
/* Drawing Starts Here */
printf("\t\t ========================================\n");
printf("\t\t || || \n");
printf("\t\t || ||=============== || \n");
printf("\t\t || || || \n");
printf("\t\t || || ======= || \n");
printf("\t\t || || Gamebox || || \n");
printf("\t\t || || BETA v0.2 || || \n");
printf("\t\t || ||==============|| || \n");
printf("\t\t || || \n");
printf("\t\t || ||==== ||====|| == == || \n");
printf("\t\t || || || || || == == || \n");
printf("\t\t || ||===|| || || == || \n");
printf("\t\t || ||___|| ||====|| == == || \n");
printf("\t\t || == == || \n");
printf("\t\t || Artwork by: Mark Sanchez || \n");
printf("\t\t ========================================\n");
/* Drawing Ends Here */
scanf("%c", &Choice);
switch (Choice) {
case'A':case'a':{
Menu();
break;
}
case'F':case'f':{
exit(0);
}
default: {
main();
}
}
return 0;
}
void Menu() {
char Choice1;
system("cls");
printf("\t\t Welcome to Mark and Leroy's GameBox BETA v0.2 \n\n\n");
printf("--------------------------------------------------------------------------------\n\n");
printf("\t\t Press 'R' to play the Enhanced Rock-Paper-Scissors game\n\n\n");
printf("\t\t Press 'B'to play Blackjack\n\n\n");
printf("\t\t Press 'C' to view the changelog\n\n\n");
printf("--------------------------------------------------------------------------------\n\n");
scanf("%c", &Choice1);
switch(Choice1) {
case'R':case'r': {
ERPS();
break;
}
case'B':case'b': {
BJack();
break;
}
case'C':case'c': {
Log();
break;
}
default: {
Menu();
}
}
}
void ERPS() {
system("cls");
char Instruct1;
char Instruct2;
/* Menu of ERPS */
printf("\t\t\t Welcome to ENHANCED Rock Paper Scissors\n\n\n");
printf("\t\t\t Press A to start the game\n\n");
printf("\t\t\t Press S to view the instructions\n\n");
printf("\t\t\t Press 'P' to return to the main menu\n");
scanf("%c", &Instruct1);
scanf("%c", &Instruct2);
switch(Instruct2) {
case'A':case'a': {
ERPSGame();
break;
}
case'S':case's': {
ERPSInstruct();
break;
}
case'P':case'p': {
Menu();
break;
}
default:{
ERPS();
}
}
}
int rand_i(int n)
{
int rand_max = RAND_MAX - (RAND_MAX % n);
int ret;
while ((ret = rand()) >= rand_max);
return ret/(rand_max / n);
}
int weighed_rand(int *tbl, int len)
{
int i, sum, r;
for (i = 0, sum = 0; i < len; sum += tbl[i++]);
if (!sum) return rand_i(len);
r = rand_i(sum) + 1;
for (i = 0; i < len && (r -= tbl[i]) > 0; i++);
return i;
}
int ERPSGame(int argc, const char *argv[]) /* THIS IS THE ERPSGame */
{
char umove[10], cmove[10], line[255];
int user, comp;
int tbl[]={0,0,0};
int tbllen=3;
printf("Hello, Welcome to rock-paper-scissors\nBy The Elite Noob\n");
mainloop:
while(1)
{ // infinite loop :)
printf("\n\nPlease type in 1 for Rock, 2 For Paper, 3 for Scissors, 4 to quit\n");
srand(time(NULL));
comp = (weighed_rand(tbl, tbllen) + 1) % 3;
fgets(line, sizeof(line), stdin);
while(sscanf(line, "%d", &user) != 1) //1 match of defined specifier on input line
{
printf("You have not entered an integer.\n");
fgets(line, sizeof(line), stdin);
}
if( (user > 4) || (user < 1) )
{
printf("Please enter a valid number!\n");
continue;
}
switch (comp)
{
case 1 :
strcpy(cmove, "Rock");
break;
case 2 :
strcpy(cmove, "Paper");
break;
case 3 :
strcpy(cmove, "Scissors");
break;
default :
printf("Computer Error, set comp=1\n");
comp=1;
strcpy(cmove, "Rock");
break;
}
switch (user)
{
case 1 :
strcpy(umove, "Rock");
break;
case 2 :
strcpy(umove, "Paper");
break;
case 3 :
strcpy(umove, "Scissors");
break;
case 4 :
printf("Goodbye! Thanks for playing!\n");
return 0;
default :
printf("Error, user number not between 1-4 exiting...");
goto mainloop;
}
if( (user+1)%3 == comp )
{
printf("Comp Played: %s\nYou Played: %s\nSorry, You Lost!\n", cmove, umove);
}
else if(comp == user)
{
printf("Comp Played: %s\nYou Played: %s\nYou Tied :p\n", cmove, umove);
}
else
{
printf("Comp Played: %s\nYou Played: %s\nYay, You Won!\n", cmove, umove);
}
tbl[user-1]++;
}
}
void ERPSInstruct() {
system("cls");
char Instruct3;
printf("\t\t\t Instructions of the Game \n\n");
printf("\t\t\t There will be 5 choices to choose from\n");
printf("\t\t\t 1. Rock\n");
printf("\t\t\t 2. Paper\n");
printf("\t\t\t 3. Scissors\n");
printf("\t\t\t 4. Lizard\n");
printf("\t\t\t 5. Spock\n\n\n\n");
printf("\t\t This is the mechanism of the choices: \n\n");
printf("\t\t Scissor cuts paper. \n\n");
printf("\t\t Paper covers rock. \n\n");
printf("\t\t Rock crushes lizard. \n\n");
printf("\t\t Lizard poisons spock. \n\n");
printf("\t\t Spock smashes scissors. \n\n");
printf("\t\t Scissors decapitate lizard. \n\n");
printf("\t\t Lizard eats paper. \n\n");
printf("\t\t Paper disproves spock. \n\n");
printf("\t\t Spock vaporizes rock. \n\n");
printf("\t\t Rock crushes scissors. \n\n");
printf("\t\t\t Press 'P' to return to the menu\n");
scanf("%c", &Instruct3);
switch(Instruct3) {
case'P':case'p': {
Menu();
break;
}
default: {
ERPSInstruct();
}
}
}
void BJack() {
system("cls");
char Instruct2;
printf("BLACKJack \n\n\n");
printf("\t\t\t Press 'P' to return to the menu\n");
scanf("%c", &Instruct2);
switch(Instruct2) {
case'P':case'p': {
Menu();
break;
}
default: {
BJack();
break;
}
}
getch();
}
void Log() {
system("cls");
char Instruct3;
printf("CHANGELOG: \n\n\n");
printf(" 0.1 -- Fixed a bug wherein the program forces\n to do 'default' upon choosing a game\n\n\n");
printf("-----------------------------------------------\n\n");
printf(" 0.2 -- Fixed display in CMD: Added a few dividers \nas well as correcting a few grammatical mistakes\n");
printf(" Added awesome graphics \n\n\n");
printf("-----------------------------------------------\n\n");
printf("\t\t\t Press 'P' to return to the menu\n");
scanf("%c", &Instruct3);
switch(Instruct3) {
case'P':case'p': {
Menu();
break;
}
default: {
Log();
break;
}
}
}
Is it my way of declaring ERPSGame? I used int.
Function declaration is not matched with function definition.
Declaration:
int ERPSGame();
Definition
int ERPSGame(int argc, const char *argv[])
In Dev-C++, under Tools -> Compiler Options, put "-Wall" in the "...commands when calling compiler" box. It will be helpful for you to get warnings and do effective coding and saves your time too.
When you take in instruct1 and then use instruct2 in the switch...
scanf("%c", &Instruct1);
scanf("%c", &Instruct2);
switch(Instruct2) {
case'A':case'a': {
ERPSGame();
This could be giving you an odd value given the fact that users response is in instruct1. Potentially.