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.
Related
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 months ago.
Improve this question
#include<stdio.h>
int main()
{
char str(100);
int i;
printf("Please enter your name")
scanf("%s",str);
printf("your name");
return 0 ;
}
This is the code I have written,
but the problem is the code runs but never gives the output
until I manually stop the code.
How does the scanf("") function work?
Scanf reads formatted input from the standard input. There is nothing wrong with scanf itself. Your way of using it is also correct, but there are two problems with your code:
The first is that arrays must be defined with []. Therefore,
char str(100);
should be
char str[100];
The second problem is that you are receiving the input, but you are never printing it. To do so, you can write the following code:
printf("%s\n", str);
The final code being:
#include <stdio.h>
int main()
{
char str[100];
printf("Please enter your name");
scanf("%s", str);
printf("your name is: %s\n", str);
return 0;
}
Also, don't forget the semi-colons.
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);
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
//code for mvt. in this ch did not scan by compiler. ch is used as a condition for loop . Its initial value is 'y' , and if it goes to 'n' , loop breaks. In this code i am asking user if he/she want to continue then press y else n . but don't know why gcc do not wait for scan it. It is a simple code but i can not get the mistake.
int main(){
char ch;
temp=tm;
ch='y';
for(i=0;ch=='y';i++){
printf("enter memory size for process %d",i+1);
scanf("%d",&m);
if(m<temp)
{printf("memory allocated\n"); ms[n]=m; n++;
temp-=ms[i];
}
else
{printf("memory not allocated\n"); }
https://stackoverflow.com/users/10404087/rishabh-sharma
printf("do you want to continue");
scanf("%c",&ch);
}
printf("total memory : %d\n",tm);
printf("process \t occupied \n");
for(i=0;i<n;i++)
printf("%7d \t %8d \n",i+1,ms[i]);
printf("total externel fregment : %d \n",temp);
return 0;
}
You have to use flushall() function that clears all buffers associated with input streams, and writes any buffers associated with output streams.
Added:flushall() isn't C, but a vendor specific extension.
OR
Other alternative is to use space before %c
Example
char ch;
scanf(" %c", &ch);
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 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 8 years ago.
Improve this question
In C programming,how to write a program,which on entering the name gives output as last name first then first name?
for example
input - faiz anwar
output- anwar faiz
#include <stdio.h>
int main(void) {
char first_name[32];
char last_name[32];
scanf("%31s %31s", first_name, last_name);
printf("%s %s\n", last_name, first_name);
return 0;
}
char full_name[64];
char first_name[32];
char last_name[32];
scanf("%63[^\n]", full_name);//fgets(full_name, sizeof(full_name), stdin);
sscanf(full_name, "%31s %31s", first_name, last_name);
printf("%s %s\n", last_name, first_name);
Read the line into a buffer using fgets()
Break the line into tokens using strtok()
Save the tokens into a array using strcpy() and print the array from last_idx to 0.
This applies generally to a line of input instead of just 2 strings.