Switch-Case Operator -- C - c

Is there a way to store user inputs in switch case from one operation and use it across switch operations at run-time.
Example: If its a software for a Bank and I want to take information from the user and validate if his a/c number is correct and also check if he has enough bank balance to withdraw money.
I need to know how to store the value of one operation,so that I could use it for further ops.
switch(ops)
{
char ac_no;
long amt,amt2,init_dep;
char name,ac_allocated;
case OpenAC:
{
printf("1.Name:\n");
scanf("%s",&name);
printf("2.A/Cno_allocated:\n");
scanf("%s",&ac_allocated);
printf("3.Initial deposit:\n");
scanf("%d",&init_dep);
break;
}
case Deposit:
{
printf("Enter the a/c number: ");
scanf("%s",&ac_no);
printf("Amount:Rs. ");
scanf("%ld",&amt);
break;
}
case Withdraw:
{
printf("Enter the a/c number: ");
scanf("%s",&ac_no);
printf("Amount:Rs. ");
scanf("%ld",&amt2);
{printf("Cannot withdraw.Rs.500 minimum balance mandatory.\n");}
break;
}
return ops;
}
I also tried declaring variables in the switch(ops) to store the value in them(like in the following case to validate the a/c number in the next step but it doesn't help.)
Edited code:
`
char ac_no;
long amt,amt2,init_dep,dep1;
char name,ac_allocated,ac1;
case OpenAC:
{
printf("1.Name:\n");
scanf("%s",&name);
printf("2.A/Cno_allocated:\n");
scanf("%s",&ac_allocated);
ac_allocated = ac1;
printf("3.Initial deposit:\n");
scanf("%d",&init_dep);
init_dep = dep1;
//break;
}
case Deposit:
{
printf("Enter the a/c number: ");
scanf("%s",&ac_no);
if(ac_no == ac1)
{
printf("Amount:Rs. ");
scanf("%ld",&amt);
}
break;
`

Why not declare your variables outside the switch. You could even put braces around the switch to prevent the variables from leaking to the surrounding function, like this:
// code from the surrounding function
{
char ac_no;
long amt,amt2,init_dep;
char name,ac_allocated;
switch(ops)
{
case OpenAC:
...
}
} // end of block

I'm not sure I understand your problem, but if you declare something inside the curly brackets of the switch statement, it will be out of scope when you hit the ending curly bracket and not usable the next time the switch statement is encountered.

First issue: you're using the wrong type for ac_no, name, ac_allocated, and ac1. You're obviously wanting to store strings at these locations, so instead of a plain char, you need to declare arrays of char:
char ac_no[AC_NO_SIZE];
char name[NAME_SIZE];
char ac_allocated[AC_ALLOCATED_SIZE];
char ac1[AC1_SIZE];
where each *_SIZE is large enough to hold your input data plus 1 extra character for the 0 terminator. IOW, if your account numbers are at most 10 characters long, AC_NO_SIZE needs to be 11.
Second issue: do not declare variables at the head of a switch statement; any initializations will be skipped.
Third issue: auto variables declared inside a specific scope will not be available outside of that scope; they will cease to exist when that scope exits. None of the variables you declare will be available outside of the switch statement.
Fourth issue: if this switch operation is inside of a function, and you want to preserve these values between function calls, you can do one of three things:
Declare these items at file scope, outside of any function (not recommended):
char ac_no[AC_NO_SIZE];
char name[NAME_SIZE];
char ac_allocated[AC_ALLOCATED_SIZE];
char ac1[AC1_SIZE];
long amt, amt2, init_dep;
void foo()
{
int ops;
...
ops = bar(ops);
...
}
int bar(int ops)
{
switch(ops)
{
case OpenAC:
printf("Name: ");
fflush(stdout);
scanf("%s", name); // note no &; true for all char [] types
printf("A/C no allocated: ");
fflush(stdout);
scanf("%s", ac_allocated);
printf("Initial deposit: ");
fflush(stdout);
scanf("%ld", &init_dep);
...
}
return ops;
...
Declare these items as `static` in the function (slightly less recommended); they will not be visible outside of the function, but their value will be retained between function calls:
int bar(int ops)
{
static char ac_no[AC_NO_SIZE];
static char name[NAME_SIZE];
static char ac_allocated[AC_ALLOCATED_SIZE];
static char ac1[AC1_SIZE];
static long amt, amt2, init_dep;
switch(ops)
{
case OpenAC:
printf("Name: ");
fflush(stdout);
scanf("%s", name);
printf("A/C no allocated: ");
fflush(stdout);
scanf("%s", ac_allocated);
printf("Initial deposit: ");
fflush(stdout);
scanf("%ld", &init_dep);
...
}
return ops;
}
Declare these items in the calling function and pass them as parameters to this function (recommended):
int foo(int ops,
char *ac_no,
char *name,
char *ac_allocated,
char *ac1,
long *amt, // Since these values are being modified in the function,
long *amt2, // we must pass pointers to them, otherwise the changes
long *init_dep) // will not be reflected in the calling function
{
switch(ops)
{
case OpenAC:
printf("Name: ");
fflush(stdout);
scanf("%s", name); // no & before name; same is true for rest of parameters
printf("A/C no allocated: ");
fflush(stdout);
scanf("%s", ac_allocated);
printf("Initial deposit: ");
fflush(stdout);
scanf("%ld", init_dep); // no & before init_dep since it's already a pointer
...
All of this assumes I'm understanding your problem correctly.

Maybe you should use structure to hold account data and functions to make it readable and maintainable.
struct account_s { /* Fields borrowed to #John Bode */
char ac_no[AC_NO_SIZE];
char name[NAME_SIZE];
char ac_allocated[AC_ALLOCATED_SIZE];
char ac1[AC1_SIZE];
long amt, amt2, init_dep;
};
int openAC(struct account_s *account);
to be continued... :)

Related

Having problems displaying structure array content

I want to display structure members based on user input, but I don't know if I've stored the input properly.
When I try display all people, it just outputs random numbers.
These are the structures and function prototypes
#define MAX_NAME_LEN 15
#define MAX_NUM_PERSON 4
#define MAX_JOB_LENGTH 20
typedef struct birth_date
{
int month;
int day;
int year;
} person_birth_t;
typedef struct person
{
char pName[MAX_NAME_LEN];
char job[MAX_JOB_LENGTH];
person_birth_t birth_t;
} person_t[MAX_NUM_PERSON];
void print_menu (void);
void scanPerson(person_t p, int);
void displayPeople(person_t p);
This is the main code for the program, a menu is printed asking user to input a number, if a user enters 1 then it prompts them to add a person. Entering 2 displays all people entered.
int main(void)
{
/* TODO */
print_menu();
return 0;
}
void print_menu (void)
{
int choice;
person_t p;
static int index = 0;
int *indexP = NULL;
indexP = &index;
/*Print the menu*/
scanf("%d", &choice);
switch (choice)
{
case 1:
if (index < MAX_NUM_PERSON){
scanPerson(p, index);
++*indexP;
print_menu();
} else {
printf("Can't add more people - memory full \n");
print_menu();
}
break;
case 2:
displayPeople(p);
break;
case 3:
exit(0);
break;
default:
print_menu();
}
}
/*function called when add person is chosen from menu */
void scanFlight(person_t p, int index){
/*printf to enter name*/
scanf(" %s", p[index].pName);
/*printf to enter job*/
scanf("%s", p[index].job);
}
void displayPeople(person_t p){
for(int i = 0; i < MAX_NUM_PERSON; i++){
printf("%s %d-%d-%d %s \n",p[i].pName
,p[i].birth_t.month
,p[i].birth_t.day
,p[i].birth_t.year
,p[i].job);
}
}
I've tried other ways to take input and add it to a struct array, but I'm just not sure how to do it right.
person_t p;
Here, you use the local variable p (in print_menu function), so each recursion, you just print the parameters of the local variable that is not initialized.
To solve it, you can declare p as the global variable.
OT, in scanFlight function, to avoid overflow, you should change the scanf function to:
/*printf to enter name*/
scanf("%14s", p[index].pName);
/*printf to enter job*/
scanf("%20s", p[index].job);
And, rename scanPerson to scanFlight, because i do not see any implementation of scanPerson function in your code. I think it's typo, no ?
None of the methods were working, so instead of trying to figure it out, I scrapped the static index and indexP.
Instead, I initialized p with malloc:
person_t *p= malloc(MAX_NUM_PERSON * sizeof(person_t));
I changed the scan function to accommodate for the change and made index a pointer instead, and I made the display function pass the index.
When I ran it, the output was correct.

Trying to print a struct element but getting blank. - C

I have a struct person that has the following elements, defined in data.h
typedef struct person{
char firstName[20];
char familyName[20];
char telephoneNum[20];
int type; // 0 = student / 1 = employee;
}newPerson;
I created an array of person[MAX_PERSONS] that is initialized in my menu() function. I then have an addFirstName(newPerson pers) function. However when I try to test print format using my printFormat(newPerson pers)function, I get blank, instead of the inputted name.
I have included the menu(), addFirstname(newPerson pers), and printFormat(newPerson pers) function below. I was wondering if anyone could tell me the reason for this. Any help or pointers would be appreciated. Thank you in advance.
int menu(){
int num = 0;
newPerson person[MAX_PERSONS];
int option; // for user input for menu
printf("\n\tPlease choose one of the following options to continue (0-9): ");
scanf("%d", &option );
printf("\n\tYou selected %d\n", option);
if (option == 0){ //program will close
printf("\tProgram will now close.\n");
exit(1);
}
if (option == 1){ //program will ask for name input
addRecord(person[num]);
printFormat(person[num]);
char choice[0];
printf("\n\t\tWould you like to enter another record? (y/n): ");
scanf("%s", choice);
if (choice[0] == 'y'){
num++;
addRecord(person[num]);
}
if (choice[0] == 'n'){
num++;
mainMenu();
}
/*
IF YES, THEN NUM++
THEN RUN ADDRECORD(PERSONNUM) AGAIN.
IF NO, THEN RETURN TO MAIN MENU.
PRINTMENU
THEN RUN MENU AGAIN
*/
}
printf("\n\tNot a valid option, please try again // THE END OF MENU FUNCTION\n");
return 0;
}
void addFirstName(newPerson pers){
char firstName[20];
printf("\n\tEnter first Name: ");
scanf("%20s", firstName);
strcpy(pers.firstName, firstName);
printf("\n\tThe name entered is %s", pers.firstName);
}
void printFormat(newPerson pers){
printf("\t\tThe name is %s", pers.firstName);
}
It's because you pass the structure to addFirstName by value meaning that the function receives a copy of the structure. And changing a copy will of course not change the original.
While C does not support passing arguments by reference, it can be emulated using pointers. So change the addFirstName function to receive a pointer to the structure as its argument.
Your big problem is that you are passing structures by value instead of by pointers. This result in that you change copies of original objects inside your function addFirstName and not the original object. You should declare it as:
void addFirstName( newPerson* pers);
void printFormat( newPerson* pers);
Example:
#include <stdio.h>
void call_by_value(int x) {
printf("Inside call_by_value x = %d before adding 10.\n", x);
x += 10;
printf("Inside call_by_value x = %d after adding 10.\n", x);
}
int main() {
int a=10;
printf("a = %d before function call_by_value.\n", a);
call_by_value(a);
printf("a = %d after function call_by_value.\n", a);
return 0;
}
this will produce:
a = 10 before function call_by_value.
Inside call_by_value x = 10 before adding 10.
Inside call_by_value x = 20 after adding 10.
a = 10 after function call_by_value.

Defining functions that return a char used later in the main program

I am having a lot of trouble wrapping my head around calling functions and using them in the main program again later. I have not found an answer in depth to explain why this doesn't run. I understand that parameters belong inside of the called function parentheses, but I want the user input to begin in the called program. Is this even possible? Theoretically, the function would ask the user for a year, check that it is within certain parameters, then return it to the main function where I would like to eventually be able to store it in an array. For now, can someone please show me how I would make that work in this elementary program? Thank you in advance!
#include <stdio.h>
char year_info();
int main(void)
{
int menu_selection;
char year;
printf("Please choose from the following menu: \n1. Insert a new movie\n2. Show movie\n3. List all\n4. Exit\n");
scanf("%i", &menu_selection);
switch (menu_selection)
{
case 1: year = year_info();
printf("%c", year);
break;
}
}
char year_info()
{
int year_input;
printf("\nYear: ");
scanf("%i", &year_input);
if (year_input > 2016 || year_input < 1920)
{
printf("Sorry, I do not recognize this command. Please try again.\n");
}
else
{
int year = year_input;
return year;
}
}
It doesn't run because you're passing scanf the variable, but you should pass the address of the variable, i.e. use:
scanf("%i", &something);
instead of scanf("%i", something);
Also, as others pointed out, you're mixing char and int too liberally, so it won't work as expected.
year and year_imput can't be chars because they won't hold values large enough, you'll need at least a short.
You had 2 errors.
scanf("%i", &menu_selection);
scanf("%i", &year_imput);
You need to use the & to pass the address of the variables to scanf().
Edit: However, I would have used an integer for that, because a scanf("%c", &something) will only recognize the first char you enter, and not the whole string, even if that happened you can't do if (year_imput > 2016 || year_imput < 1920) between strings, you can do that with chars, but again, they can only store one character, so I would have done your program like this.
#include <stdio.h>
int year_info();
int main() {
int menu_selection;
int year;
printf("Please choose from the following menu: \n1. Insert a new movie\n2. Show movie\n3. List all\n4. Exit\n");
scanf("%i", &menu_selection);
switch (menu_selection) {
case 1:
year = year_info();
printf("%i", year);
break;
default:
break;
}
return 0;
}
int year_info() {
int year_imput;
printf("\nYear: ");
scanf("%i", &year_imput);
if (year_imput > 2016 || year_imput < 1920) {
printf("Sorry, I do not recognize this command. Please try again.\n");
return 0;
}
else
return year_imput;
}

Taking in unique inputs in a struct array in C

I am writing a program to create a structure named 'student'. I need to input various data about a particular student. Here is my program till now.
#include<stdio.h>
#include<stdlib.h>
struct student
{
char* name;
int id;
float marks_1;
float marks_2;
};
void main()
{
int num, var_i, var_j, var_k, var_l, duplicated_id = 0;
printf("Enter number of students\n");
scanf("%d", &num);
struct student s[num];
printf("Enter the data for the students\n");
for (var_i = 0; var_i < num; var_i++)
{
var_j = var_i + 1;
printf("Enter name of student_%d\n", var_j);
scanf(" %[^\n]%*c", &s[var_i].name);
printf("Enter id of student_%d\n", var_j);
scanf("%d", &s[var_i].id);
for (var_k = 0; var_k < var_i; var_k++)
{
if (s[var_k].id == s[var_i].id)
{
printf("Duplicate Id, program will exit");
return;
}
}
printf("Enter marks(sub_1) of student_%d\n", var_j);
scanf("%d", &s[var_i].marks_1);
printf("Enter marks(sub_2) of student_%d\n", var_j);
scanf("%d", &s[var_i].marks_2);
}
}
In the following for loop I am checking all the previously entered 'id' values to check if there is a duplicate. In case of a duplicate, the program will exit.
for(var_k=0;var_k<var_i;var_k++)
{
if(s[var_k].id==s[var_i].id)
{
printf("Duplicate Id, program will exit");
return;
}
}
Now instead of exiting the program I want to prompt the user to enter a different value. This goes on till he enters a unique value. How should I do it?
Any help appreciated.
This is wrong:
scanf(" %[^\n]%*c", &s[var_i].name);
You're passing the address of the pointer member name (i.e. you're passing a char **) to scanf() which per the format string, is expecting a char* and enough memory to hold the data it subsequently reads. This is invalid, is undefined behavior, and blindly overwrites data in the s[] array. Frankly I'm amazed this doesn't seg-fault your process.
Change this:
struct student
{
char* name;
int id;
float marks_1;
float marks_2;
};
To this:
struct student
{
char name[128]; // or some other suitable size.
int id;
float marks_1;
float marks_2;
};
And change this:
scanf(" %[^\n]%*c", &s[var_i].name);
To this:
scanf(" %[^\n]%*c", s[var_i].name);
I strongly suggest a size-limiter on that scanf() call as well, but I leave that to you to discover. Read about the API here.
Just use a loop.
here is some psudocode
bool isDuplicate = false
do
{
GetInput()
isDuplicate = CheckForDuplicate()
}while(isDuplicate);

When I run this program, after entering a name, I get a segmentation fault. How can I fix this to make it work properly?

Restauraunt.c
This program allows you to create a restaurant menu, stores it in a file, and then rates each item on the menu. It uses file functions to output everything into a file that can then be viewed through almost any program. When the program gets to the line in nametofile() 'fprintf(restauraunt, "%s Restauraunt\n\n",name);' the program gives a segmentation fault. I do not know why it is doing this, I have attempted several different methods of debugging, but none have worked. If you have any suggestions, please comment them below.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
FILE *restauraunt;
char name[20];
char item[20];
char price[20];
int count=0;
void nametofile();
void rate();
void itemtofile();
void counter();
void renamefile();
int main()
{
int i,j;
int num;
printf("Restauraunt Creator\n\n");
printf("Enter the name of your restauraunt:\n");
scanf("%s",&name);
nametofile();
printf("\nEnter the number of items to be included in your menu:\n");
scanf("%d", &num);
/* Cycles through each entry to the menu */
for(i=0;i<num;i++)
{
counter();
fpurge(stdin);
printf("\nPlease enter the name of item number %d:\n",count);
scanf("%s", &item);
printf("\nPlease enter the price of item number %d:\n",count);
scanf("%s", &price);
itemtofile();
rate();
}
renamefile();
}
/*void nametofile()
{
restauraunt = fopen("restauraunt","w");
fprintf(restauraunt, "%s Restauraunt\n\n",name);
fclose(restauraunt);
}*/
/* The function that sends the restaurant name to the file */
void nametofile()
{
int i;
i = strlen(name);
name[i+1] = '\0';
restauraunt = fopen("restauraunt","w");
/* the line that gives a segmentation fault */
fprintf(restauraunt, "%s Restauraunt\n\n",name);
fclose(restauraunt);
}
/* rates each menu item */
void rate()
{
int rating;
srandom((unsigned)time(NULL));
restauraunt = fopen("restauraunt", "a");
rating = random() % 5 + 1;
fprintf(restauraunt,"Your food's rating was:\t%d stars!",rating);
switch(rating)
{
case 1:
{
fprintf(restauraunt," Here's why: Your food was not very good tasting and the price was ridiculously high.\n");
break;
}
case 2:
{
fprintf(restauraunt," Here's why: Your food was mildly good tasting and the price was too high.\n");
break;
}
case 3:
{
fprintf(restauraunt," Here's why: Your food was somewhat good tasting and the price was fair.\n");
break;
}
case 4:
{
fprintf(restauraunt," Here's why: Your food was quite good tasting and the price was very nice.\n");
break;
}
case 5:
{
fprintf(restauraunt," Here's why: Your food was very delicious and the price was amazingly low.\n");
break;
}
}
}
/* sends each item to the file */
void itemtofile()
{
restauraunt = fopen("restauraunt","a");
fprintf(restauraunt, "%s: $%s\nRating:",item,price);
fclose(restauraunt);
}
/* counts up one each time function is called */
void counter()
{
count += 1;
}
/* renames the file at the end */
void renamefile()
{
int x,y;
char bridge[] = { "menu" };
name[0] = tolower(name[0]);
x = strcat(name,bridge);
y = rename("restauraunt",name);
}
name is a char array. When you pass it to scanf or other functions, it decays to a pointer, so you do not need the & operator:
scanf("%19s", name);
When you read strings with scanf, it is a good idea to pass the size limit: this lets you avoid buffer overruns. Since name is declared as char[20], you pass 19, because one more char needs to be reserved for the null terminator.

Resources