Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
void patient_data(){
char name[10];
int ID[4];
int age;
char history[100];
float temp;
int breath;
printf("enter patient data:\n");
scanf("name : %s\n", name);
scanf("ID: %d\n",ID);
scanf("age %d\n",&age);
scanf("history: %s\n", history);
scanf("temp: %f\n",&temp);
scanf("breath: %s\n", breath);
FILE *info;
info = fopen("info.txt","a");
fscanf(info,"%s %d %d %s %f %d",name, ID, &age, history, &temp,&breath);
}
this code is supposed to take user input for patient data and save it in a file that should be accessed later, but the scanf functions are not working.
any Idea on what might be the issue here?
Your scanf's are ill-formed, they should simply be:
scanf(" %9s", name); //<-- added buffer limit
scanf("%d",ID);
scanf("%d",&age);
scanf(" %99s", history); //<-- added buffer limit
scanf("%f",&temp);
scanf("%d", &breath);` // <-- corrected the specifier
If you want to print tags for the user to know what to input use printf or puts before every scanf.
Note that the last scanf has a specifier missmatch, it takes an integer but uses a string specifier.
Also note that the "%s" specifier is unsafe, you should use "%99s" for a 100 char container to avoid buffer overflow. The way you have it, it's no better than gets().
Finally the correct function to write to a file is fprintf.
fprintf(info,"%s %d %d %s %f %d", name, ID, age, history, temp, breath);
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 1 year ago.
Improve this question
Here I am entering my name Amir but is printing weird character É
My code:
#include<stdio.h>
int main()
{
char Name[64];
printf("Enter first name: ");
scanf("%c", &Name);
printf("%c", Name);
}
Source: https://i.stack.imgur.com/qfcUD.png
Few things to fix here.
Firstly, use the correct format specifiers for printing values. %c deals with a character. To print a string you need to use %s.
If you must use scanf then following should work well unless user enters 64 or more characters.
scanf("%s", Name);
printf("%s", Name);
However, it is highly recommended to avoid using scanf to get strings as user input. Use fgets instead.
fgets(name, 63, stdin);
printf("%s", name);
I recommend you to read the man page for fgets(), but in a nutshell, the benefit of using it over scanf is that it prevents array overrun (array out of bounds).
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am trying to develop a small program which is available below:
When I run the program, even after the right input, it gives me the output described in else.
#include <stdio.h>
#include <stdlib.h>
int main()
{
char course;
printf("Enter Your Course Name: \n");
scanf(" %s", &course);
if (course == 'TOEFL') {
printf("Yes, you are eligible \n");
} else {
printf("You Can Not Join Us \n");
}
return 0;
}
You mean
scanf(" %c", &course);
But also,
Strings in c MUST be wrapped with double qoutes, the expression 'TOEFL' must be generating a warning about multi character constant, do not ignore it.
Strings in c, are compared one character at a time, so you need to use a function called strcmp() for that.
To read a string, you need an array to store it in, and yes, the "%s" specifier
char cours[100];
scanf("%99s", course);
if (strcmp(course, "TOEFL") == 0) ...
course is a char variable, so it only can contain a single character.
Try changing its declaration to:
char course[10];
See my code comments for explaination //xxxxxx below
#include <stdio.h>
#include <stdlib.h>
int main()
{
char course; //this decl stores only single character not string
printf("Enter Your Course Name: \n");
scanf(" %s", &course); //passing string argument
if (course == 'TOEFL') {//can't compare strings with == use strcmp
printf("Yes, you are eligible \n");
} else {
printf("You Can Not Join Us \n");
}
return 0;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am extracting data from file with:
sscanf (match, "cpu %d %d %d...", &user1, &nice1, &kernel1, &idl...);
My problem starts when I try to read it for N cores in a loop:
sscanf (match, "cpu0 %d %d..", &user1, &nic...);
sscanf (match, "cpu1 %d %d..", &user1, &nic...);
sscanf (match, "cpu2 %d %d...", &user1, &nic...);
Is there any way to insert decimal variable to this sscanf in C?
Edit:
I want to increment number next to "cpu" string to get "cpu0 %d %d", "cpu1 %d %d", "cpu2 %d %d"
Edit2 + solution:
/* Locate the line that starts with "cpuX " */
match = strstr (buffer, cpux);
/* Parse the line and extract data */
sscanf (match, "%s %d %d %d %d %d %d %d %d %d %d", *&cpuXgarbage,&...)
There is, but it is probably the wrong way to do it.
Instead of putting cpu0 in your sscanf string, why not a %s? Read the cpu0 into a string. Then it does not matter if it matches or not. You could have a read loop that looks for "cpu" at the start and stops when it doesn't match.
for (int i=0; i < 10; i++) {
sprintf(format, "cpu%d blah blah", i);
sscanf(match, format, ...);
}
int user[20];
int nic[20];
int i = 0;
sscanf (match, "cpu0 %d %d..", &(user[i++]), &(nic[i++])...);
sscanf (match, "cpu0 %d %d..", &(user[i++]), &(nic[i++])...);
.....
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
char s[20];
for(int i=1;i<=3;i++)
{
printf("enter a name \n "); // printf & scanf
scanf("%s",&s);
printf("the names are %s \n",s);
}
for(int j=1;j<=3;j++)
{
puts("enter a name \n ");
gets(s); // puts and gets
puts(s);
}
char ch='a';
putchar(ch); // putchar and getchar
ch = getchar();
getch();
}
There is a repetition (three times) of the printf statement...can anyone explain this?
The output is like this:
enter name
my name is alex
the names are my
enter a name
the names are name
enter a name
the names are is
enter a name
alex
enter a name
alex again
enter a name
alex twice
alex twice
scanf with %s will read until the first whitespace character. So your input my name is alex is read over the space of 4 calls to scanf, which will make your output look strange. You might want to consider using the function getline if you want to read until the end of a line (http://en.cppreference.com/w/cpp/string/basic_string/getline)
Note that this was originally also tagged as c++, so I assume the OP can use c++ libs as well.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I've made the following but it skips the part where I have to input the character and I don't understand what my mistake is. Any help ?
#include <stdio.h>
main()
{
int num;
float f;
char c;
printf("Input an integer: ");
scanf_s("%d", &num);
printf("Input an floating point number: ");
scanf_s("%f", &f);
printf("Input a character: ");
scanf_s("%c", &c);
printf("\nThe number is: %d\n",num);
printf("\nThe floating point number is: %f\n", f);
printf("\nThe character is: %c\n", c);
return 0;
}
scanf_s("%d", &d);
This reads characters as long as they fit the "%d" format. The first character not fitting that format is the newline you entered. This remains in the input stream.
scanf_s("%f", &f);
This skips leading whitespaces (i.e., your newline), then reads characters as long as they fit the "%f" format. The first character not fitting that format is the newline you entered. This remains in the input stream.
scanf_s("%c", &c);
This reads one character from the input without skipping potential leading whitespace, i.e., your newline.
It also is undefined behaviour: One thing that scanf_s() does differently than standard scanf() is that it demands a "buffer size" numerical parameter for %c and %s, to avoid buffer overflow. Not giving that parameter makes scanf_s() pull an integer from the stack where you didn't put one, resulting in undefined behaviour. If that next memory address happens to be zero, you'll be scratching your head a lot I would bet...
If I'm not wrong your scanf_s("%c", &c); reads the '\n' character from the previous input.
What you can do is add 1 space before %c so:
scanf_s(" %c", &c);
that will fix your issue. Remember you need to do that if reading a character after some previous input.
Tested and works.
Specify the size. Use this line:
scanf_s("%c", &c, 1);
It should work!