0
I can not add (-) in id and my a full name(Richard Philip)
I provide the question and my code. please help me out Q: Write a program that takes input your name, id and department from keyboard and write those to a file (your_name.txt). Hints: your name, id and department instead of following. Richard.txt Richard Philip 1909-1997-2 Computer science My ans: #include<stdio.h> #include<string.h> int main() { char name[30]; int id; char dep[50]; FILE *fptr; fptr = fopen("M:\\Tafhim.txt","w"); printf("Enter Name:"); scanf("%s",name); printf("Enter ID:"); scanf("%d",id); printf("Enter Department:"); scanf("%s",dep); fprintf(fptr,"%s %d %s\n",name,id,dep); fclose(fptr); return 0; }
5 ответов
+ 2
Use & sign while taking int as user input.
scanf("%d",&id);
Except char arrays, everything else need & sign while taking user input.
+ 2
When you use scanf("%s",name) the compiler stop reading when it reaches a space. So the characters after space won't enter into the variable.
Use this method to read names with spaces : scanf("%[^\n]",name);
+ 2
In the int variable you can write only numbers, you cannot add - in int. So id cannot take numbers with -
If you want to add - in id then declare id as a string too.
0
Arun Ruban SJ I fixed my mistake. But when i try to write name as Richard Philip it was not take this was a single name. In the ID section i can only writ number 1909 but i can not write 1909-1997.
Please fixed this problem
0
Hey boss. As far as I'd you will almost certainly have to take it as a string as well, because it has characters other than whole numbers. As for your name, there are probably going to be a few ways to do it, my suggestion would be to take it in two parts first-name and last-name. In which case you can place your first name as the output filename as well for extra credit. Hope this helps.