how to make switch case with characters work? [duplicate] - c

This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
Closed 4 years ago.
I'm currently working on an assignment for my class. In C, I have to make a program that works with the roster of a soccer team, where you can update, replace, compare players etc. However, right now I cannot make any of the options in my menu work. This will probably be better understood with my code:
#include <stdio.h>
int main(void) {
int i, jersey, rating, newJersey, newRating, playerJerseyNumber[5], playerRating[5];
char choice;
for (i = 0; i < 5; i++)
{
printf("Enter player %d's jersey number: \n", (i + 1));
scanf("%d", &playerJerseyNumber[i]);
printf("Enter player %d's rating: \n\n", (i + 1));
scanf("%d", &playerRating[i]);
}
printf("ROSTER\n");
for (i = 0; i < 5; i++)
{
printf("Player %d -- Jersey number: %d, Rating: %d\n", (i + 1), playerJerseyNumber[i], playerRating[i]);
}
printf("\n\nMENU \nu - Update player rating \na - Output players above a rating \nr - Replace player \no - Output roster \nq - Quit\n\n");
printf("Choose an option: \n");
scanf("%c", &choice);
switch (choice) {
case 'u':
{
printf("Enter a jersey number: \n");
scanf("%d", &jersey);
printf("Enter a new rating for player: \n");
scanf("%d", &newRating);
for (i = 0; i < 5; i++)
{
if (jersey == playerJerseyNumber[i])
{
playerRating[i] = newRating;
}
}
break;
}
case 'a':
{
printf("Enter a rating: \n");
scanf("%d", &rating);
printf("\n ABOVE %d\n", rating);
for (i = 0; i < 5; i++)
{
if (playerRating[i] > rating)
{
printf("Player %d -- Jersey number: %d, Rating: %d\n", (i + 1), playerJerseyNumber[i], playerRating[i]);
}
}
break;
}
case 'r':
{
printf("Enter a jersey number: \n");
scanf("%d", &jersey);
printf("Enter a new jersey number: \n");
scanf("%d", &newJersey);
printf("Enter a rating for the new player: \n");
scanf("%d", &newRating);
for (i = 0; i < 5; i++)
{
if (jersey == playerJerseyNumber[i])
{
playerJerseyNumber[i] = newJersey;
playerRating[i] = newRating;
}
}
break;
}
case 'o':
{
printf("ROSTER\n");
for (i = 0; i < 5; i++)
{
printf("Player %d -- Jersey number: %d, Rating: %d\n", (i + 1), playerJerseyNumber[i], playerRating[i]);
}
break;
}
default:
printf("didnt work");
break;
}
return 0;
}
now, the first part of my code works correctly. however, if i try to use any of the options in the menu, they do not work. it automatically goes to the default case and prints "didn't work".
right now, i am testing with
84 7
23 4
4 5
30 2
66 9
u
4
6
o
q
which does not work, even though it should update jersey #4 to a rating of 6.
Any ideas on why this isn't working? thanks.

use scanf(" %c", &choice)
The blank in the format string tells scanf to skip leading whitespace, and the first non-whitespace character will be read with the %c conversion specifier.
Refer Here

Related

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.

How to jump back to the top of the code after if statement C language

I'm trying to make my program to start again from the beginning after user has selected an option from a menu.
When the user selects 1 and then enters the amount of the donation I want the program to start again and show the menu What would you like to do? and not to just restart just the if statement.
Should I add a for loop inside of the if statement to accomplish this? thanks for the help.
printf("What would you like to do?\n");
printf(" 1 - Donate\n");
printf(" 2 - Invest\n");
printf(" 3 - Print balance\n");
printf(" 4 - Exit\n");
printf("\n");
//scans menu choice
scanf("%d", &menu_option);
if(menu_option==1)
{
printf("How much do you want to donate?\n");
scanf("%lf", &donation);
donations_made++;
current_Balance = initial_Balance + donation;
}
When the user selects 1 and then enters the amount of the donation I want the program to start again and show the menu
just do
for(;;) {
printf(" 1 - Donate\n");
printf(" 2 - Invest\n");
printf(" 3 - Print balance\n");
printf(" 4 - Exit\n");
printf("\n");
//scans menu choice
scanf("%d", &menu_option);
if(menu_option==1)
{
printf("How much do you want to donate?\n");
scanf("%lf", &donation);
donations_made++;
current_Balance = initial_Balance + donation;
// NO BREAK
}
else {
.... management of other cases
break;
}
}
or if you prefer
do {
printf(" 1 - Donate\n");
printf(" 2 - Invest\n");
printf(" 3 - Print balance\n");
printf(" 4 - Exit\n");
printf("\n");
//scans menu choice
scanf("%d", &menu_option);
if(menu_option==1)
{
printf("How much do you want to donate?\n");
scanf("%lf", &donation);
donations_made++;
current_Balance = initial_Balance + donation;
}
// ... management of other cases
} while (menu_option==1);
But are you sure you do not want to redo also in cases 2 and 3 ? In that case replace while (menu_option==1); by while (menu_option != 4); or in the first proposal do the break only when menu_option is 4
I also encourage you to check the return value of scanf("%d", &menu_option); to be sure a valid integer was given in input and menu_option was set
#include <stdio.h>
int main()
{
int menu_option;
double donation;
int donations_made = 0;
int current_Balance = 0;
int initial_Balance = 0;
for (;;)
{
printf("What would you like to do?\n");
printf(" 1 - Donate\n");
printf(" 2 - Invest\n");
printf(" 3 - Print balance\n");
printf(" 4 - Exit\n");
printf("\n");
//scans menu choice
scanf("%d", &menu_option);
if (menu_option==1)
{
printf("How much do you want to donate?\n");
scanf("%lf", &donation);
donations_made++;
current_Balance = initial_Balance + donation;
}
else if (menu_option == 4)
break;
}
}

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.

