#include <stdio.h>
int main()
{
char name[10];
int birth_year;
printf("Enter your name : ");
scanf("%c",name);
printf("Enter your birth year : ");
scanf("%i",&birth_year);
int age = 2020 - birth_year;
printf("Your age is %i",age);
}
I am trying to take the value of birth_year as an input but it automatically assigns it to 0 for some reason what am I doing wrong
In the first scanf you should read a string instead of a char, that should do it.
Also, it's always good to have a whitespace before you read a char, so it resets the buffer memory.
#include <stdio.h>
int main()
{
char name[10];
int birth_year;
printf("Enter your name : ");
scanf(" %s",name);
printf("Enter your birth year : ");
scanf(" %d",&birth_year);
int age = 2020 - birth_year;
printf("Your age is %i",age);
}
Related
The program is just simply supposed to calculate the users age by subtracting their dob from the current year. When I run the program it compiles successfully but I get a long number such as -215863352. The if and else conditions are added just to test them out, I was writing various programs using them to make sure I understand the syntax in c. I figure I'm missing something simple but can't figure it out.
#include <stdio.h>
int main()
{
int year;
int cyear;
int age = cyear - year;
printf("Please enter the year you were born: \n");
scanf("%i", &year);
printf("Now enter the current year: \n");
scanf("%i", &cyear);
if (1+1 == 2){
printf("You must be %i", age);
}
else {
printf("Cannot compute age, GOODBYE:\n");
}
return 0;
}
You are calculating the age before the input is taken from the user. So the age variable is storing a garbage value.
Solution:
Position the calculation of age after taking the input from user that is after taking input of cyear using scanf. The correct code is given below
#include <stdio.h>
int main()
{
int year;
int cyear;
int age =0; //initialise with 0
printf("Please enter the year you were born: \n");
scanf("%i", &year);
printf("Now enter the current year: \n");
scanf("%i", &cyear);
age = cyear - year; //note the change here
if (1+1 == 2){
printf("You must be %i", age);
}
else {
printf("Cannot compute age, GOODBYE:\n");
}
return 0;
}
enter code here
#include <stdio.h>
int main()
{
long long int year;
printf("Please enter the year you were born: \n");
scanf("%lld",&year);
long long int cyear;
printf("Now enter the current year: \n");
scanf("%lld",&cyear);
long long int age = cyear-year;
if (1){
printf("You must be %lld", age);
}
else { printf("Now enter the current year: \n");
scanf("%lld",&cyear);
printf("Cannot compute age, GOODBYE:\n");
}
return 0;
}
This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
Closed 4 years ago.
I wrote this C program to enter names and ages of 3 people. But the output wasn't my expectation. It was able to enter name and age for the first person, but it wasn't able for second and third persons. Please help.
#include <stdio.h>
#include <string.h>
int main()
{
int i, age;
char name[20];
for(i=0; i<3; i++)
{
printf("\nEnter name: ");
gets(name);
printf("Enter age: ");
scanf(" %d", &age);
puts(name);
printf(" %d", age);
}
return 0;
}
In short: Your 2nd puts is processing the '\n' from your scanf.
Fix by adding getchar(); after scanf
Explanation:
1st iteration:
printf("\nEnter name: ");
gets(name); // line is read from input
printf("Enter age: ");
scanf(" %d", &age); // a number is read from input, and the newline char ('\n') remains in buffer
puts(name);
printf(" %d", age);
2nd iteration:
printf("\nEnter name: ");
gets(name); // previously buffered newline char is read, thus "skipping" user input
printf("Enter age: ");
scanf(" %d", &age);
puts(name);
printf(" %d", age);
Same goes for 3rd iteration, and this is why you lose user input
The best way to store information of more than one person is to use struct, like
struct person {
int age;
char name[20];
};
and make array of struct, like
struct person people[3];
than use loop with accessing people[i].age and people[i].name, e.g.:
#include <stdio.h>
#include <string.h>
struct person {
int age;
char name[20];
};
#define ARR_SIZE 3
int main(int argc, char* argv[])
{
struct person people[ARR_SIZE];
int i;
char *lastpos;
for(i = 0; i < ARR_SIZE; i++)
{
printf("\nEnter name: ");
scanf(" %s", people[i].name);
if ((lastpos=strchr(people[i].name, '\n')) != NULL) *lastpos = '\0'; // remove newline from the end
printf("Enter age: ");
scanf(" %d", &people[i].age);
}
printf("This is the people you entered:\n");
for(i = 0; i < ARR_SIZE; i++)
{
printf("%d : %s : %d\n", i+1, people[i].name, people[i].age);
}
return 0;
}
UPDATE:
As you see I use scanf(" %s", people[i].name); instead of gets(people[i].name); to read name from stdin. Try both option for the following cases:
enter short name (e.g. John) and correct age (e.g. 15)
enter two word name (e.g. John Smith) and correct age (e.g. 17)
enter short name and uncorrect age (e.g. five)
Then read articles about value returned by scanf and cleaning input buffer
How to control user input for char type in C programming??
Problem: If user give 'Male' as first input then program did not ask to give 'age'
Output
Enter gender(M/F):Male
Enter age:
Your Gender is Male(M)
Your Age:71 //garbage value
#include <stdio.h>
int main()
{
char gender;
int age;
printf("\nEnter gender(M/F):");
scanf("%c", &gender);
printf("\nEnter age:");
scanf("%d", &age);
if(gender=='M'){
printf("\nYour Gender is Male(%c)",gender);
}else{
printf("\nYour Gender is not Male(%c)",gender);
}
printf("\nYour Age a:%d",age);
return 0;
}
Expected Output: if user give input 'Male'/'Female'/'M'/'F' then hit enter
then console will ask to give age
Enter gender(M/F):Male
Enter age:23
Your Gender is Male(M) //base on &gender
Your Age:23
You want to read a sting, but you are using a character, so change your code to this:
char gender[7];
scanf("%6s", gender);
...
if (gender[0] == 'm' || gender[0] == 'M')
printf("\nYour Gender is Male(%c)", gender[0]);
...
Second solution:
Just use fgets() for gender, since you want to be able to read a string, and then scanf() for age, like this:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
char gender[7];
printf("Enter gender(M/F): ");
if (!(fgets(gender, sizeof(gender), stdin) != NULL)) {
fprintf(stderr, "Error reading Gender.\n");
exit(1);
}
gender[strcspn(gender, "\n")] = 0;
int age;
printf("\nEnter age:");
scanf("%d", &age);
if (gender[0] == 'm' || gender[0] == 'M')
printf("\nYour Gender is Male(%c)", gender[0]);
else if (gender[0] == 'f' || gender[0] == 'f')
printf("\nYour Gender is Female(%c)", gender[0]);
else
printf("Unrecocognized gender\n");
printf("\nYour Age is: %d\n",age);
return 0;
}
Output:
Enter gender(M/F): f
Enter age: 25
Your Gender is Female(f)
Your Age is: 25
Use gets() or char array and Strcmp() to compare strings.
#include <stdio.h>
#include <string.h>
int main()
{
char gender[40];
int age;
printf("\nEnter gender(M/F):");
gets(gender);
printf("\nEnter age:");
scanf("%d", &age);
if(gender=="M" || (strcmp(gender, "Male")==0)){
printf("\nYour Gender is Male(%s)",gender);
}else{
printf("\nYour Gender is not Male(%s)",gender);
}
printf("\nYour Age a:%d",age);
return 0;
}
I am writing a simple program in C using structs. The user needs to enter some values for the struct - a name and age. After I enter the data for the first time, the second time the program just skips one of the fields and only wants me to enter the second field of the data. I can't figure out what's wrong.
struct Person {
char name[20];
int age;
};
void main(){
struct Person pArray[10];
for (int i = 0; i < 10; i++) {
printf("Please enter a name and age:\n");
printf("Name: ");
fgets(pArray[i].name, 20, stdin);
printf("Age: ");
scanf("%d", &pArray[i].age);
}
}
As you can see, after entering Jonathan and 45 for the first time, the second time it skipped the name and wants only the age. Why is this happening?
I try not to mix formatted and unformatted input (e.g., fgets and scanf). Here is your program using only fgets for input:
#include <stdio.h>
struct Person {
char name[20];
int age;
};
int main(){
struct Person pArray[10];
char numberBuffer[20];
for (int i = 0; i < 10; i++) {
printf("Please enter a name and age:\n");
printf("Name: ");
fgets(pArray[i].name, 20, stdin);
printf("Age: ");
fgets(numberBuffer, 20, stdin);
sscanf(numberBuffer, "%d", &pArray[i].age);
}
}
The compiler does not show any error but the second and the third gets() functions do not seem to work as expected. Instead the next line of code gets executed.
#include <stdio.h>
#include <string.h>
struct student {
char name[100], result[100];
int marks;
} s1, s2, s3;
main() {
printf("Enter the name of the student s1:");
gets(s1.name);
printf("Enter the marks obtained by the student s1:");
scanf("%d", &s1.marks);
printf("Enter the name of the student s2:");
gets(s2.name);
printf("Enter the marks obtained by the student s2: ");
scanf("%d", &s2.marks);
printf("Enter the name of the student s3:");
gets(s3.name);
printf("Enter the marks of the student s3:");
scanf("%d", &s3.marks);
if ((s1.marks > s2.marks) & (s1.marks > s3.marks))
printf("the student s1 has got the highest marks");
else
if ((s2.marks > s3.marks))
printf("The student s2 has got the highest marks");
else
printf("The student s3 has got the highest marks");
}
This is because you are mixing up gets (which is deprecated*) and scanf.
End-of-line mark from scanf remains in the buffer, so when gets tries to read the next input, it treats it as an empty line.
You can fix this by using scanf for all your input, including strings:
printf("Enter the name of the student s1:");
scanf("%99s", s1.name); // Use "%99[^\n]" to allow multi-part names
printf("Enter the marks obtained by the student s1:");
scanf("%d", &s1.marks);
* gets is deprecated, so you should use fgets(s1.name, sizeof(s1.name), stdin) instead. Here is a reference for fgets.
Use scanf("%d ",&s1.marks);. The space after %d would escape the \n that you enter after the number. If we do not do that, gets() will read "\n" as the next string input.
This should work:
#include <stdio.h>
struct student {
char name[100], result[100];
int marks;
} s1,s2,s3;
int main() {
printf("Enter the name of the student s1:");
scanf("%s", s1.name);
printf("Enter the marks obtained by the student s1:");
scanf("%d", &s1.marks);
printf("Enter the name of the student s2:");
scanf("%s", s2.name);
printf("Enter the marks obtained by the student s2: ");
scanf("%d", &s2.marks);
printf("Enter the name of the student s3:");
scanf("%s", s3.name);
printf("Enter the marks of the student s3:");
scanf("%d", &s3.marks);
if ((s1.marks > s2.marks) && (s1.marks > s3.marks))
printf("the student s1 has got the highest marks");
else if((s2.marks > s3.marks))
printf("The student s2 has got the highest marks");
else
printf("The student s3 has got the highest marks");
return 0;
}