Unresolved external symbol in c for user-define function - c

im very new to programming and for my course we learning c language so have mercy on my badly written code and the compiler just wont run the program because of the error so its hard for me to identify whats wrong.Back to main question im getting unresolved external symbol...referenced in function_main and i dont really know where im messed up
#include<stdio.h>
char DisplayMenu(void);
void ViewBooks();
void SearchBookPrice();
void UpdateBookPrice();
int main()
{
char userchoice;
int bookID[5] = { 200,230,500,540,700 };
char bookTitle[5][50] = { {"Using Information Technology 12th Edition" }, { "Beyond HTML" },{"Build Your Own PC"},{"Instant Java Servlets"},{"DIgital Image: A Practical Guide"} };
float bookPrice[5] = { 100.30,50.40,47,83.30,22.90 };
do
{
userchoice = DisplayMenu();
if (userchoice == 'V')
ViewBooks();
else if (userchoice == 'S')
SearchBookPrice();
else if (userchoice == 'U')
UpdateBookPrice();
} while (userchoice != 'E');
printf("Thank you for using our system. Have a nice day!\n");
return 0;
}
char DisplayMenu(void)
{
char choice;
printf("*************************************************************************\n");
printf("V:View Books S:Search Book Price U:Update Book Price E:Exit");
printf("*************************************************************************\n");
do
{
printf("Enter your choice: ");
scanf(" %c", &choice);
if (choice != 'V' && choice != 'S' && choice != 'U' && choice != 'E')
printf("Invalid Choice\n");
} while (choice != 'V' && choice != 'S' && choice != 'U' && choice != 'E');
return choice;
}
void ViewBooks(int bookID[],float bookPrice[])
{
printf("%d Using Information Technology 12th Edition $%f\n", bookID[0], bookPrice[0]);
printf("%d Beyond HTML $%f\n", bookID[1], bookPrice[1]);
printf("%d Build Your Own PC $%f\n", bookID[2], bookPrice[2]);
printf("%d Instant Java Servlets $%f\n", bookID[3], bookPrice[3]);
printf("%d Digital Image: A Pratical Guide $%f\n", bookID[4], bookPrice[4]);
return;
}
void SearchBookPrice(int bookID[5],char bookTitle[5][50], float bookPrice[5])
{
int idsearch, i= 0, match = -1;
printf("*************************************************************************\n");
printf("Search by book ID\n");
printf("*************************************************************************\n");
printf("Enter book ID: ");
scanf("%d", &idsearch);
while (i<5 && match==-1)
{
if (bookID[i] == idsearch)
match = i;
i++;
}
if (match == -1)
printf("Please refer to the customer service for assitance");
else
{
printf("The book id is : %d\n", &idsearch);
printf("The price of %s is %f", bookTitle[match], bookPrice[match]);
}
return;
}
void UpdateBookPrice(int bookID[5], char bookTitle[5][50], float bookPrice[5])
{
int idsearch, i = 0, match = -1;
printf("Enter book ID: ");
scanf("%d", &idsearch);
while (i < 5 && match == -1)
{
if (bookID[i] == idsearch)
match = i;
i++;
}
if (match == -1)
printf("The book ID is not found. Please make sure the book ID is correct");
else
{
printf("Current book price is %f\n", bookPrice[match]);
printf("Enter new price for (%d-%s): ", bookID[match], bookTitle[match]);
scanf("%f", &bookPrice[match]);
}
return;
}

