New Data is Replacing Older Data Instead Adding New Data in C - c

I am making simple C program. This program should be able to store data in memory (not using database). But when i tested it, it can not store more than 1 data. Every time i store new data, the old one get replaced. I hope these screenshot could help you understand what i mean.
As you can see, i input first set of data. It shows up with no problem.
Then, i added second set of data. Where is the first data ? The No 2 Data supposed to be after the No 1 data. But the first data get lost at all.
This is my input code :
//this is function for the input
void masukan() {
n=n+1; //use n as index for the struct (mhsw)
printf("\n");
printf("Masukkan NIM : "); scanf("%s", mhsw[n].nim);
printf("Masukkan Nama : "); scanf("%s", mhsw[n].nama);
printf("Masukkan Golongan UKT : "); scanf("%d", &mhsw[n].golUKT);
mhsw[n].nominalUKT = nominal(mhsw[n].golUKT)*100000;
printf("");
}
//the nominal UKT = factorial of golUKT
int nominal(int n) {
int hasil = 0;
if (n == 1) return 15;
else {
hasil = ((n*n) + nominal(n-1));
return hasil;
}
}
This is my output code :
//this is for the output
void tampil() {
if (!kbhit()) {
printf("\n");
printf(" ===========================================================================\n");
printf(" DATA MAHASISWA \n");
printf("|====|================|====================|==============|=================|\n");
printf("| No | NIM | Nama | Golongan UKT | Nominal UKT |\n");
printf("|====|================|====================|==============|=================|\n");
for(i=0; i<n; i++); //to display record
{
printf("%5d", i);
printf("%17s", mhsw[i].nim);
printf("%20s", mhsw[i].nama);
printf("%15d", mhsw[i].golUKT);
printf("%18d", mhsw[i].nominalUKT);
printf("\n");
}
printf("|====|================|====================|==============|=================|\n");
printf("\n \nPencet sembarang tombol untuk kembali ke Home");
getch();
} else {
menu();
}
}
This is the whole code :
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <windows.h>
//global variable
struct mahasiswa
{
char nim[10];
char nama[50];
int golUKT;
long int nominalUKT;
};
struct mahasiswa mhsw[50];
int i, n, nim;
bool ada = false;
char carinim[10];
//prototype
void login();
void menu();
void gotoxY(int, int);
void loading();
void masukan();
int nominal(int);
void tampil();
void cari();
//main function
void main() {
login();
}
void login() {
char user[10], pass[10];
int x, y;
printf("Masukkan Username : "); scanf("%s", user);
printf("Masukkan Password : "); scanf("%s", pass);
x = strcmp(user, "admin");
y = strcmp(pass, "admin123");
if (x == 0 && y == 0) {
menu();
}
else {
system("cls");
printf("Gagal Login, Ulangi Proses\n");
login();
}
}
void menu()
{
int pilih;
system("cls");
printf(" ____________________________________________________\n");
printf("| Siukat Lite |\n");
printf("|____________________________________________________|\n");
printf("|_______________________Home_________________________|\n");
printf("| 1. Pendaftaran Mahasiswa |\n");
printf("| 2. Daftar Golongan UKT |\n");
printf("| 3. Cari NIM |\n");
printf("| 4. Cetak Data Siukat |\n");
printf("| Ketik sembarang nomor untuk keluar |\n");
printf("|____________________________________________________|\n");
printf("Masukkan Pilihan Nomor : ");
scanf("%d",&pilih);
switch(pilih)
{
case 1:
//loading();
printf("\nPendaftaran Mahasiswa");
masukan();
menu();
break;
case 2:
case 3:
printf("Cari NIM");
cari();
break;
case 4:
//loading();
printf("\nData Siukat");
tampil();
default:
menu();
}
}
void gotoxy (int x,int y)
{
HANDLE hConsoleOutput;
COORD dwCursorPosition;
dwCursorPosition.X = x;
dwCursorPosition.Y = y;
hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hConsoleOutput,dwCursorPosition);
}
void loading() {
int a;
char x;
gotoxy(1,12);
for(a=0;a<=5;a++)
{
usleep(500000);
printf("*",x);
}
}
//for input record
void masukan() {
printf("\n");
printf("Masukkan NIM : "); scanf("%9s", mhsw[n].nim);
printf("Masukkan Nama : "); scanf("%49s", mhsw[n].nama);
printf("Masukkan Golongan UKT : "); scanf("%d", &mhsw[n].golUKT);
mhsw[n].nominalUKT = nominal(mhsw[n].golUKT)*100000;
printf("");
n=n+1;
}
int nominal(int n) {
int hasil = 0;
if (n == 1) return 15;
else {
hasil = ((n*n) + nominal(n-1));
return hasil;
}
}
//for find NIM
void cari() {
printf("\nMasukkan NIM :"); scanf("%s", carinim);
printf("\n");
if (!kbhit()) {
for(i=0; i<n; i++) {
if (strcmp(carinim, mhsw[i].nim) == 1) {
ada = true;
} else {
ada = false;
}
}
if(ada) {
printf("NIM : %s \n", mhsw[i].nim);
} else {
printf("Data Tidak Ditemukan");
}
} else {
menu();
}
}
//for showing the record
void tampil() {
if (!kbhit()) {
printf("\n");
printf(" ===========================================================================\n");
printf(" DATA MAHASISWA \n");
printf("|====|================|====================|==============|=================|\n");
printf("| No | NIM | Nama | Golongan UKT | Nominal UKT |\n");
printf("|====|================|====================|==============|=================|\n");
for(i=0; i<n; i++);
{
printf("%5d", i);
printf("%17s", mhsw[i].nim);
printf("%20s", mhsw[i].nama);
printf("%15d", mhsw[i].golUKT);
printf("%18d", mhsw[i].nominalUKT);
printf("\n");
}
printf("|====|================|====================|==============|=================|\n");
printf("\n \nPencet sembarang tombol untuk kembali ke Home");
getch();
} else {
menu();
}
}
Thanks for any help
UPDATE : i tried to put increment after the insertion and see the value of n. Still doesn't work. Here is some screenshot.
The value of n before insertion is still 0
The value of n after insertion, it incremented to 1. But the data doesn't get recorded at all.

