I'm starting coding this week so I'm dummy about it. I need help about return to main in my script. For example when I done Course registration part I can't return menu program is crashing
Codes:
#include <stdafx.h>
#include <stdio.h>
void eng();
void menu();
void huh();
int main()
{
menu();
return 0;
}
void menu()
{
int menu1choice;
printf("Menu\n");
printf("\n");
printf("1. Student Registration\n");
printf("2. Show Students.\n");
printf("Please enter number: ");
scanf_s("%d", &menu1choice);
switch (menu1choice)
{
case 1:
{
eng();
break;
}
}
}
void eng()
{
int a = 5;
char name[30];
printf("1.Student Number: ");
scanf_s("%d", &a);
//student number
printf("2.Name: ");
scanf_s("%s", &name);
//student name
getchar();
}
void huh()
{
int a = 5;
char name[30];
printf("Your Student number: %d\n", a);
printf("Your Name: %s\n", name);
//result
getchar();
}
Pls help me write return code lines, Thanks in Advance
Here is some code that may help you understand how to the mechanics of functions returning values, in your case back to the main function.
As for some advice, please read up about magic numbers and specifically why they are bad.
/*
* 35917794_main.c
*/
#include <stdio.h>
#define STU_REG 1
#define STU_SHOW 2
#define EXIT_SUCCESS 0
unsigned int show_menu(void);
unsigned int main
(
unsigned int argc,
unsigned char *arg[]
)
{
unsigned int menu1choice;
/*
* The next statements says run the function show_menu() and put the returned
* result in the variable menu1choice.
*/
menu1choice = show_menu();
switch(menu1choice)
{
case (STU_REG):
{
printf("\nGo do STUDENT REGISTRATION things...\n\n");
break;
}
case STU_SHOW:
{
printf("\nGo do SHOW STUDENT things...\n\n");
break;
}
default:
{
printf("\nGo do something for invalid option...\n\n");
break;
}
}
return(EXIT_SUCCESS);
}
unsigned int show_menu
(
void
)
{
unsigned int ui_W0;
printf("Menu\n\n");
printf("1. Student Registration\n");
printf("2. Show Students.\n");
printf("Please enter number: ");
scanf("%d", &ui_W0);
/*
* The next statements says run the function show_menu() has finished and returned
* returns the result in the variable ui_W0.
*/
return(ui_W0);
}
Related
I'm new to C programming and am trying to do a booking system for my assignment. I've been faced with this error multiple time with no idea how to solve it.
I've tried what Visual Studio recommend which is to change the scanf to scanf_s. However, this does not work either. As for the function unidentified error I have no idea on where to fix it.
#include <stdio.h>
#include <stdlib.h>
char MainMenu()
{
int sel;
sel = 1, 2, 3, 4;
printf("WELCOME TO WISDOM COLLEGE HOTEL\n\n");
printf("Please choose your desired service:\n");
printf("1.Booking\n");
printf("2.Check Room Availability\n");
printf("3.Payment detail\n");
printf("4.Exit\n");
if (scanf_s("%d",&sel) == 1) {
return Booking();
}
else if (scanf_s("%d", &sel) == 2) {
return Room();
}
else if (scanf_s("%d", &sel) == 3) {
return Payment();
}
}
char Booking()
{
int selBook;
printf("\t\t\t\t BOOKING\n\n");
printf("Book base on:\n");
printf("1.Gender\n");
printf("2.Room Type\n");
printf("3.Back to Main Menu\n");
scanf_s("%d", &selBook);
printf("\n%d", selBook);
}
char Room()
{
int selRoom;
printf("\t\t\t ROOM AVAILABILITY\n\n");
printf("Check room availability by:\n");
printf("1.Type\n");
printf("2.Gender\n");
if (scanf_s("%d", &selRoom) == 1) {
return 0;
}
}
char main()
{
char MainMenu;
char Booking;
char Payment;
char Room;
return MainMenu;
}
I expect the program to run so the MainMenu run first, and based on user input, it will then go from Booking to Exit.
you should first get the input and then check what it is.
scanf_s doesnt return the value which you entered as input, it's return value is the number of fields which successfully converted and assigned.
So you should first get the input and save it in the memory and then check what it's value.
Also, I didnt know what you tried to do with MainMenu that was the return value of char main() (i dont think its common to use char main - use int or void main istead):
#include <stdio.h>
#include <stdlib.h>
char MainMenu();
char Booking();
char Payment();
char Room();
char MainMenu()
{
int sel;
sel = 1, 2, 3, 4;
printf("WELCOME TO WISDOM COLLEGE HOTEL\n\n");
printf("Please choose your desired service:\n");
printf("1.Booking\n");
printf("2.Check Room Availability\n");
printf("3.Payment detail\n");
printf("4.Exit\n");
scanf_s("%d",&sel)
if (sel == 1) {
return Booking();
}
else if (sel == 2) {
return Room();
}
else if (sel == 3) {
return Payment();
}
}
char Booking()
{
int selBook;
printf("\t\t\t\t BOOKING\n\n");
printf("Book base on:\n");
printf("1.Gender\n");
printf("2.Room Type\n");
printf("3.Back to Main Menu\n");
scanf_s("%d", &selBook);
printf("\n%d", selBook);
}
char Room()
{
int selRoom;
printf("\t\t\t ROOM AVAILABILITY\n\n");
printf("Check room availability by:\n");
printf("1.Type\n");
printf("2.Gender\n");
if (scanf_s("%d", &selRoom) == 1) {
return 0;
}
}
int main()
{
MainMenu();
return 0;
}
I want to create an edit function that receives as a parameter by reference the vector of songs. (using pointers)
The user must choose the song number and re-enter the data of that position of the vector.
I created the struct, I am already receiving the values and I am playing. But I do not know how to edit. Anyone to help me start this part?
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <locale.h>
#include <string.h>
struct registry_of_music {
char name[50];
char artist[60];
char url[80];
};
struct registry_of_music music[9];
int main() {
int i;
printf("\nRegistry of Music\n\n\n");
for(i = 0; i <= 3;i++ ){
printf("Name of Music: ");
fflush(stdin);
fgets(music[i].name, 50, stdin);
printf("Name of Artist: ");
fflush(stdin);
fgets(music[i].artist, 60, stdin);
printf("URL of Internet: ");
fflush(stdin);
fgets(music[i].url, 80, stdin);
}
int op;
do
{
printf("1 - Play\n");
printf("2 - Edit\n");
printf("3 - Exit\n");
printf("Please enter a value:");
scanf("%d", &op);
switch(op) {
case 1: play();
break;
case 2: edit();
break;
case 3: printf("Bye\n");
break;
default: printf("Try Again\n");
}
} while (op!=3);
getch();
return(0);
}
void play(){
int i;
for(i = 0; i <= 3;i++ ){
printf("Name ...........: %s", music[i].name);
printf("Artist .....: %s", music[i].artist);
printf("URL .....: %s", music[i].url);
}
}
void edit(){}
The «fill instance of structure» action is absolutely identical if performing on uninitialized structure or initialized. Even if an instance is not initialized, it has some rubbish values in its fields.
On the other hand there is no way to specify default value which will be shown in fgets's prompt and will be available for keyboard edit, unless you're using much more complicated (and NOT included in ISO C standard) tools.
#include <stdio.h>
#include <conio.h>
char save ();
char save()
{
char name[30];
char surname[30];
char number[12];
printf("Name : \n");
scanf("%d",&name);
printf("Surname : \n");
scanf("%d",&surname);
printf("Number :\n");
scanf("%d",&number);
}
int main()
{
while(1)
{
int a;
printf("please select ");
scanf("%d",&a);
switch(a)
{
case 1 : save(); break;
default : printf("ok."); break;
}
break;
}
}
when i run save() function with pressing 1, it prints all statements together not one by one.
for example in this code output is:
(first output)1- Name : (i enter my name)
(second output)2- Surname:
Number :
i would like to run save function's code one by one. how can i fix it?
You have to change %d (integer) in your scanf to %s (string) in your char save(); function. We use %d when we need to read integer like %c for char , %f for float etc. %s is used to read string (char array) .Your code is not properly intended .Also you do not return anything from char save(); function , whose return type is char. If you are not returning any values the you could make return type void.
In case of reading strings you don't need & operator.Like scanf("%s", name);.
Modified code :-
#include <stdio.h>
#include <conio.h>
char save();
char save()
{
char name[30];
char surname[30];
char number[12];
printf("Name : \n");
scanf("%s", name); //change
printf("Surname : \n");
scanf("%s", surname); //change
printf("Number :\n");
scanf("%s", number); //change
}
int main()
{
while (1)
{
int a;
printf("please select ");
scanf("%d", &a);
switch (a)
{
case 1:
save();
break;
default:
printf("ok.");
break;
}
break;
}
}
Output :-
please select 1
Name :
abc
Surname :
def
Number :
123
new to programming here :3
(Don't check my profile, I actually only know C, for now...)
I need help here, I dunno what's causing the problem, but whenever I repeat the program, it skips asking for a replacement value in
char C1.name
in function
void Name()
{
p("ENTER NAME: ");
gets(C1.name);
p("CONTACT DETAILS: ");
s("%d", &C1.cont_no);
}
in my Program
#include <stdio.h>
#include <windows.h>
#define p printf
#define s scanf
void Name();
void Order();
void Total();
void Receipt();
char Repeat();
/* CUSTOMER DETAILS */
struct CustomerOrder
{
char name[50];
long int cont_no;
int qty;
float price, total;
} C1;
main()
{
char cont;
/* Program Process... */
do{
Name();
Order();
Total();
Receipt();
cont = Repeat();
}while(cont == 'Y');
}
void Name()
{
/* ENTER NAME AND CONTACT DETAILS */
p("ENTER NAME: ");
gets(C1.name);
p("CONTACT DETAILS: ");
s("%d", &C1.cont_no);
}
void Order()
{
/* ENTER ORDERS */
p("HOW MANY ORDERS: ");
s("%d", &C1.qty);
C1.price = 59.99;
}
void Total()
{
/* TOTAL */
C1.total = C1.price * C1.qty;
p("TOTAL IS: %.2f", C1.total);
system("pause");
}
void Receipt()
{
system("cls");
/*PRINTED RECEIPT SAMPLE */
p("NAME IS: %s\n", C1.name);
p("CONTACT DETAILS: %d\n", C1.cont_no);
p("QTY: %d\n", &C1.qty);
p("PRICE EACH: %.2f\n", C1.price);
p("TOTAL PAYOUT: %.2f\n", C1.total);
}
char Repeat()
{
/* ASKS USER TO REPEAT PROGRAM, THEN RETURN VALUE TO BE USED BY function
main */
char repeat;
p("REPEAT?: ");
s("%s", &repeat);
return repeat;
}
There may be other problems you would probably notice (which I dunno if there are, but I would like it NOT to skip the Name part and stuff...
You only want to get a character when asking the user to repeat so change
s("%s", &repeat);
to
s(" %c", &repeat);
if you add a space ^ you will skip the \n that you entered previously.
For my intro to programming class, we have to code a phonebook in C that lets users add contacts, as well as delete and display them. It also has to allocate and free memory as necessary (I tried to do this, but I honestly don't really know what I'm doing).
Anyway, I cannot figure out how to add a contact to the phonebook. I've pasted the relevant part of the program so far. It compiles, but it crashes every time I try to add a contact. Once I get this figured out, I think I can get the rest of the functions without too much trouble. If anyone could help me out, I'd really appreciate it.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct entry {
char fname[20];
char lname[20];
char pnumber[20];
} entry;
// function prototypes
void addentry(int, entry*, char addfname[20], char addlname[20], char addpnumber[20]);
main() {
int selection = 0;
int inputtest = 1;
int pnum = 0; // keeps track of number of contacts
char addfname[20] = { '\0' };
char addlname[20] = { '\0' };
char addpnumber[20] = { '\0' };
entry *pcontacts;
pcontacts = (entry*)calloc(1, (sizeof(entry)));
if (pcontacts == NULL) {
printf("No memory is available.");
free(pcontacts);
return 0;
}
while (1) {
do {
printf("\nPhonebook Menu\n\n");
printf("1:\tAdd contact\n");
printf("2:\tDelete contact\n");
printf("3:\tDisplay contacts\n");
printf("4:\tExit\n");
printf("\nChoose an action (1-4): ");
scanf("%d", &selection);
if (selection < 1 || selection > 4) {
printf("Invalid input. Please enter an integer between 1 and 4.\n");
inputtest = 0;
}
if (selection == 4) {
free(pcontacts);
printf("\nThank you for using this phonebook.");
return 0;
}
switch (selection) {
case 1:
pnum++;
printf("\nEnter first name: ");
scanf("%s", addfname);
printf("Enter last name: ");
scanf("%s", addlname);
printf("Enter phone number (no spaces): ");
scanf("%s", addpnumber);
addentry(pnum, pcontacts, addfname[20], addlname[20], addpnumber[20]);
break;
}
} while (inputtest == 1);
}
}
void addentry(int pnum, entry *pcontacts, char addfname[20], char addlname[20], char pnumber[20]) {
pcontacts = (entry*)malloc(pnum * (sizeof(entry)));
if (pcontacts != NULL) {
strcpy(*pcontacts[pnum - 1].fname, addfname);
printf("\nContact has been added.");
} else {
printf ("No memory is available.\n");
}
}
You get strings from standard input with scanf, but you should tell scanf the maximum number of bytes to store to the destination arrays to avoid buffer overruns:
scanf("%19s", addfname);
...
scanf("%19s", addlname);
...
scanf("%19s", addpnumber);
The way you call addentry is incorrect:
addentry(pnum, pcontacts, addfname[20], addlname[20], addpnumber[20]);
You actually try to read the byte just after the end of addfname, addlname and addpnumber. You should instead pass the arrays themselves, that will be passed to the function addentry as pointers to their first bytes:
addentry(pnum, pcontacts, addfname, addlname, addpnumber);
addentry should reallocate the array with realloc. It should be passed a pointer to the array pointer to it can update the pointer in main.
addentry does not copy the strings correctly: it only copies one, but with a syntax error.
Here is a corrected version:
void addentry(int, entry**, char addfname[20], char addlname[20], char addpnumber[20]);
int main(void) {
int selection = 0;
int inputtest = 1;
int pnum = 0; // keeps track of number of contacts
char addfname[20];
char addlname[20];
char addpnumber[20];
entry *pcontacts = NULL;
for (;;) {
do {
printf("\nPhonebook Menu\n\n");
printf("1:\tAdd contact\n");
printf("2:\tDelete contact\n");
printf("3:\tDisplay contacts\n");
printf("4:\tExit\n");
printf("\nChoose an action (1-4): ");
scanf("%d", &selection);
if (selection < 1 || selection > 4) {
printf("Invalid input. Please enter an integer between 1 and 4.\n");
inputtest = 0;
}
if (selection == 4) {
free(pcontacts); /* OK for NULL */
printf("\nThank you for using this phonebook.");
return 0;
}
switch (selection) {
case 1:
printf("\nEnter first name: ");
scanf("%19s", addfname);
printf("Enter last name: ");
scanf("%19s", addlname);
printf("Enter phone number (no spaces): ");
scanf("%19s", addpnumber);
addentry(pnum, &pcontacts, addfname, addlname, addpnumber);
pnum++;
break;
}
} while (inputtest == 1);
}
}
/* add an entry at position pnum */
void addentry(int pnum, entry **pp, char addfname[20], char addlname[20], char pnumber[20]) {
entry *pcontact = *pp;
pcontacts = realloc(pcontacts, (pnum + 1) * sizeof(entry));
if (pcontacts != NULL) {
*pp = pcontacts; /* update pointer in main */
strcpy(pcontacts[pnum].fname, addfname);
strcpy(pcontacts[pnum].lname, addlname);
strcpy(pcontacts[pnum].pnumber, addpnumber);
printf("\nContact has been added.");
} else {
printf ("No memory is available.\n");
}
}