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.
Related
I am writing a program where if the user chooses "2" the program will add a new student record and if he chooses "4" then it will compare his surname with all the other surnames, it will find everyone that has the same surname and print their school information. The problems are the following:
When the user selects "2" my program actually shows all the prompts that are needed and you can input all the information however when I try to print it just gives me 0 0 0.000000 .When it should have given me 2(id) B(name) b(surname) 1(semester) 4(grade).
When the user selects "4" it reads the surname but when it goes to compare it with the others to see if it exists or not it just crashes.
Here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct student{
int id;
char name[50];
char surname[50];
int semester;
float grade;
}student;
int main()
{
int x,std,i;
x=0;
int car=0;
int flag=1;
struct student *ptr = NULL;
while (x!=8)
{
printf("\n1. Initialize student list\n2. Add a student record\n3. Delete a student record\n4. Display a student record by student surname\n5. Display students passed\n6. Display students failed\n7. Display all student records\n8. Exit");
printf("\nYour choice: ");
scanf("%d",&x);
if (x==1)
{
printf("How many new students? ");
scanf("%d",&std);
ptr = (student*)malloc(std*sizeof(student));
for(i = 0; i < std; i++){
printf("Enter detail of student #%d\n", (i + 1));
ptr[i].id=i+1;
printf("Enter first name: ");
scanf("%s", ptr[i].name);
printf("Enter last name: ");
scanf("%s", ptr[i].surname);
printf("Enter semester: ");
scanf("%d", &ptr[i].semester);
printf("Enter grade: ");
scanf("%f", &ptr[i].grade);
}
}
else if (x==2)
{
ptr=realloc(ptr,100* sizeof(student));
std=std+1;
ptr[std].id=std+1;
printf("Enter first name: ");
scanf("%s", ptr[std].name);
printf("Enter last name: ");
scanf("%s", ptr[std].surname);
printf("Enter semester: ");
scanf("%d", &ptr[std].semester);
printf("Enter grade: ");
scanf("%f", &ptr[std].grade);
flag=0;
}
else if (x==3)
{
/* code */
}
else if (x==4)
{
printf("Give the surname: ");
const char *sur;
scanf("%s", sur);
for (i = 0; i < std; i++)
{
if(strcmp(sur, ptr[i].surname)==0){
printf("%d ", ptr[i].id);
printf("%s ", ptr[i].name);
printf("%s ", ptr[i].surname);
printf("%d ", ptr[i].semester);
printf("%f ", ptr[i].grade);
printf("\n");
car=1;
}
}
if (car=0)
{
printf("That surname does not exist");
}
}
I may have missed a "{" but this is only part of my code
else if (x==7)
{
for(i = 0; i < std; i++){
printf("%d ", ptr[i].id);
printf("%s ", ptr[i].name);
printf("%s ", ptr[i].surname);
printf("%d ", ptr[i].semester);
printf("%f ", ptr[i].grade);
printf("\n");
}
}
}
}
For when the user chooses "2" everything goes well until its time to print so i tried increasing std by 1 two times.One when the program goes to option "2" and another time when it goes back to main. It didnt do anything so i returned it to how it was in the beginning.
For when the user chooses "4" I tried going with strcmp but it would just throw errors because of sur. I had it written as char sur. After I wrote it as const char *sur it stopped throwing errors but it would just crash when it went to compare it with the other.
# 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
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 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;
}
Here im working on my first coding in c programming. I've got the problem when I want to get the user input and display the output from the user input. here my code:
#include <stdio.h>
int main(){
printf("Enter the number : ");
int hallo = 0;
scanf("%d", hallo);
printf("hallo, %d", hallo);
}
after executing the code the last line not appear where is prinf("hallo, %d", hallo);. Which is to display the user input.
The 4th line of the code: scanf("%d", hallo);
Here &hallo should be used instead of just the variable name hallo.
The significance of the & sign is that it gives the address of a particular variable. So whatever the value is entered by the user, it will be stored at the address of the variable (in this case at the address of variable hallo).
Use the & after the , in scanf
Like this
scanf("%d", & hallo);
#include <stdio.h>
#include <stdlib.h>
typedef struct{
char name[30];
int id;
double salary;
} Employee;
int main()
{
int n;
printf("Number of employee to process: ");
scanf("%d",&n);
Employee employees[n];
printf("Enter %d Employee Details \n \n",n);
for(int i=0; i<n; i++){
printf("Employee %d:- \n",i+1);
printf("Name: ");
scanf("%[^\n]s",employees[i].name);
printf("Id: ");
scanf("%d",&employees[i].id);
printf("Salary: ");
scanf("%lf",&employees[i].salary);
char ch = getchar();
printf("\n");
}
printf("-------------- All Employees Details ---------------\n");
for(int i=0; i<n; i++){
printf("Name \t: ");
printf("%s \n",employees[i].name);
printf("Id \t: ");
printf("%d \n",employees[i].id);
printf("Salary \t: ");
printf("%.2lf \n",employees[i].salary);
printf("\n");
}
return 0;
}