This is my assignment:
Create a program that allows a user to enter up to 10 addresses of friends. Use a two dimensional array to store the address of friends’. After each address is entered, the user should have the option to enter another address or print out a report that shows each addresses entered thus far.
I get most of it but I am having my main issue with fgets. I keep getting this error:
warning: passing arg 1 of `fgets' makes pointer from integer without a cast
The code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main ()
{
char name[20]= {0};
char address[40]= {100};
int choice;
int i;
printf("Welcome to the Address Book!\n\n");
for (i=0;i<10;i++) //start of the array loop. should give an exit after each entry
{
printf("Would you like to (1)Enter an address, or (2)Print the address book?\n");
scanf("%i",&choice);
switch (choice)
{
case 1:
{
printf("Please enter a name...\n");
fgets(name[i],20,stdin);
printf("You entered %s .", name);
printf("Please enter an address...\n");
fgets(address[i],40,stdin);
printf("You enteres %s .", address);
}
break;
case 2:
for (i = 0; i<10; i ++)
{
printf("%s\n", name[i]);
printf("%s\n", address[i]);
}
break;
}
}
return (0);
}
Find the corrected code in below
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main ()
{
char name[10][20]; //Fix3
char address[10][40];//Fix4
int choice;
int i, j;
printf("Welcome to the Address Book!\n\n");
for (i=0;i<10;i++) //start of the array loop. should give an exit after each entry
{
printf("Would you like to (1)Enter an address, or (2)Print the address book?\n");
scanf("%i",&choice);
switch (choice)
{
case 1:
{
printf("Please enter a name...\n");
getchar(); //Fix1
fgets(name[i],20,stdin);
printf("You entered %s .", name);
printf("Please enter an address...\n");
//getchar();//Fix2
fgets(address[i],40,stdin);
printf("You enteres %s .", address);
}
break;
case 2:
{
for (j = 0; j<i; j ++)
{
printf("%s\n", name[j]);
printf("%s\n", address[j]);
}
}
break;
}
}
return (0);
}
The corrections are:
2D array is used to get the name and address
getchar() is called to consume '\n' before using fgets();
Related
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
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);
}
my code is:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
char names[10][31];
int u=0;
char new[31];
int ctr=1;
int notInList=0;
int y=0;
int i=0;
char *p;
strcpy(names[0],"mahmoud");
while(1)
{
printf("\t\t§§§Menu items§§§\n");
printf("1==>enter a new name\n");
printf("2==>search for a name\n");
printf("3==>delete a name from the list\n");
printf("Note !!.. ((if you want to exit the program press(1)and then type ((exit))\n");
fflush(stdin);
scanf("%i",&u);
notInList=1;
if(u==1)
{
printf("please enter a name : ");
fflush(stdin);
gets(new);
_strlwr(new);
if(strcmp(new,"exit")==0)
{
printf("bye bye\n");
break;
}
else
{
notInList=1;
for(int i=0;i<=ctr;i++)
{
p=strstr(new,names[i]);
if(strcmp(new,names[i])==0)
{
printf("the name is already exist\n");
break;
}
else if (p)
{
printf("did you mean (( %s ))\n",names[i]);
printf("1==>yes\n");
printf("2==>no\n");
fflush(stdin);
scanf("%d",&y);
if(y==1)
{
printf("the name is already exist\n");
break;
}
else if(y==2)
{
notInList=0;
strcpy(new,names[ctr]);
ctr++;
break;
}
else printf("plz enter a number from the list");
}
else
{
notInList=0;
}
}
if(notInList==0)
{
strcpy(new,names[ctr]);
ctr++;
for(int i=0;i<ctr;i++);
{
printf("%d==>%s\n",i+1,names[i]);
}
}
// break;
}
}
return 0;
}
the first problem is: when I enter ( 1) and then add a name is similar to the first name it printf to me did you mean ( )//// without a name
the second is when I want to add a new name it doesn't please help me
notice that he program is not finished only the first choise
My main goal for this code is to capture the users input and do whatever he wants to do with the choices I have presented, but I'm stuck: when I compile, I can only type the word and the program stops working.
i have no idea where I'm making a mistake.
The is my code:
#include <stdio.h>
#include <string.h>
#define MAX_STRING_LENGTH 100
void grab_user_input(void);
void load_menu(void);
void Count_the_letters(void);
int main(void)
{
grab_user_input();
return 0;
}
void grab_user_input(void)
{
char word;
{
printf("Please enter a single word (25 characters or less): \n");
scanf("%s", &word);
printf("Thanks! The word you entered is: %s\n", word);
}
void load_menu(void)
{
int choice;
do
{
int choice;
printf("\n(:===Menu====:)\n");
printf("1. Count_the_letters\n");
printf("2. Count_the_vowels\n");
printf("3. Reverse_the_word\n");
printf("4. Check_if_palindrome\n");
printf("5. Enter_a_new_word\n");
printf("6. Exit\n");
scanf("%d", &choice);
switch (choice)
{
case 1: Count_the_letters();
break;
}
} while (choice != 3);
}
void Count_the_letters(void)
{
char S[MAX_STRING_LENGTH];
int count;
count = 0;
do {
printf("string:\t");
scanf("%s",S);
if (strcmp(S,"exit") != 0)
++count;
} while (strcmp(S,"exit") != 0);
printf("word count:\t%d\n", count);
}
return 0;
}
scanf("%s", &word);
needs an array of characters to read the data. &word only has space for one character.
You are running into undefined behavior.
Use
char word[26];
scanf("%25s", &word);
The reason is that you are passing the address to the char variable you declared and scanf() is trying to write two bytes where it only fits one.
char word
this declares a char variable, it can hold a single byte
scanf("%s", &word);
whill require at least one byte for an empty string the '\0'.
But also, you declared a lot of functions inside void grab_user_input(void), that is not valid standard c, it might work with some compiler, but it's not standard.