Your function declaration is not matched with function body.
This is working code.
#include<stdio.h>
char DisplayMenu(void);
void ViewBooks(int bookID[],float bookPrice[]);
void SearchBookPrice(int bookID[5],char bookTitle[5][50], float bookPrice[5]);
void UpdateBookPrice(int bookID[5], char bookTitle[5][50], float bookPrice[5]);
int main()
{
char userchoice;
int bookID[5] = { 200,230,500,540,700 };
char bookTitle[5][50] = { {"Using Information Technology 12th Edition" }, { "Beyond HTML" },{"Build Your Own PC"},{"Instant Java Servlets"},{"DIgital Image: A Practical Guide"} };
float bookPrice[5] = { 100.30,50.40,47,83.30,22.90 };
do
{
userchoice = DisplayMenu();
if (userchoice == 'V')
ViewBooks(bookID, bookPrice);
else if (userchoice == 'S')
SearchBookPrice(bookID, bookTitle, bookPrice);
else if (userchoice == 'U')
UpdateBookPrice(bookID, bookTitle, bookPrice);
} while (userchoice != 'E');
printf("Thank you for using our system. Have a nice day!\n");
return 0;
}
char DisplayMenu(void)
{
char choice;
printf("*************************************************************************\n");
printf("V:View Books S:Search Book Price U:Update Book Price E:Exit");
printf("*************************************************************************\n");
do
{
printf("Enter your choice: ");
scanf(" %c", &choice);
if (choice != 'V' && choice != 'S' && choice != 'U' && choice != 'E')
printf("Invalid Choice\n");
} while (choice != 'V' && choice != 'S' && choice != 'U' && choice != 'E');
return choice;
}
void ViewBooks(int bookID[],float bookPrice[])
{
printf("%d Using Information Technology 12th Edition $%f\n", bookID[0], bookPrice[0]);
printf("%d Beyond HTML $%f\n", bookID[1], bookPrice[1]);
printf("%d Build Your Own PC $%f\n", bookID[2], bookPrice[2]);
printf("%d Instant Java Servlets $%f\n", bookID[3], bookPrice[3]);
printf("%d Digital Image: A Pratical Guide $%f\n", bookID[4], bookPrice[4]);
return;
}
void SearchBookPrice(int bookID[5],char bookTitle[5][50], float bookPrice[5])
{
int idsearch, i= 0, match = -1;
printf("*************************************************************************\n");
printf("Search by book ID\n");
printf("*************************************************************************\n");
printf("Enter book ID: ");
scanf("%d", &idsearch);
while (i<5 && match==-1)
{
if (bookID[i] == idsearch)
match = i;
i++;
}
if (match == -1)
printf("Please refer to the customer service for assitance");
else
{
printf("The book id is : %d\n", &idsearch);
printf("The price of %s is %f", bookTitle[match], bookPrice[match]);
}
return;
}
void UpdateBookPrice(int bookID[5], char bookTitle[5][50], float bookPrice[5])
{
int idsearch, i = 0, match = -1;
printf("Enter book ID: ");
scanf("%d", &idsearch);
while (i < 5 && match == -1)
{
if (bookID[i] == idsearch)
match = i;
i++;
}
if (match == -1)
printf("The book ID is not found. Please make sure the book ID is correct");
else
{
printf("Current book price is %f\n", bookPrice[match]);
printf("Enter new price for (%d-%s): ", bookID[match], bookTitle[match]);
scanf("%f", &bookPrice[match]);
}
return;
}

Related

Need to get the output only when the loop ends

Here, i want to get the output only after the user enters the letter 'Q', which is the last task of my program. However, when i enter 'R' which is another case for my program, it directly prints the output for me.
int main() {
float amountOfTurkishLira = 0;
float amountOfBtc = 0;
float amountOfEth = 0;
char operationCode;
char operationTurkishLira;
// getting the amount of money for the initial money;
scanf(" %c", &operationTurkishLira);
if (operationTurkishLira != 'T') {
printf("Error: Operation could not be completed.");
} else {
scanf(" %f", &amountOfTurkishLira);
scanf(" %c", &operationCode);
while (operationCode != 'Q') {
switch (operationCode) {
case 'R':
if (amountOfBtc == 0 && amountOfEth == 0) {
printf("Our account holds %.2f TRY", amountOfTurkishLira);
} else {
printf("Our account holds %.2f TRY | %.2f BTC | %.2F ETH.", amountOfTurkishLira, amountOfBtc,
amountOfEth);
}
scanf(" %c", &operationCode);
break;
}
}
printf("Bye..");
}
return 0;
}
This happens when i enter the input:
T
1000
R
Our account holds 1000.00 TRY
Bye...
But I want it to be like this:
T
1000
R
Q
after i enter all my input, it should give me
Our account holds 1000.00 TRY
Bye...
Take the printing code out of the loop and do it at the end.
And take scanf(" %c", &operationCode); out of the switch, since it should be done no matter what the previous code was.
int main() {
float amountOfTurkishLira = 0;
float amountOfBtc = 0;
float amountOfEth = 0;
char operationCode;
char operationTurkishLira;
// getting the amount of money for the initial money;
scanf(" %c", &operationTurkishLira);
if (operationTurkishLira != 'T') {
printf("Error: Operation could not be completed.");
} else {
scanf(" %f", &amountOfTurkishLira);
scanf(" %c", &operationCode);
while (operationCode != 'Q') {
switch (operationCode) {
case 'R':
break;
}
scanf(" %c", &operationCode);
}
if (amountOfBtc == 0 && amountOfEth == 0) {
printf("Our account holds %.2f TRY", amountOfTurkishLira);
} else {
printf("Our account holds %.2f TRY | %.2f BTC | %.2F ETH.", amountOfTurkishLira, amountOfBtc,
amountOfEth);
}
printf("Bye..");
}
return 0;
}
Take a look on my version. I have some comments in it. You just have to add your logic for the case you pressing 'R' or an other key (I suppose you will add some more key press listeners).
#include <stdio.h>
int main() {
float amountOfTurkishLira = 0;
float amountOfBtc = 0;
float amountOfEth = 0;
char operationCode;
char operationTurkishLira;
// getting the amount of money for the initial money;
scanf(" %c", &operationTurkishLira);
if (operationTurkishLira != 'T') {
printf("Error: Operation could not be completed.");
}
else {
scanf(" %f", &amountOfTurkishLira);
scanf(" %c", &operationCode);
while (operationCode != 'Q') {
switch (operationCode) {
case 'R':
if (amountOfBtc == 0 && amountOfEth == 0) {
printf("LOGIC FOR amountOfBtc == 0 && amountOfEth == 0\n");
} else {
printf("LOGIC FOR amountOfBtc != 0 || amountOfEth != 0\n");
}
break;
case 'X': // X, Y, or WHATEVER
// LOGIC FOR 'X'
break;
}
scanf(" %c", &operationCode);
}
if (operationCode == 'Q') {
if (amountOfBtc == 0 && amountOfEth == 0) {
printf("Our account holds %.2f TRY\n", amountOfTurkishLira);
}
else {
printf("Our account holds %.2f TRY | %.2f BTC | %.2F ETH.\n", amountOfTurkishLira, amountOfBtc, amountOfEth);
}
printf("Bye..");
}
}
return 0;
}

