How to separate two if statements in my code? - c

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");
}
}

Related

I wrote a code in C language to print passed/not passed on basis of his marks. No matter, what I input, I am getting the same results i.e passed

# include<stdio.h>
int main() {
int marks;
printf("enter marks: ");
scanf("enter marks: %d ", &marks);
if(marks > 30)
{
printf("passed");
}
else
{
printf("not");
}
return 0;
}
The criteria that I put doesn't help in the output, even if enter marks as 14, then too it displays passed. I am a beginner, so please help.
printf("enter marks: ");
scanf("enter marks: %d ", &marks);
Is not correct.
You do not put a "prompt" in scanf. Only the value you want to read.
It should be:
scanf("%d", &marks); // Prompt was already done with printf on prior line
#include <stdio.h>
#include <conio.h>
int main() {
int marks;
printf("enter marks:");
scanf("%d",&marks);
if(marks > 30)
{
printf("passed");
}
else
{
printf("not");
}
return 0;
}
its working fine

Having issues with this simple program

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;
}

how to combine registration and billing for the program

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.

programe name .exe stops working in c IDE ,whenever i use SCANF funtion

programe name .exe stops working in c IDE ,whenever i use SCANF funtion....even when it builds and runs correctly.....please help me with this...i am a beginner
#include <stdio.h>
#include <stdlib.h>
int main() {
int age;
printf("please enter the age");
scanf("%d, &age");
if (age > 18) {
printf("the age is grater than 18");
}
if (age == 18) {
printf("the age is equal to 18");
}
if (age < 18) {
printf("the age is less than 18");
}
}
scanf("%d, &age");
is
scanf("%d", &age);
Also, if you defined main as "int main()", then it has to return an int.

Program does not exit when expected

I'm new to C and I am trying to write a program that will let you enter up to 100 people's age and salary. The program at first will print some sentences to introduce (the display function) and then ask you whether you want to continue or not (yes_no function). After you enter a person's information, the program will also ask if you would like to continue to enter the next person's information or not. If you would like to continue, you will need to enter 1 and 0 for no/exit. When I compile and run the code, I found a problem and i could not figure out why!
The problem is that if you choose 0 (which means no/exit) after entering one's information, the program does not exit. It instead just asks the next person's information. But when I choose 0 right from the beginning, it just exits like usual. Why?
#include<stdio.h>
#define max 100
#define yes 1
#define no 0
int display(void);
int yes_no(void);
void get_data(void);
int date[max],month[max],year[max];
int cont;
int salary;
int main(){
cont=display();
if (cont==yes){
get_data();
}
return 0;
}
int display(void){
printf("This program will let you enter ");
printf("the age and salary of up to 100 people ");
cont=yes_no();
return cont;
}
int yes_no(void){
int i=0;
printf("\nDo you want to continue? Enter 1 for Yes and 0 for No\n");
scanf("%d", &i);
while(i<0 || i>1){
printf("Invalid value.Please enter again\n");
scanf("%d", &i);
}
if(i==1){
return (yes);
}else return (no);
}
void get_data(void){
int i=0;
for(i=0;i<max;i++){
printf("Enter information for people %d\n", i+1);
printf("Enter birthday\n");
do{
printf("Enter date\n");
scanf("%d", &date[i]);
}while( 0>date[i] || 31<date[i] );
do{
printf("Enter month\n");
scanf("%d", &month[i]);
}while( 0>month[i] || 12<month[i]);
do{
printf("Enter year\n");
scanf("%d", &year[i]);
}while( 1900>year[i] || 2016<year[i]);
printf("Enter salary\n");
scanf("%d", &salary);
cont=yes_no();
}
}
void get_data(void){
int i=0;
for(i=0;i<max;i++){
printf("Enter information for people %d\n", i+1);
printf("Enter birthday\n");
do{
printf("Enter date\n");
scanf("%d", &date[i]);
}while( 0>date[i] || 31<date[i] );
do{
printf("Enter month\n");
scanf("%d", &month[i]);
}while( 0>month[i] || 12<month[i]);
do{
printf("Enter year\n");
scanf("%d", &year[i]);
}while( 1900>year[i] || 2016<year[i]);
printf("Enter salary\n");
scanf("%d", &salary);
cont=yes_no(); // <== The Problem lies here
}
}
You ask the user if wants to continue but you never check the return value of yes_no()
Just add this after this line and it should work like a charm:
if (cont == no)
return;
As others have mentioned, there are still some things you can do to "improve" your code.
defines should be capitalized so #define YES 1 would stick to this convention.
And you shouldn't use global variables. These are bad programming style. Simply pass the things you need in other functions as a parameter and if you need the manipulated value later on past them as a pointer.
Also the formatting could be improved (however this is a mostly opinion based topic ;) )
In C you usually have an extra line for each curly bracket.
void get_data(void)
{
...
}
//instead of
void get_data(void){
...
}
Also the blanks after the do-while-loop should look more like so:
do
{
...
} while(1900 > year[i]); //here the curly bracket is ok that way
And operators should have a blank on both sides:
printf("Enter information for people %d\n", i + 1);
// instead of this
printf("Enter information for people %d\n", i+1);
This is all I've seen up to now.

Resources