Searching through struct array for string entered by user

This is just a function from my program that is supposed to search for the string or integer the user enters in the struct array. What I'm trying to do is add an error message and the ability to try again when that entered string or integer is not found in the struct array. It searches just fine if you enter the correct string or integer but nothing happens if you don't. I'm trying to change that but can't find the solution.
I've tried for a while now with one of the cases in my switch statement but I need to do it for all three. But so far I've only tried on case 3.
search(struct items aItems[], int *num_items)
{
int choice_of_search, b=0, found_word=0, search_num, i=0, j=0, k=0;
char search_phrase[20]; struct items search[MAX];
printf("Choose what to search for? (1) Item number, (2) Name and (3) Balance. ");
scanf("%d", &choice_of_search);
while(choice_of_search < 1 || choice_of_search > 3)
{
printf("Wrong choice!\n");
printf("Choose what to search for? (1) Item number, (2) Name and (3) Balance. ");
scanf("%d", &choice_of_search);
}
switch(choice_of_search)
{
case 1:
printf("Item number?\n");
scanf("%d", &search_num);
for(i = 0; i < *num_items; i++)
{
if(search_num == aItems[i].itemnumber)
{
printf("Item number found!\n");
search[found_word]=aItems[i];
found_word+=1;
}
}
break;
case 2:
printf("Name?\n");
scanf("%s", search_phrase);
for(i = 0; i < *num_items; i++)
{
if(strstr(aItems[i].name, search_phrase))
{
printf("Name found!\n");
search[found_word]=aItems[i];
found_word+=1;
}
}
break;
case 3:
printf("Balance?\n");
scanf("%d", &search_num);
for(i = 0; i < *num_items; i++)
{
if(search_num == aItems[i].balance)
{
printf("Balance found!\n");
search[found_word]=aItems[i];
found_word+=1;
}
else
{
printf("Balance not found! Try again.\n");
printf("Balance?\n");
scanf("%d", &search_num);
}
}
break;
}
while(b < found_word)
{
printf("Item number: %d Name: %s Balance: %d\n", search[b].itemnumber, search[b].name, search[b].balance);
b++;
}
}
Maybe this can help
int done;
...
...
case 3:
done = 0;
while(1);
{
printf("Balance?\n");
scanf("%d", &search_num);
for(i = 0; i < *num_items; i++)
{
if(search_num == aItems[i].balance)
{
printf("Balance found!\n");
search[found_word]=aItems[i];
found_word+=1;
done = 1;
}
}
if (done) break;
printf("Balance not found! Try again.\n");
}
break;
But notice that the code isn't user friendly as it doesn't allow the user a way to stop the search without a match. So maybe you should consider adding a "Would you like try again" option.
A simple approach could be
printf("Balance not found! Would like to try again?.\n");
scanf(" %c", &some_char);
if (some_char != 'y') break;

Function do not return proper value