I don't understand or find the errors in my code [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am making a tic tac toe game in C language but it won't run
I don't know where is the error at. The compiler just freeze without any error messages this is my code below if anyone could point out the errors and the way of writing clean code i am a new programmer who is still learning and this is my first project.
#include <stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<time.h>
void BuildGround;
void first_player_choice;
void second_player_choice;
void ai_choice;
char result;
void AiVsP;
void PVP;
int count_ground;
int main()
{
char ground[9] = {' ',' ',' ',' ',' ',' ',' ',' ',' '};
int mode;
printf("choose game mode (1 or 2): ");
printf("1. PVP ");
printf("2. AI vs P: ");
scanf("%d",&mode);
switch(mode){
case 1 :
PVP();
break;
case 2:
AiVsP();
break;
default:
printf("Invalid input: ");
break;
}
}
int count_ground(char symbole){
int total=0;
for(int i =0 ; i<9 ; i++){
if(ground[i]== symbole)
total=+ 1;
}
}
void AiVsP(void){
char name[100];
printf("Enter your name : ");
gets(name);
while(1==1){
system("cls");
BuildGround();
if (count_ground('X') == count_ground('O')){
puts("Turn: ");
puts(name);
fisrt_player_choice();
}
else{
ai_choice;
}
char winner == result();
if (winner == 'X'){
system("cls");
BuildGround();
puts("The winner is: ");
puts(name);
break;
}
else if
(winner == 'O'){
system("cls");
BuildGround();
printf("AI wins ")
break;
}
else winner == 'D'
printf("The Game is Draw ")
break;
}
}
void PVP(void){
char name1[100];
char name2[100];
printf("Enter the name of the first player : ");
gets(name1);
printf("Enter the name of the second player : ");
gets(name2);
while(1==1){
system("cls");
BuildGround;
if (count_ground('X') == count_ground('O')){
puts("Turn: ");
puts(name1);
first_player_choice();
}
else{
puts("Turn: ");
puts(name2);
second_player_choice();
}
char winner == result();
if (winner == 'X'){
system("cls");
BuildGround();
puts("The winner is: ");
puts(name1);
break;
}
else if
(winner == 'O'){
system("cls");
BuildGround();
puts("The winner is: ");
puts(name2);
break;
}
else winner == 'D'
printf("The Game is Draw ")
break;
}
}
char result(char ground[9]){
int i;
for(i = 0;i<=6; i+=3)//it's for row
if(ground[i] == ground[i+1]== && ground[i+1]== ground[i+2]&& ground[i] != ' ' ){
return ground[0];
}
for(i = 0;i<3; i++)//it's for column
if(ground[i]== ground[i+3]== && ground[i+3]== ground[i+6] && ground[i]!=' ' ){
return ground[1];
}
if(ground[0] == && ground[4] == && ground[4]== ground[8] && ground[0]!=' '){
return ground[4];
}
else if(ground[2] == && ground[4] == && ground[4]== ground[6] && ground[2]!=' '){
return ground[6];
}
else if(count_ground('X')+ count_ground('O') < 9){
return 'C';
}else return 'D';
}
void ai_choice(void){
srand(time(0));
do{
int c;
c = rand()%10;
}
while(ground[c]!= ' ');
ground(c)='O';
}
void first_player_choice(char ground[9]){
while(1==1){
int position;
printf("Choose your position from 0-8 (column wise): ");
scanf("%d", &position);
if(position < 0 || position > 8 ){
printf("Invalid number please try again: ");
}
else if(ground[position]!=' '){
printf("Position is already taken please try again: ");
}
else
ground[c]='X';
break;
}
}
void second_player_choice(char ground[9]){
while(1==1){
int position;
printf("Choose your position from 0-8 (column wise): ");
scanf("%d", &position);
if(position < 0 || position > 8 ){
printf("Invalid number please try again: ");
}
else if(ground[position]!=' '){
printf("Position is already taken please try again: ");
}
else
ground[c]='O';
break;
}
}
void BuildGround(char ground[9]){
printf("\t\t\t\tT i c t a c t o e");
printf("\nPlayers 1 Symbol: X");
printf("\nPlayers 2 Symbol: O");
printf("\n\t\t\t | | ");
printf("\n\t\t\t %c | %c | %c ",ground[0],ground[1],ground[2]);
printf("\n\t\t\t_______|_______|_______");
printf("\n\t\t\t %c | %c | %c ",ground[3],ground[4],ground[5]);
printf("\n\t\t\t_______|_______|_______");
printf("\n\t\t\t %c | %c | %c ",ground[6],ground[7],ground[8]);
printf("\n\t\t\t | | ");
}
You have multiple issues, like
case 1 :
PVP; //this is not a function call.
break;
should use the call like
PVP();
or rather, pvp(); , as uppercase letters usually carry special meaning, not a good idea to use them for user-defined code.
That said, function definitions like
void ai_choice{
srand(time(0));
and
void PVP{
char name1[100];
char name2[100];
are wrong syntax, you have to use the parenthesis, like
void PVP (void){
char name1[100];
char name2[100];
That said, scanf("%d",& mode); is usually written as scanf("%d",&mode); (no space between operand and operator, but that's just a choice).

How to keep inputing the frequency and duration till 100 values only and retreive them

The user can only enter a 100 values maximum for both the duration and frequency, so how do we have the user keep entering them continuously and retrieve them from the arrays, when they wish to play the song? I'm not sure about my code below
#include<stdio.h>
#include<stdlib.h>
#include<Windows.h>
int main(void)
{
int i = 0;
char menu = 0, a, A, b, B, c, C;
int duration[100];
float frequency[100];
if (menu != 'c' || menu != 'C')
{
while (menu != 'c' || menu != 'C')
{
printf("a. Build the song\n");
printf("b. Play the song\n");
printf("c. Quit\n");
printf("Please enter an option: ");
scanf(" %c", &menu);
if (menu == 'A' || menu == 'a')
{
printf("Please enter a frequency: ");
scanf("%f", &frequency[i] + 1);
printf("Please enter a duration: ");
scanf("%d", &duration[i] + 1);
system("cls");
}
else if (menu == 'b' || menu == 'B')
{
Beep(frequency[i], duration[i]);
system("cls");
}
else if (menu == 'c' || menu == 'C')
{
return 0;
}
}
system("pause");
return 0;
}
}
Looking at this part of your code:
if (menu == 'A' || menu == 'a')
{
printf("Please enter a frequency: ");
scanf("%f", &frequency[i] + 1);
printf("Please enter a duration: ");
scanf("%d", &duration[i] + 1);
system("cls");
}
I see these problems:
1) You never incremt i (something you should do every time a new tone is added)
2) You do + 1 to the address
3) You don't check for max 100 tones
4) You don't check the return value from scanf
You can fix it like:
if (menu == 'A' || menu == 'a')
{
if (i < 100)
{
printf("Please enter a frequency: ");
if (scanf("%f", &frequency[i]) == 1)
{
printf("Please enter a duration: ");
if (scanf("%d", &duration[i]) == 1)
{
// Tone added - increment i
++i;
}
else
{
printf("Illegal dureation\n");
}
}
else
{
printf("Illegal frequency\n");
}
}
else
{
printf("You already have 100 tones\n");
}
}
Note: It will probably be good to rename i to a more descriptive name. e.g. numTones

CGPA Calculation

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.

Im getting Id returned 1 exit status [closed]

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.

Resources