You have a ; after your for inside tampil().
for(i=0; i<n; i++);
It's causing your loop body to be empty so nothing is printed inside tampil().
To protect against such mistakes use a good compiler gcc and enable all warnings (for gcc it's -Wall -Wextra). Ex. gcc outputs this helpful warning message:
1.c: In function ‘tampil’:
1.c:167:9: warning: this ‘for’ clause does not guard... [-Wmisleading-indentation]
167 | for(i=0; i<n; i++);
| ^~~
1.c:168:9: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the ‘for’
168 | {
| ^

It is due to the fact that you do the n = n+1 instruction before the insertion in masukan(). You must move this instruction at the end of the function.
When the program starts, the value of n is 0. When you insert a new set of values, you first increment n and store thus the values at index 1. The values at index 0 are the default values.
When you print the values, you print the values with index 0 to n-1. This doesn't include the value at index n that you just inserted.
By moving the increment of n at the end of the new value insertion, the first value set will be inserted at index position 0 and n is the number of values in the table. n is also the index of the after last values in the table. This is where you insert the next value after what you increment n.
EDIT: another error is a ; after the for(i=0; i<n; i++) in the tampil function. That is the reason one line is printed regardless of the value of n.

Related

binary tree for sorting and search on c program

So i have a program to data the users input.
in this program i want to make some function
to ask the user for the product coming in
to display the history of data coming in by sorting by date.
to ask the user for the product coming out
to display the history of data coming out by sorting by date.
to search a specific product data by calling the code
to display all the data, the data is sorted by date and then the weight is data coming in minus data coming out
actually i have made a complete program of this, but the teacher updated the assignment. in the previous assignment i mostly use array instead of pointer and for sorting i use recursive bubble sort.
For now i need someone's help to make a sort by date using binary tree and search by product code using binary tree
Note : i must use binary tree because that's the assignment my teacher gave me
assignments are :
have a function with address argument
have a function with array argument
have function with argument as address to a structure
have a function with structure argument
add node to linked list/stack/queue/tree
read/delete node from linked list/stack/queue/tree.
Honestly i still understand linked list/stack/queue/tree. i thought to make a linked list i just change my array to a pointer
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
typedef struct {
int kode;
int amount;
char variety[20];
int weight;
int date;
}data;
data *innData;
data *outtData;
int menu;
int i;
int pointIn = 0, pointOut=0;
int amount = 0;
char variety[5][20] = {"Fish", "Crab", "Squid", "Clam", "Lobster"};
void sortDate(data *list, int range);
void back_menu();
void input();
void inputHistory();
void output();
void outHistory();
//void sort
//void calculateWeight
//void search();
//void showAll();
int main(){
innData=(data *)malloc(100*sizeof(data));
outtData=(data *)malloc(100*sizeof(data));
do {
printf(" ---------------- MARINE PRODUCT STOCK PROGRAM ---------------");
printf( " \n\nMAIN MENU :\n");
printf( "___________________________________________________________________\n\n");
printf( " 1. Input Product\n\n"); //data that the user input
printf( " 2. Entered History\n\n"); //show the history of the entered data by sorting the date
printf( " 3. Output Product\n\n"); //data that the user take out
printf( " 4. Exited Product\n\n");//show the history of the exited data by sorting the date
printf( " 5. Search Product\n\n"); //show the specific product by calling the code of the product
printf( " 6. Show All Product\n\n");
printf( " _____________________________\n\n");
printf( " choose (1 - 6) : ");
scanf ("%d", &menu);
if (menu < 1 || menu > 2) {
system("cls");
printf ("Please select the number between 1-2\n");
back_menu();
}
switch(menu) {
case 1:
input();
break;
case 2:
inputHistory();
break;
case 3:
output();
break;
case 4:
outHistory();
break;
// case 5:
// search(); // this function is used to search specific product by entering the code
// break;
// case 6:
// showAll(); // this function is used to show all of the stock in the warehouse. but the weight = input-output
// break;
}
free(innData);
free(outtData);
}
while (menu != 6);
system("cls");
printf ("============ Thankyou for using our program ============");
return 0;
}
void input(){
int code;
system ("cls");
printf("================= INPUT MENU =================\n\n");
printf("Enter the number of items to be recorded : "); scanf("%d", &amount);
printf( "_________________________________________________________________\n");
printf("\n Kode 0 = Fish \n kode 1 = Crab \n kode 2 = Squid \n kode 3 = Clam \n kode 4 = Lobster\n\t\t\t ");
for(i = 0; i < amount; i++){
printf("\n\nPRODUCT ORDER-%d",pointIn + 1);
printf("\n\tInput code\t : "); scanf(" %d", &code);
if(code < 0 || code > 4){
printf ("product with that number is not available, select number 0-4\n");
i--;
}
else {
strcpy( (innData+pointIn)->variety, variety[code]);
(innData+pointIn)->kode = code;
printf("\tvariety\t\t : %s\n", (innData+pointIn)->variety);
printf("\t Weight\t : "); scanf(" %d", &(innData+pointIn)->weight );
printf("\t Date (YYYYMMDD)\t : ");scanf(" %d", &(innData+pointIn)->date);
pointIn++;
}
}
back_menu();
}
void inputHistory(){
system("cls");
printf("***** INPUT STOCK DATE HISTORY ***** \n" );
printf("------------------------------------------------------------------\n");
printf("| DATE | CODE | NAME | WEIGHT | \n");
printf("------------------------------------------------------------------\n");
//call sort function here
for (i = 0; i < pointIn; i++){
printf(" %d %d %s %d \n", (innData+i)->date,(innData+i)->kode, (innData+i)->variety,(innData+i)->weight) ;
}
printf("------------------------------------------------------------------\n");
back_menu();
}
void output(){
int code;
system ("cls");
printf("================= INPUT MENU =================\n\n");
printf("Enter the number of items to be recorded : "); scanf("%d", &amount);
printf( "_________________________________________________________________\n");
printf("\n Kode 0 = Fish \n kode 1 = Crab \n kode 2 = Squid \n kode 3 = Clam \n kode 4 = Lobster\n\t\t\t ");
for(i = 0; i < amount; i++){
printf("\n\nPRODUCT ORDER-%d",pointOut + 1);
printf("\n\tInput code\t : "); scanf(" %d", &code);
if(code < 0 || code > 4){
printf ("product with that number is not available, select number 0-4\n");
i--;
}
else {
strcpy( (outtData+pointOut)->variety, variety[code]);
(outtData+pointOut)->kode = code;
printf("\tvariety\t\t : %s\n", (outtData+pointOut)->variety);
printf("\tWeight\t : "); scanf(" %d", &(outtData+pointOut)->weight );
printf("\tDate (YYYYMMDD)\t : ");scanf(" %d", &(outtData+pointOut)->date);
pointOut++;
}
}
back_menu();
}
void outHistory(){
system("cls");
printf("***** EXIT STOCK DATE HISTORY ***** \n" );
printf("------------------------------------------------------------------\n");
printf("| DATE | CODE | NAME | WEIGHT | \n");
printf("------------------------------------------------------------------\n");
//call sort function here
for (i = 0; i < pointIn; i++){
printf(" %d %d %s %d \n", (outtData+i)->date,(outtData+i)->kode, (outtData+i)->variety,(outtData+i)->weight) ;
}
printf("------------------------------------------------------------------\n");
back_menu();
}
void back_menu(){
printf("\n\n\t\t\tPress any key to continue.....");
getch();
system("cls");
}

My Array item keeps getting overwritten in C programming

I'm trying to create a program that allows users to add information into a nameCard array using a function called AddNameCard. but when I try to add another set of input in, the previous items seems to get overwritten. and the listnamecard function only displays the last inputted items. Anyone know what i need to do to get around this problem? I'm learning C programming currently, go easy on me please :).
#include <stdio.h>
#include <stdlib.h>
# define Max_Size 5
typedef struct
{
int nameCardID;
char personName[20];
char companyName[20];
} NameCard;
NameCard nameCard[Max_Size];
void AddNameCard() {
int j;
printf("\n");
for (int i = j - 1; i < j; i++){
printf("Enter Name Card ID: \n");
scanf("%d", &nameCard[i].nameCardID);
printf("Enter Person Name: \n");
scanf("%s", &nameCard[i].personName);
printf("Enter Company Name : \n");
scanf("%s", &nameCard[i].companyName);
}
printf("\n");
return;
}
void ListNameCard() {
int j;
printf("\n");
printf("\nName_Card_ID Person_Name Company_Name \n");
for (int i = 0; i < j; i++){
printf("%d %s %s \n", nameCard[i].nameCardID, nameCard[i].personName, nameCard[i].companyName);
}
printf("\n");
return;
}
void GetNameCard() {
printf("%d %s %s", nameCard[1].nameCardID, nameCard[1].personName, nameCard[1].companyName);
}
int main()
{
int options;
while (options != 5) {
printf("1:List Name Cards\n2:Add Name Card\n3:Remove Name Cards\n4:Get Name Cards\n5:quit\n");
printf("\n");
printf("What would you like to do? : ");
scanf("%d", &options);
switch (options)
{
case 1:
ListNameCard();
break;
case 2:
AddNameCard();
break;
case 3:
printf("Case3 ");
printf("\n");
break;
case 4:
GetNameCard();
break;
default:
printf("quit ");
}
}
#include <stdio.h>
#include <stdlib.h>
#define Max_Size 5
typedef struct {
int nameCardID;
char personName[20];
char companyName[20];
}
NameCard;
NameCard nameCard[Max_Size];
void AddNameCard() {
printf("\n");
int i;
printf("enter the number of person you want to add :");
scanf("%d", & i);
printf("Enter Name Card ID: \n");
scanf("%d", & nameCard[i - 1].nameCardID);
printf("Enter Person Name: \n");
scanf("%s", nameCard[i - 1].personName);
printf("Enter Company Name : \n");
scanf("%s", nameCard[i - 1].companyName);
printf("\n");
return;
}
void ListNameCard() {
printf("\n");
printf("\nName_Card_ID Person_Name Company_Name \n");
for (int i = 0; i < Max_Size; i++) {
printf("%d %s %s \n", nameCard[i].nameCardID, nameCard[i].personName, nameCard[i].companyName);
}
printf("\n");
return;
}
void GetNameCard() {
int n;
printf("\n");
printf("enter the number of the person");
scanf("%d", & n);
printf("%d %s %s", nameCard[n - 1].nameCardID, nameCard[n - 1].personName, nameCard[n - 1].companyName);
printf("\n");
}
int main() {
int options;
while (options != 5) {
printf("1:List Name Cards\n2:Add Name Card\n3:Remove Name Cards\n4:Get Name Cards\n5:quit\n");
printf("\n");
printf("What would you like to do? : ");
scanf("%d", & options);
switch (options) {
case 1:
ListNameCard();
break;
case 2:
AddNameCard();
break;
case 3:
printf("Case3 ");
printf("\n");
break;
case 4:
GetNameCard();
break;
default:
printf("quit ");
}
}
}
Let's follow the comments' suggestions.
First we avoid using options variable uninitialized by changing:
while (options != 5) {
...
}
To:
do {
... (set options variable here)
} while (options != 5);
Secondly, we change the name of j variable to count and use it as function parameter/return so we keep track and update the number of added cards. For instance, AddNameCard becomes:
int AddNameCard(int count)
{
if (count < Max_Size) {
count++;
int i = count - 1;
printf("\n");
printf("Enter Name Card ID: ");
scanf("%d", &nameCard[i].nameCardID);
discard_newline();
printf("Enter Person Name: ");
scanf("%19[^\n]", nameCard[i].personName);
discard_newline();
printf("Enter Company Name: ");
scanf("%19[^\n]", nameCard[i].companyName);
discard_newline();
} else {
printf("Maximum number of cards reached\n");
}
printf("\n");
return count;
}
(discard_newline prevents newline characters to creep into the next scanf. "%19[^\n]" format prevents buffer overrun into 20 length strings.)
The name of an array variable is already taken as its address (or the address of its first element). So personName and companyName members shouldn't be preceded by &.
The code becomes:
#include <stdio.h>
#include <stdlib.h>
#define Max_Size 5
typedef struct
{
int nameCardID;
char personName[20];
char companyName[20];
} NameCard;
NameCard nameCard[Max_Size];
void discard_newline(void)
{
while( getchar() != '\n' );
}
int AddNameCard(int count)
{
if (count < Max_Size) {
count++;
int i = count - 1;
printf("\n");
printf("Enter Name Card ID: ");
scanf("%d", &nameCard[i].nameCardID);
discard_newline();
printf("Enter Person Name: ");
scanf("%19[^\n]", nameCard[i].personName);
discard_newline();
printf("Enter Company Name: ");
scanf("%19[^\n]", nameCard[i].companyName);
discard_newline();
} else {
printf("Maximum number of cards reached\n");
}
printf("\n");
return count;
}
void ListNameCard(int count) {
if (count > 0) {
printf("\nName_Card_ID Person_Name Company_Name\n");
for (int i = 0; i < count; i++){
printf("%d %s %s \n", nameCard[i].nameCardID, nameCard[i].personName, nameCard[i].companyName);
}
} else {
printf("Empty list: none added yet\n");
}
printf("\n");
return;
}
void GetNameCard() {
printf("%d %s %s", nameCard[1].nameCardID, nameCard[1].personName, nameCard[1].companyName);
}
int main()
{
int options;
int count = 0;
do {
printf("1:List Name Cards\n2:Add Name Card\n3:Remove Name Cards\n4:Get Name Cards\n5:quit\n");
printf("\n");
printf("What would you like to do? : ");
scanf("%d", &options);
switch (options)
{
case 1:
ListNameCard(count);
break;
case 2:
count = AddNameCard(count);
break;
case 3:
printf("Case3 ");
printf("\n");
break;
case 4:
GetNameCard();
break;
default:
printf("quit\n");
}
} while (options != 5);
}
Running it:
1:List Name Cards
2:Add Name Card
3:Remove Name Cards
4:Get Name Cards
5:quit
What would you like to do? : 1
Empty list: none added yet
1:List Name Cards
2:Add Name Card
3:Remove Name Cards
4:Get Name Cards
5:quit
What would you like to do? : 2
Enter Name Card ID: 123
Enter Person Name: John Smith
Enter Company Name: COMP_01
1:List Name Cards
2:Add Name Card
3:Remove Name Cards
4:Get Name Cards
5:quit
What would you like to do? : 1
Name_Card_ID Person_Name Company_Name
123 John Smith COMP_01
1:List Name Cards
2:Add Name Card
3:Remove Name Cards
4:Get Name Cards
5:quit
What would you like to do? : 5
quit
You still have to complete the other options.

return multiple output from option 1 , how to store it at main function and use it for option 3

How can I store the result of getYearOfBirth ,option 1 and 2 to main function and use it for option 3 where to print those result as receipt.
What problem i have found is:
1) if I put , for example :
int y;
y=getYearOfBirth
when I run until the function where user can re-enter the yearOfBirth , it will print the 1st input instead of re-enter input
2) in option 1 , I have many value to return , totalSum, quantity1,quantity2,quantity3,quantity4, i dont know how to return it to main function
This is my code:
edited ,added y=getYearOfBirth()
#include <stdio.h>
#include <math.h>
void printWelcome(void); //print welcome
int getYearOfBirth (void); // prompt user to enter a year of birth , below 1900 show no valid
int confirmYob (void); // check if the year of birth is enter correctly
void displayMenu (void); //in option1 show table of product,with code and price
int getTotalSum (void); // when user selected product will calculate sum
int confirmTotalSum (void); // if the user make mistake can Re-enter the product purchase
int parkingCalculator (void); //enter the parking hour and calculate amount that need to pay
int confirmParkingFee (void); // if the user make mistake can Re-enter the parking hour
int main ()
{
int choice,y;
printWelcome();
y = getYearOfBirth();
confirmYob();
while(1)
{
printf("Select an Action:\n");
printf("1.Enter product purchased\n");
printf("2.Enter parking hours\n");
printf("3.Calculate and print the total bill\n");
printf("4.Exit\n\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
displayMenu();
getTotalSum();
confirmTotalSum();
}
break;
case 2:
parkingCalculator();
confirmParkingFee();
break;
case 3:
{
printf("Test3\n\n");
printf("%d\n\n",y)
break;
}
case 4:
exit(0);
}
}
return 0;
}
void printWelcome(void)
{
printf("Welcom to :-\n");
printf("+-----------------------+\n");
printf("| BIGSALES MALL |\n");
printf("+-----------------------+\n");
}
int getYearOfBirth (void)
{
int yearOfBirth;
printf("Please enter your year of birth:\n");
scanf_s("%d", &yearOfBirth);
while (yearOfBirth <1900)
{
printf("Warning! The year you entered is not valid!\n");
printf("Please Enter Again.\n");
printf("Please enter your year of birth:\n");
scanf_s("%d", &yearOfBirth);
}
printf("Your year of birth is: %d\n",yearOfBirth);
return yearOfBirth;
}
int confirmYob (void)
{
int confirm1;
printf("Is it correct? Enter 1 for YES to Continue ,0 for NO and Re-Enter
\n");
scanf("%d",&confirm1);
while( confirm1 ==0 )
{
getYearOfBirth();
printf("Is it correct? (Enter 1 for YES to Continue ,0 for NO and Re-
Enter) \n");
scanf("%d",&confirm1);
}
printf("Year of birth confirmed\n\n\n");
return 0;
}
void displayMenu (void)
{
printf("+--------------+--------------------+\n");
printf("| Product Code | Rentail Price (RM) |\n");
printf("+--------------+--------------------+\n");
printf("| 1 | 45.20 |\n");
printf("+--------------+--------------------+\n");
printf("| 2 | 14.50 |\n");
printf("+--------------+--------------------+\n");
printf("| 3 | 3.45 |\n");
printf("+--------------+--------------------+\n");
printf("| 4 | 7.80 |\n");
printf("+--------------+--------------------+\n");
}
int getTotalSum (void)
{
int code;
float sum=0,totalSum,quantity1=0,quantity2=0,quantity3=0,quantity4=0;
while(1)
{
printf("Enter a product code.(Enter 5 to get total sum)\n");
scanf("%d",&code);
switch(code)
{
case 1:
{
quantity1=( quantity1 + 1);
sum=( 45.20 + sum);
printf("Current Sum: RM%.2f\n",sum);
break;
}
case 2:
{
quantity2=( quantity2 + 1);
sum=(14.50 + sum);
printf("Current Sum: RM%.2f\n",sum);
break;
}
case 3:
{
quantity3=( quantity3 + 1);
sum=(3.45 + sum);
printf("Current Sum: RM%.2f\n",sum);
break;
}
case 4:
{
quantity4=( quantity4 + 1);
sum=(7.80 + sum);
printf("Current Sum: RM%.2f\n",sum);
break;
}
case 5:
totalSum= sum;
printf("+--------------+--------------------+----------+\n");
printf("| Product Code | Rentail Price (RM) | Quantity |\n");
printf("+--------------+--------------------+----------+\n");
printf("| 1 | 45.20 | %.f |\n",quantity1);
printf("+--------------+--------------------+----------+\n");
printf("| 2 | 14.50 | %.f |\n",quantity2);
printf("+--------------+--------------------+----------+\n");
printf("| 3 | 3.45 | %.f |\n",quantity3);
printf("+--------------+--------------------+----------+\n");
printf("| 4 | 7.80 | %.f |\n",quantity4);
printf("+--------------+--------------------+----------+\n");
printf("Total Sum: RM%.2f\n",totalSum);
return 0;
}
}
}
int confirmTotalSum (void)
{
int confirm2;
printf("Is it correct? Enter 1 for YES to Continue ,0 for NO and Re-Enter \n");
scanf("%d",&confirm2);
while( confirm2 ==0 )
{
getTotalSum();
printf("Is it correct? (Enter 1 for YES to Continue ,0 for NO and Re-Enter) \n");
scanf("%d",&confirm2);
}
printf("Total Sum confirmed\n\n\n");
return 0;
}
int parkingCalculator (void)
{
float parkingHours, totalHours,parkingFee,maxParkingFee;
printf("Enter a parking hour\n");
scanf("%f",&parkingHours);
totalHours = ceil(parkingHours);
if (totalHours>24)
{
parkingFee=0;
printf("Parking Fee: RM %.2f\n\n",parkingFee);
}
else {if (totalHours==24)
{
parkingFee=10;
printf("Parking Fee: RM %.2f\n\n",parkingFee);
}
else {if (totalHours>3)
{
parkingFee=(2+((totalHours-3)*0.50));
if (parkingFee>10)
{
parkingFee=10;
}
printf("Parking Fee: RM %.2f\n\n",parkingFee);
}
else {if (totalHours<=3 && totalHours>=1)
{
parkingFee=2;
printf("Parking Fee: RM %.2f\n\n",parkingFee);
}
else {if(totalHours==0)
{
parkingFee=0;
printf("Parking Fee: RM %.2f\n\n",parkingFee);
}
}
}
}
}
return parkingFee;
}
int confirmParkingFee (void)
{
int confirm3;
printf("Is it correct? Enter 1 for YES to Continue ,0 for NO and Re-Enter \n");
scanf("%d",&confirm3);
while (confirm3 >1)
{
printf("Error");
}
while( confirm3 ==0 )
{
parkingCalculator();
printf("Is it correct? (Enter 1 for YES to Continue ,0 for NO and Re-Enter) \n");
scanf("%d",&confirm3);
}
printf("Parking Fee confirmed\n\n\n");
return 0;
}
Follow the execution of you program very carefully:
y is set on line 15, from the 1st call to getYearOfBirth()
then you call confirmYob() on line 16
confirmYob() makes a 2nd call to getYearOfBirth() on line 82
the results of the call to getYearOfBirth() on line 82 are never stored
you print the value of y (set on line 15; see #1), so of course it will print the value you provided during the 1st call to getYearOfBirth()
Also, here is what I meant by a minimal example that focuses on the problem at hand; this uses the same logic for YoB as your larger program, but makes is much easier to see the problem:
#include <stdio.h>
#include <math.h>
int getYearOfBirth (void); // prompt user to enter a year of birth , below 1900 show no valid
int confirmYob (void); // check if the year of birth is enter correctly
int main () {
int y;
y = getYearOfBirth();
confirmYob();
printf("%d\n\n",y);
return 0;
}
int getYearOfBirth (void) {
int yearOfBirth;
printf("Please enter your year of birth:\n");
scanf("%d", &yearOfBirth);
while (yearOfBirth <1900) {
printf("Warning! The year you entered is not valid!\n");
printf("Please Enter Again.\n");
printf("Please enter your year of birth:\n");
scanf("%d", &yearOfBirth);
}
printf("Your year of birth is: %d\n",yearOfBirth);
return yearOfBirth;
}
int confirmYob (void) {
int confirm1;
printf("Is it correct? Enter 1 for YES to Continue ,0 for NO and Re-Enter\n");
scanf("%d",&confirm1);
while (confirm1 == 0) {
getYearOfBirth();
printf("Is it correct? (Enter 1 for YES to Continue ,0 for NO and Re-Enter) \n");
scanf("%d",&confirm1);
}
printf("Year of birth confirmed\n\n\n");
return 0;
}
Here is my suggestion for fixing it : call getYearOfBirth() just once A key part of this is including the confirmation logic inside the getYearOfBirth() function, so that if it needs to be re-entered, you are still within the same call to getYearOfBirth():
#include <stdio.h>
#include <math.h>
int getYearOfBirth (void); // prompt user to enter a year of birth , below 1900 show no valid
int main () {
int y;
y = getYearOfBirth();
printf("%d\n\n",y);
return 0;
}
int getYearOfBirth (void) {
int yearOfBirth;
int confirm1;
while (1) {
printf("Please enter your year of birth:\n");
scanf("%d", &yearOfBirth);
if (yearOfBirth < 1900) {
printf("Warning! The year you entered is not valid!\n");
printf("Please Enter Again.\n");
continue;
}
printf("Your year of birth is: %d\n",yearOfBirth);
printf("Is it correct? Enter 1 for YES to Continue ,0 for NO and Re-Enter\n");
scanf("%d",&confirm1);
if (confirm1 == 1) {
printf("Year of birth confirmed\n\n\n");
break;
}
}
return yearOfBirth;
}

scanf in function seems to be writing garbage values to one array but not the other, despite both being identical

So in the function getRoster, I have two arrays that are defined in the same way in main and called in the same way. But for some reason, one of the scanf functions write garbage data to the array so that in line 91 (A debugging line in this case) one value is the one I entered and the other is seemingly random. I've checked this code top to bottom already to see if there were any notations that I added to one array and not the other, and I can't seem to find a single one.
#include <stdio.h>
#include <stdlib.h>
void getRoster(int *jerseyNumbers[10], int *playerRatings[10])
{
int i;
for (i=0; i<5; ++i)
{
printf("Enter player %d's jersey number\n", i+1);
scanf("%d", &jerseyNumbers[i]);
printf("Enter player %d's rating\n", i+1);
scanf("%d", &playerRatings[i]);
}
}
void updateRating(int *jerseyNumbers[10], int *playerRatings[10])
{
int i, n = -1, hold;
printf("Enter a jersey number:\n");
scanf("%d", n);
for (i=0; i<5; ++i)
if(jerseyNumbers[i]==n)
hold = n;
if (n=-1);
printf("Error");
printf("Enter a new rating for player:\n");
scanf("%d", &playerRatings[n]);
}
void aboveRating()
{
}
void replacePlayer()
{
}
void outputRoster(int *jerseyNumbers[10], int *playerRatings[10])
{
int i;
for (i=0; i<5; ++i)
{
printf("Player %d -- Jersey number: %d , Rating: %d \n", i+1, jerseyNumbers[i], playerRatings[i]);
}
}
void menu(int *jerseyNumbers[10], int *playerRatings[10])
{
char menuInput;
printf("\nMENU\n");
printf("u - Update player rating\n");
printf("a - Output players above a rating\n");
printf("r - Replace player\n");
printf("o - Output roster\n");
printf("q - Quit\n");
printf("\nChoose an option:\n");
scanf(" %c", &menuInput);
if (menuInput == 'u')
{updateRating(&jerseyNumbers[10], &playerRatings[10]);
}
else if (menuInput == 'a')
{aboveRating();
}
else if (menuInput == 'r')
{replacePlayer();
}
else if (menuInput == 'o')
{outputRoster(&jerseyNumbers[10], &playerRatings[10]);
}
else if (menuInput == 'q')
printf("Reached Quit");
else
printf("Input Error\n");
}
int main()
{
int *jerseyNumbers[10];
int *playerRatings[10];
char menuInput;
int quitFlag = 0;
getRoster(&jerseyNumbers[10], &playerRatings[10]);
printf("%d %d", jerseyNumbers[0], playerRatings[1]);
menu(&jerseyNumbers[10], &playerRatings[10]);
return 0;
}
Refer to #NaveenKumar and #DeiDei's comment to your question.
Actually, the logic of your program is alright. But where you've gone wrong is the syntax regarding how you have declared the arrays and passed them as arguments to the functions. I have listed the changes to be made below:
First of all Dylan, do not declare an array as: int
*jerseyNumbers[10] and int *playerRatings[10]. For the regular array which you actually need, just declare as:
a. *jerseyNumbers and *playerRatings... Remove the size. OR
b. jerseyNumbers[10] and playerRatings[10]... Remove the * from the declaration.
When you're passing these arrays as arguments to a function, don't ever send it how you've done. Just pass the array name as an argument. Like this: getRoster(jerseyNumbers, playerRatings); and menu(jerseyNumbers, playerRatings);.
In the function definition, the parameters representing the array should be either *arrayname or arrayname[size]. Since we have used the first during declaration, use the same here as follows:
void getRoster(int *jerseyNumbers, int *playerRatings) {...},
void updateRating(int *jerseyNumbers, int *playerRatings){...},
void outputRoster(int *jerseyNumbers, int *playerRatings) {...},
void menu(int *jerseyNumbers, int *playerRatings) {...}.
And as #PaulSm4 has suggested don't use the semi-colon at the end of an if conditional. Though this isn't the cause of your problem here, it is a practice you need to follow and mistake to be avoided.
I have attached the working code below, along with the output.
CODE:
#include <stdio.h>
#include <stdlib.h>
void getRoster(int *jerseyNumbers, int *playerRatings)
{
int i;
for (i=0; i<5; ++i)
{
printf("Enter player %d's jersey number\n", i+1);
scanf("%d", &jerseyNumbers[i]);
printf("Enter player %d's rating\n", i+1);
scanf("%d", &playerRatings[i]);
}
}
void updateRating(int *jerseyNumbers, int *playerRatings)
{
int i, n = -1, hold;
printf("Enter a jersey number:\n");
scanf("%d", n);
for (i=0; i<5; ++i)
if(jerseyNumbers[i]==n)
hold = n;
if (n=-1)
printf("Error");
printf("Enter a new rating for player:\n");
scanf("%d", &playerRatings[n]);
}
void aboveRating()
{
}
void replacePlayer()
{
}
void outputRoster(int *jerseyNumbers, int *playerRatings)
{
int i;
for (i=0; i<5; ++i)
{
printf("Player %d -- Jersey number: %d , Rating: %d \n", i+1, jerseyNumbers[i], playerRatings[i]);
}
}
void menu(int *jerseyNumbers, int *playerRatings)
{
char menuInput;
printf("\nMENU\n");
printf("u - Update player rating\n");
printf("a - Output players above a rating\n");
printf("r - Replace player\n");
printf("o - Output roster\n");
printf("q - Quit\n");
printf("\nChoose an option:\n");
scanf(" %c", &menuInput);
if (menuInput == 'u')
{updateRating(jerseyNumbers, playerRatings);
}
else if (menuInput == 'a')
{aboveRating();
}
else if (menuInput == 'r')
{replacePlayer();
}
else if (menuInput == 'o')
{outputRoster(jerseyNumbers, playerRatings);
}
else if (menuInput == 'q')
printf("Reached Quit");
else
printf("Input Error\n");
}
int main()
{
int jerseyNumbers[10];
int playerRatings[10];
char menuInput;
int quitFlag = 0;
getRoster(jerseyNumbers, playerRatings);
printf("%d %d", jerseyNumbers[0], playerRatings[1]);
menu(jerseyNumbers, playerRatings);
return 0;
}
OUTPUT:
Enter player 1's jersey number
1
Enter player 1's rating
10
Enter player 2's jersey number
2
Enter player 2's rating
20
Enter player 3's jersey number
3
Enter player 3's rating
30
Enter player 4's jersey number
4
Enter player 4's rating
40
Enter player 5's jersey number
5
Enter player 5's rating
50
1 20
MENU
u - Update player rating
a - Output players above a rating
r - Replace player
o - Output roster
q - Quit
Choose an option: o
Player 1 -- Jersey number: 1 , Rating: 10
Player 2 -- Jersey number: 2 , Rating: 20
Player 3 -- Jersey number: 3 , Rating: 30
Player 4 -- Jersey number: 4 , Rating: 40
Player 5 -- Jersey number: 5 , Rating: 50
Hope this helps.
STRONG SUGGESTION:
Get in the habit of using curly braces often - even when you don't need them.
void updateRating(int *jerseyNumbers, int *playerRatings)
// You probably only need a pointer or an array - but probably not both ;)
{
int i, n = -1, hold;
printf("Enter a jersey number:\n");
scanf("%d", n);
for (i=0; i<5; ++i) { // Curly brace here...
if(jerseyNumbers[i]==n) { // And here...
hold = n;
}
}
if (n=-1) { // BUG ALERT: deleted ";" (and added a brace)
printf("Error"); // BUG ALERT: fixed this by removing ";"?
} else {
printf("Enter a new rating for player:\n");
scanf("%d", &playerRatings[n]);
}
Or perhaps better:
void updateRating(int *jerseyNumbers, int *playerRatings)
{
int n;
printf("Enter a jersey number:\n");
scanf("%d", n);
printf("Enter a new rating for player:\n");
scanf("%d", &playerRatings[n]);
}
And, as Rishikesh Raje correctly pointed out:
The definition of the array's and the function calls for OP is also
not correct. Please mention this also.

Hangman program string issue

I am in an introduction-to-C-programming class and we were assigned to write a Hangman program.
In the game, the computer chooses a word at random and shows how many letters it has. The user must guess the word by entering a letter they think might be in the word. Also the user only has six chances to get the word correct. With every wrong guess the picture of the being hanged will be completed. The program needs to have a main menu with three options (New game, Score and Quit). The program also needs to have these three functions:
selectWord to make a random selection of words (I created a string of words within this function).
drawMan to draw the hangman.
checkWord to check to see if the input is correct and replaces dashes with correct letters.
The problem for me occurs when I run the game and instead of shows the dashes the line where the dashed are supposed to be just says (null). The picture still displays though.
I am perplexed as to what might be causing this. Here is what I have so far:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char selectWord(char []);
int drawMan(int);
void checkWord(char, char [], char [], int, int);
int main()
{
int menuSelect;
int chances = 0;
char word[13];
int length;
int score;
do
{
printf("\n\t\t\tThe Hangman Game v1.0 has booted\n\n");
printf("\t\t[1]\t Create new game\n");
printf("\t\t[2]\t View Score\n");
printf("\t\t[3]\t Exit game\n");
printf("Please enter a number from the menu: ");
scanf("%d", &menuSelect);
switch(menuSelect)
{
case 1:
selectWord(word);
length = strlen(word);
char dash[20]; //Will create dashes considering the length of the selected word
int dashCount;
int letterTry;
int wordMatch = 0;
for(dashCount = 0; dashCount < length; dashCount++)
{
dash[dashCount] = '-';
}
dash[dashCount] = '\0';
while(wordMatch != 1)
{
drawMan(chances);
printf("\n%s", dash[dashCount]);
printf("\n\nPlease enter a letter: ");
fflush(NULL);
while(letterTry != '\n')
{
letterTry = getchar();
}
letterTry = getchar();
if(strcmp(dash, word) == 0)
{
printf("\nThat is correct!\n");
wordMatch = 1;
score++;
}
}
break;
case 2:
printf("The score is: %d", score);
break;
case 3:
printf("Thank you for playing!");
break;
}
}while(menuSelect != 3);
}
char selectWord(char word[])
{
int index;
char list[65][13]={{"sailboat"}, {"school"}, {"rug"}, {"chalk"}, {"tusk"}, {"camera"}, {"pajamas"}, {"cello"}, {"cake"}, {"beehive"}, {"gate"}, {"swing"}, {"lighthouse"}, {"toe"}, {"newspaper"}, {"stingray"}, {"brain"}, {"french"}, {"purse"}, {"soda"}, {"money"}, {"dryer"}, {"scale"}, {"clam"}, {"knee"}, {"artist"}, {"stomach"}, {"ticket"}, {"face"}, {"TV"}, {"can"}, {"blowfish"}, {"popsicle"}, {"beaver"}, {"snowball"}, {"dollar"}, {"sprinkler"}, {"hair"}, {"bathroom"}, {"rain"}, {"skirt"}, {"beach"}, {"suitcase"}, {"garden"}, {"doormat"}, {"pretzel"}, {"watering"}, {"fur"}, {"birthday"}, {"fries"}, {"owl"}, {"chin"}, {"spool"}, {"mushroom"}, {"peach"}, {"pelican"}, {"pineapple"}, {"flagpole"}, {"doghouse"}, {"boot"}, {"flute"}, {"quilt"}, {"hook"}, {"rainbow"}, {"happy"}};
srand(time(NULL));
index = rand()%65;
strcpy(word, list[index]);
return word;
}
int drawMan(int chances)
{
if(chances == 6)
{
printf("\n\n");
printf("\n +-------+");
printf("\n | |");
printf("\n | ");
printf("\n | ");
printf("\n |");
printf("\n |");
printf("\n /|\\\n\n");
}
else if(chances == 5)
{
printf("\n\n");
printf("\n +-------+");
printf("\n | |");
printf("\n | O");
printf("\n | |");
printf("\n |");
printf("\n |");
printf("\n /|\\\n\n");
}
else if(chances == 4)
{
printf("\n\n");
printf("\n +-------+");
printf("\n | |");
printf("\n | O");
printf("\n | /|");
printf("\n | ");
printf("\n | ");
printf("\n /|\\\n\n");
}
else if(chances == 3)
{
printf("\n\n");
printf("\n +-------+");
printf("\n | |");
printf("\n | O");
printf("\n | /|\\");
printf("\n | ");
printf("\n | ");
printf("\n /|\\\n\n");
}
else if(chances == 2)
{
printf("\n\n");
printf("\n +-------+");
printf("\n | |");
printf("\n | O");
printf("\n | /|\\");
printf("\n | ");
printf("\n | ");
printf("\n /|\\\n\n");
}
else if(chances == 1)
{
printf("\n\n");
printf("\n +-------+");
printf("\n | |");
printf("\n | O");
printf("\n | /|\\");
printf("\n | /");
printf("\n | ");
printf("\n /|\\\n\n");
}
else if(chances == 0)
{
printf("\n\n");
printf("\n +-------+");
printf("\n | |");
printf("\n | O");
printf("\n | /|\\");
printf("\n | / \\");
printf("\n | ");
printf("\n /|\\\n\n");
printf("\n\n\t You have lost!");
}
}
void checkWord(char ltrTry, char word[], char dash[], int length, int chances)
{
int count;
int correct = 0; // 0 is incorrect 1 is correct
for(count = 0; count < length; count++)
{
if(ltrTry == word[count])
{
dash[count] = word[count];
correct = 1;
}
}
}
Update #1: Thank you all for the dash string fix. Adding null character to the dash array fixed the dash problem. I added a new variable to case 1 in my main function called "wordMatch" and made it my control variable for the while loop since it's possible to get the word correct and exit the loop withuot using up all the chances. But it seems a new one has arisen. When selecting new game, the hangman is displayed twice and upon entering an incorrect letter the number of chances does not change nor does the image of the hangman change (giving me unlimited trys). However the loop exits correctly once I guess the word correctly. Why might this be happening?
Update #2: I have corrected the code and gotten it to perform correctly. The only problem now seems that the case 1 doesn't break, because it is stuck in the while loop with letterTry.
There are actually 2 problems. After assigning dashes, make sure that you terminate the string with \0 as follows:
for(dashCount = 0; dashCount < length; dashCount++)
{
dash[dashCount] = '-';
}
dash[dashCount] = '\0';
And in the while loop, print:
printf("\n%s", dash);
instead of:
printf("\n%s", dash[dashCount]);
Also, you are not updating the chances value after each try. You can do this by making checkWord function to return the correct and updating the chances count based on that, as follows:
int checkWord(char ltrTry, char word[], char dash[], int length, int trys)
{
...
return correct;
}
And in the loop, instead of just calling the function, do the following:
if(!checkWord(letterTry, word, dash, length, chances))
{
chances++;
}
Another problem I can see is while reading the letterTry value. When you read a character (in this case letterTry) after using scanf function before, the \n character will get stored in the variable. Then the program will not prompt for another input from you. In your case, the player will loose one chance for no reason. The simplest solution for this problem is to do as follows:
while(letterTry != '\n')
letterTry = getchar();
And, break from the main loop once the player gets the answer right.
if(strcmp(dash, word) == 0)
{
printf("You Won!");
score++;
break;
}
After understanding the above solutions, correctly, go through this fixed solution:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char selectWord(char[]);
int drawMan(int);
int checkWord(char, char[], char[], int);
int main()
{
int menuSelect;
int chances = 0;
char word[13];
int length;
int score;
do
{
printf("\n\t\t\tThe Hangman Game v1.0 has booted\n\n");
printf("\t\t[1]\t Create new game\n");
printf("\t\t[2]\t View Score\n");
printf("\t\t[3]\t Exit game\n");
printf("Please enter a number from the menu: ");
scanf("%d", &menuSelect);
switch(menuSelect)
{
case 1:
selectWord(word);
length = strlen(word);
char dash[20]; //Will create dashes considering the length of the selected word
int dashCount;
int letterTry;
for(dashCount = 0; dashCount < length; dashCount++)
{
dash[dashCount] = '-';
}
dash[dashCount] = '\0';
chances = 0;
while(chances != 6)
{
drawMan(chances);
printf("\n%s\n", dash);
printf("chances = %d\n", chances);
printf("\n\nPlease enter a letter: ");
fflush(NULL);
//scanf("%c%c", &letterTry, &letterTry);
while(letterTry != '\n')
letterTry = getchar();
letterTry = getchar();
if(!checkWord(letterTry, word, dash, length))
{
chances++;
}
if(strcmp(dash, word) == 0)
{
printf("You Won!");
score++;
break;
}
}
break;
case 2:
printf("The score is: %d", score);
break;
case 3:
printf("Thank you for playing!");
break;
}
}while(menuSelect != 3);
}
char selectWord(char word[])
{
int index;
char list[65][13]={{"sailboat"}, {"school"}, {"rug"}, {"chalk"}, {"tusk"}, {"camera"}, {"pajamas"}, {"cello"}, {"cake"}, {"beehive"}, {"gate"}, {"swing"}, {"lighthouse"}, {"toe"}, {"newspaper"}, {"stingray"}, {"brain"}, {"french"}, {"purse"}, {"soda"}, {"money"}, {"dryer"}, {"scale"}, {"clam"}, {"knee"}, {"artist"}, {"stomach"}, {"ticket"}, {"face"}, {"TV"}, {"can"}, {"blowfish"}, {"popsicle"}, {"beaver"}, {"snowball"}, {"dollar"}, {"sprinkler"}, {"hair"}, {"bathroom"}, {"rain"}, {"skirt"}, {"beach"}, {"suitcase"}, {"garden"}, {"doormat"}, {"pretzel"}, {"watering"}, {"fur"}, {"birthday"}, {"fries"}, {"owl"}, {"chin"}, {"spool"}, {"mushroom"}, {"peach"}, {"pelican"}, {"pineapple"}, {"flagpole"}, {"doghouse"}, {"boot"}, {"flute"}, {"quilt"}, {"hook"}, {"rainbow"}, {"happy"}};
srand(time(NULL));
index = rand()%65;
strcpy(word, list[index]);
return word;
}
int drawMan(int chances)
{
if(chances == 0)
{
printf("\n\n");
printf("\n +-------+");
printf("\n | |");
printf("\n | ");
printf("\n | ");
printf("\n |");
printf("\n |");
printf("\n /|\\\n\n");
}
else if(chances == 1)
{
printf("\n\n");
printf("\n +-------+");
printf("\n | |");
printf("\n | O");
printf("\n | |");
printf("\n |");
printf("\n |");
printf("\n /|\\\n\n");
}
else if(chances == 2)
{
printf("\n\n");
printf("\n +-------+");
printf("\n | |");
printf("\n | O");
printf("\n | /|");
printf("\n | ");
printf("\n | ");
printf("\n /|\\\n\n");
}
else if(chances == 3)
{
printf("\n\n");
printf("\n +-------+");
printf("\n | |");
printf("\n | O");
printf("\n | /|\\");
printf("\n | ");
printf("\n | ");
printf("\n /|\\\n\n");
}
else if(chances == 4)
{
printf("\n\n");
printf("\n +-------+");
printf("\n | |");
printf("\n | O");
printf("\n | /|\\");
printf("\n | ");
printf("\n | ");
printf("\n /|\\\n\n");
}
else if(chances == 5)
{
printf("\n\n");
printf("\n +-------+");
printf("\n | |");
printf("\n | O");
printf("\n | /|\\");
printf("\n | /");
printf("\n | ");
printf("\n /|\\\n\n");
}
else if(chances == 6)
{
printf("\n\n");
printf("\n +-------+");
printf("\n | |");
printf("\n | O");
printf("\n | /|\\");
printf("\n | / \\");
printf("\n | ");
printf("\n /|\\\n\n");
printf("\n\n\t You have lost!");
}
printf("print complete; exiting successfully");
}
int checkWord(char ltrTry, char word[], char dash[], int length)
{
int count;
int correct = 0; // 0 is incorrect 1 is correct
for(count = 0; count < length; count++)
{
if(ltrTry == word[count])
{
dash[count] = word[count];
correct = 1;
}
}
/* if(correct == 0)
{
trys--;
} */
return correct;
}

Resources