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;
}
Related
This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
Why is the gets function so dangerous that it should not be used?
(13 answers)
Closed 10 months ago.
#include <stdio.h>
void input(void);
void avgSalary(void);
struct Person{
char name[15];
int age;
float salary;
};
struct Person person[10];
int main(){
int choice, avg;
char cont;
do{
printf("1. Input person record \n2. Average salary\n");
printf("\nPlease enter selection: ");
scanf("%d", &choice);
if (choice == 1){
input();
}
else if (choice == 2){
avgSalary();
}
else{
printf("Error");
};
printf("\nContinue?: ");
scanf("%c", &cont);
printf("\n");
}while(toupper(cont)=='Y');
}
void input(){
int x;
getchar();
for (x=0;x<10;x++){
printf("Person %d\n", x+1);
printf("Name: ");
gets(person[x].name);
printf("Age: ");
scanf("%d", &person[x].age);
printf("Salary: ");
scanf("%f", &person[x].salary);
};
}
void avgSalary(){
int x, sum=0;
float avg=0;
for (x=0;x<10;x++){
sum += person[x].salary;
};
avg = sum/10;
printf("The average salary of %d person is %.2f\n", x, avg);
}
For the output, It asks for person's info and another is the average salary. We select 1 then after entering the first person's name, age and salary, I couldn't enter the next person's name all the way until the 10th person. Why is this happening?
Try replacing gets(person[x].name); with scanf .You could try scanf("%s",person[x].name); . Or you could probably (not recommended) add a getchar() at the end of your loop,after the last scanf call. In your first run through the loop,you are getting the behavior you want because you use the getchar() that is being called before the loop.
Edit:
Keep in mind that,using scanf you can't take an input containing a full name. If you want to do that,you can either use a seperate array for the last name in your Struct.Or else you can just probably get away with using getchar() and gets.
I wrote the code below and I have a problem I don't know how to separate the first if and the second if.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int main()
{
printf("welcome user\n");
printf("please answer this following questions\n");
printf("what is your age");
int age;
scanf("%d", &age);
printf("your age is %d\n", age);
int main();
{
int age = 30;
if (age < 30);
printf("you are yong i like that\n ");
int main();
if (age > 30);
printf("you are to old\n ");
printf("its ok you still human\n");
printf("XD\n");
}
}
I think you probably want your code to be revised to somewhere along the lines of this:
Remove these extra unnecessary main() and have if and else if block or just else block contained in braces. Also, although not required for correct code execution, indent for better readability. And finally delete the int age = 30; line.
int main()
{
printf("welcome user\n");
printf("please answer this following questions\n");
printf("what is your age");
int age;
scanf("%d", &age);
printf("your age is %d\n", age);
if (age < 30) // if more than one line, must contain if block within braces
printf("you are yong i like that\n ");
else if (age > 30) {
printf("you are to old\n ");
printf("its ok you still human\n");
printf("XD\n");
}
}
the code is register and billing for the hospital that use for some program I can't combine the code together and it could construct program that register and count for the bill that count that
#include <stdio.h>
#include <stdlib.h>
int check_pass(int pass);
int main()
{
int x,pass;
for(x=0;x<5;x++){
printf("Enter your password: ");
scanf("%d",&pass);
check_pass(pass);
if(pass==1234){
break;}
}
return 0;
}
int check_pass(int pass)
{
if(pass == 1234){
printf("Password is correct\n");
printf("============== Hospital City =============== ");
}
else{
printf("Password is wrong, please try again\n");
}
return 0;
}
the another code is for billing for patient that program thank for the help but the code is very blurred for me
#include <stdio.h>
#include <stdlib.h>
int main()
{
int id,day1,mnth1,year1,day2,mnth2,year2;
int days,Bill;
printf("ID: ");
scanf("%d",&id);
printf("Date in (MM/DD/YYYY): ");
scanf("%d/%d/%d",&mnth1,&day1,&year1);
printf("Date out (MM/DD/YYYY): ");
scanf("%d/%d/%d",&mnth2,&day2,&year2);
days = day2 - day1;
printf("Number of days: %d days\n",days);
Bill = days*10;
printf("Bill to pay:%d x RM10 = RM%d\n",days,Bill);
return 0;
}
Bit of an unclear question, but I will attempt to answer.
Break your code into modules. This means, put functionality into functions and not in main. Then, you can call them whenever from wherever.
int get_bill()
{
int id,day1,mnth1,year1,day2,mnth2,year2;
int days,Bill;
printf("ID: ");
scanf("%d",&id);
printf("Date in (MM/DD/YYYY): ");
scanf("%d/%d/%d",&mnth1,&day1,&year1);
printf("Date out (MM/DD/YYYY): ");
scanf("%d/%d/%d",&mnth2,&day2,&year2);
days = day2 - day1;
printf("Number of days: %d days\n",days);
Bill = days*10;
printf("Bill to pay:%d x RM10 = RM%d\n",days,Bill);
return Bill;
}
You can then call this from main after the user enters a password.
#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);
}
I write a program to keep track of the inventory of a grocer store. But in my code i would like to print a value. And value is (Number of units) * (unit price). But somehow i get get the garbage value in my program. So Can you Please help me?
#include<stdio.h>
#include<conio.h>
#define MAX 5
int printinventory(int , int unit[] , float price[]);
int main()
{
int item[MAX],unit[MAX],x,i;
float price[MAX];
printf("Please enter how many category items (up to 5) : ");
scanf("%d",&x);
for(i=0;i<x;i++)
{
printf("\nPlease enter Number of Units #%d : ",i+1);
scanf(" %d",&unit[i]);
printf("\nPlease enter the Unit Price #%d : ",i+1);
scanf(" %f",&price[i]);
}
printinventory(x , unit , price);
getch();
}
int printinventory (int y, int unit[] , float price[])
{
int i,j=0;
float value[MAX];
for(i=0;i<y;i++);
{
value[i] = (unit[i] * price[i]);
}
system("cls");
printf("Item Number of Units Unit Price Value ");
printf("\n\n------------------------------------------------");
for(i=1;i<=y;i++)
{
printf("\n%d",i);
printf("\t %d",unit[j]);
printf("\t\t $%.2f",price[j]);
printf("\t$%.2f",value[j]);
j++;
}
printf("\n\n------------------------------------------------");
printf("\n\t\t\t\tTotal $ ");
getch();
}
The problem seems to be that you've mistakenly included a semicolon at the end of one of your for loops:
for(i=0;i<y;i++);