The function converterm(met, bri); when called is not returning proper values. The code is still incomplete but it works for some options. Just type-in the values and whenever asked which option to select, select option 1 and see the results.
At this line conm.m = conb.ft / 3.2808; it do not return expected values.
//METRIC_BRITISH - BUILD 1.0
#include<stdio.h>
#include<stdlib.h>
#include<process.h>
//GLOBAL-STRUCTURES DECLARATION
struct metric
{
float m;
float cm;
};
struct british
{
float ft;
float in;
};
//GLOBAL-STRUCTURE-VARIABLE DECLARATION
struct metric met = { 0,0 };
struct british bri = { 0,0 };
int q = 0;
int w = 0;
//USER-DEFINED FUNCTION
struct metric converterm(struct metric conm, struct british conb);
struct british converterb(struct british conb, struct metric conm);
void header();
void header()
{
printf("*-*-*-*-*METRIC_BRITISH*-*-*-*-*");
printf("\n\n");
}
//PROGRAM STARTS HERE
main()
{
//VARIABLE-DECLARATION
int n = 0, c = 0, b = 0, v = 0, i = 0;
//FUNCTION CALL-OUT
header();
printf("Format : Metric \n\n");
printf("Enter the Value for Metres : ");
scanf_s("%f", &met.m);
printf("\n");
printf("Enter the Value for Centimetres : ");
scanf_s("%f", &met.cm);
printf("\n");
printf("*--------------------------------------------*\n\n");
printf("Format : British \n\n");
printf("Enter the Value for Feet : ");
scanf_s("%f", &bri.ft);
printf("\n");
printf("Enter the Value for Inches : ");
scanf_s("%f", &bri.in);
printf("\n\n");
printf("In which Format would you like to add other value? \n");
printf("1. Metric \n");
printf("2. British \n");
printf("Enter any Option : ");
while (i == 0)
{
printf("\n");
scanf_s("%d", &n);
switch (n)
{
case 1:
printf("In which Unit you want to add value? \n");
printf("1. Metres \n");
printf("2. Centimetres \n");
printf("Enter any Option : ");
scanf_s("%d", &c);
q = c;
met = converterm(met, bri);
i = 1;
break;
case 2:
printf("In which Unit you want to add value? \n");
printf("1. Feet \n");
printf("2. Inch \n");
printf("Enter any Option : ");
scanf_s("%d", &c);
q = c;
bri = converterb(bri, met);
i = 1;
break;
default:
printf("INVALID OPTION. \n");
printf("Please Enter Correct Option.");
i = 0;
break;
}
}
printf("Values for Metric : \n");
printf("Metres = %d \n", met.m);
printf("Centimetre = %d \n", met.cm);
printf("\n*--------------------------------------------*\n\n");
printf("Values for British : \n");
printf("Feet = %d \n", bri.ft);
printf("Inch = %d \n", bri.in);
//TERMINAL-PAUSE
system("pause");
}
struct metric converterm(struct metric conm, struct british conb)
{
int i = 0;
switch (q)
{
case 1:
printf("\n");
printf("Would you like to Add? \n");
printf("1. Add Feet to Metre \n");
printf("2. Add Inch to Metre \n");
printf("Enter any Option : ");
scanf_s("%d", &i);
break;
case 2:
printf("\n");
printf("Would you like to Add? \n");
printf("1. Add Feet to Centimetre \n");
printf("2. Add Inch to Centimetre \n");
printf("Enter any Option : ");
scanf_s("%d", &i);
break;
}
if (i == 1)
{
conm.m = conb.ft / 3.2808;
//conm.m = conb.in / 39.370;
}
else
{
conm.cm = conb.ft / 0.032808;
//conm.cm = conb.in / 0.39370;
}
return(conm);
}
struct british converterb(struct british conb, struct metric conm)
{
int i = 0;
switch (w)
{
case 1:
printf("\n");
printf("Would you like to Add? \n");
printf("1. Add Metre to Feet \n");
printf("2. Add Centimetre to Feet \n");
printf("Enter any Option : ");
scanf_s("%d", &i);
break;
case 2:
printf("\n");
printf("Would you like to Add? \n");
printf("1. Add Metre to Inch \n");
printf("2. Add Centimetre to Inch \n");
printf("Enter any Option : ");
scanf_s("%d", &i);
break;
}
if (i == 1)
{
conb.ft = conm.m*3.2808;
//conb.ft = conm.cm*0.032808;
}
else
{
conb.in = conm.m*39.370;
//conb.in = conm.cm*0.39370;
}
return(conb);
}
The problem is in the part where you try to print out the values. In case of
printf("Metres = %d \n", met.m);
printf("Centimetre = %d \n", met.cm);
and
printf("Feet = %d \n", bri.ft);
printf("Inch = %d \n", bri.in);
You're using %d format specifier to print the value of the variables of float types. You should be using %f instead.
FWIW, using inappropriate type of argument for a format specifier invokes undefined behavior.

Resources