I am trying to write some records (patient info) into a .dat file using c. The patient info contains the National ID no., name, gender and the telephone no.
Here's my code:
#include<stdio.h>
int main(void)
{
char order = 'y';
int nic = 0; // NIC no.
char name[] = "xxx"; // name
char gender = 'x'; // Patients' gender
int tel_no = 0; // telephone no.
FILE *fp;
fp = fopen("patientdetails.dat", "w"); // Creat patientdetails file
if (fp == NULL)
{
printf("Cannot open file\n");
return -1;
}
else
{
printf("Do you want to enter a record (y/n): ");
scanf("%c", &order);
while (order == 'y' || order == 'Y')
{
printf("Enter nic no.: ");
scanf("%d", &nic);
printf("Enter name: ");
scanf("%s", name);
printf("Enter gender (m/f): ");
scanf(" %c", &gender);
printf("Enter tel no.: ");
scanf("%d", &tel_no);
fprintf(fp, "%d %s %c %d\n", nic, name, gender, tel_no);
printf("Do you want to enter a record (y/n): ");
scanf(" %c", &order);
}
fclose(fp);
}
return 0;
}
However when I enter the following info into my .dat file in the terminal:
Do you want to enter a record (y/n): y
Enter nic no.: 200007102766
Enter name: Timothy
Enter gender (m/f): m
Enter tel no.: 0779428897
Do you want to enter a record (y/n): n
There is an error in the integers, namely nic no. and telephone no.
Additionally I made another program to read the file, the error in the integers still seem to show up.
Errors in the .dat file:
7956596 Timothy m 779428897
I can't seem to figure out how to correct the integer errors,
where 20007102766 is shown as 7956596
and 0779428897 is shown as 779428897.
Any help is appreciated!
Your variable byte size is larger than a int, so you should use bigger variable type.
In your example, you can use long type for nic and tel_no veriables.
Also you should use %ld in format, otherwise it gives you warning.
char order = 'y';
long nic = 0; // NIC no.
char name[] = "xxx"; // name
char gender = 'x'; // Patients' gender
long tel_no = 0; // telephone no.
And while formating
printf("Enter nic no.: ");
scanf("%ld", &nic);
printf("Enter tel no.: ");
scanf("%ld", &tel_no);
fprintf(fp, "%ld %s %c %ld\n", nic, name, gender, tel_no);
Related
I'm new to C, and currently trying to practice on some simple codes, and I'm currently stuck with that next one.
After entering the first customer data, and repeat the code... View_customer function fails to show the saved data, and when I try to go to enter a second account data, it fails at the second entry.
#include<stdio.h>
#include<stdlib.h>
typedef struct cust{
char name[60];
int acc_no,age;
char address[60];
char citizenship[15];
double phone;
char acc_type[10];
} cust;
void new_account(int num);
void view_account(int num);
int main()
{
cust customers[10];
char answer;
int n;
int cutomer_number=1;
int cutomer_num2;
printf("Welcome To The program X: \n");
do{
printf("\n How can we serve you today? \n 1.Create a new account \n 2.Print an existing Account info \n ");
scanf("%d",&n);
if (n==1)
{
new_account(cutomer_number);
cutomer_number++;
}else {
printf("Please enter your Cust number: ");
scanf(" %d",&cutomer_num2);
view_account(cutomer_num2);
};
printf("\n Press Y to continue. Press any Key To Exit: ");
scanf(" %c",&answer);
}while (answer == 'Y' || answer == 'y');
return 0;
}
void view_account(int n)
{
cust customers[n];
printf("Your name is %s \n ", customers[n].name);
printf("Your age is %d \n", customers[n].age);
printf("Your address is %s \n", customers[n].address);
printf("Your citizenship is %s \n", customers[n].citizenship);
printf("Your phone number is %f \n", customers[n].phone);
printf("Your account type is %s \n", customers[n].acc_type);
};
void new_account(int n)
{
cust customers[n];
customers[n].acc_no = n;
printf("You are the customer number %d \n", customers[n].acc_no);
printf("Please, Enter your name: ");
scanf("%s", &customers[n].name);
printf("Please, Enter your age:");
scanf(" %d", &customers[n].age);
printf("Please, Enter your address: ");
scanf(" %s", &customers[n].address);
printf("Please, Enter your citizenship: ");
scanf(" %s", &customers[n].citizenship);
printf("Please, Enter your phone number: ");
scanf(" %f", &customers[n].phone);
printf("Please, Enter your account type: ");
scanf(" %s", &customers[n].acc_type);
}
>
Each of your three functions declares its own customers array variable, so each of them has their own memory for the customer data. Moreover, the array of new_account goes out of scope at the end of the function, so you can no longer safely access the data. Because of how C compilers typically work, the customer data is not immediately erased from memory, so your view_account function might still be able to read it, but that is what is called "undefined behavior". Which means it might work, or it might not.
Try to pass down the array from the main function to the other two functions in parameters. Or, to make things simpler at first, you could also turn the local customers variable of main into a global variable.
cust customers[10];
int main(int argc, char *argv[])
{
char answer;
int n;
int cutomer_number=1;
int cutomer_num2;
printf("Welcome To The program X: \n");
...
}
void view_account(int n) {
printf("Your name is %s \n ", customers[n].name);
...
}
void new_acccount(int n)
{
customers[n].acc_no = n;
...
}
Note that there are further issues in your code, like not checking the return value of scanf or overflowing the char arrays of the struct if you enter too many characters (missing bounds and length checking), or being able to enter more than 10 customers and accessing out of bounds of the customers array, or not using customers[0] (because your customer_number starts at 1, but array indices are 0-based). But I will not go into further details here to keep the answer focused.
#include<stdio.h>
#include<stdlib.h>
char* genderfun(){
char *gender;
char g;
printf("\n >>> enter your gender (M/F/T): ");
//g = getc(stdin);
scanf(" %c",&g);
if(g == 'M'){
gender = "Male";
}else if(g == 'F'){
gender = "Female";
}else{
gender = "Transgender";
}
return gender;
}
float percentagecalculator(){
float math,physics,chemistry,english,other,percent;
printf("\n >>> Enter maths marks: ");
scanf("%f",&math);
printf("\n >>> Enter english marks: ");
scanf("%f",&english);
printf("\n >>> Enter physics marks: ");
scanf("%f",&physics);
printf("\n >>> Enter chemistry marks: ");
scanf("%f",&chemistry);
printf("\n >>> Enter additional subject marks: ");
scanf("%f",&other);
percent = ((math+english+physics+chemistry+other)/500)*100;
return percent;
}
void main(){
char *name;
char* genders;
int age,count;
float percent;
printf(">>> Enter your name: ");
scanf(" %c",&name);
//fflush(stdin);
printf("\n >>> Enter your age: ");
scanf("%d",&age);
genders = genderfun();
percent = percentagecalculator();
if(percent < 33){
printf("\n name : %c \n age : %d \n gender : %c \n Percentage : %f \n Status : Failed",name,age,genders,percent);
}else if(percent >= 33){
printf("\n name : %c \n age : %d \n gender : %c \n Percentage : %f \n Status : Passed",name,age,genders,percent);
}else{
printf("\n Error");
}
}
My code is not taking a name more than one character as input and if I try that it skips everything and programs ends and also the gender is not being returned but instead it prints very absurd values in console. Please help me fix this bug.
Well, I've been reading your code and there are several problems. Here are the ones that I've found out:
Return type of 'main' is not 'int'.
In main: Unused variable 'count'
Missing spacing and camel notation for function
You're assuming to read string, but you are actually reading a char (e.g. the name, the creation of gender string, and so on).
You're trying to insert a string, but reading a char, you pollute all your input buffer.
You're using fflush(...). Please check here why you shouldn't use it: Using fflush(stdin)
In order to use strings (or array of char) while reading dynamically names and assigning chars to an array of char, you need dynamic memory or at least VLA (in the following code, you will find the implementation with Dynamic Memory).
In the 'percentage calculator' function you are assuming to take multiple values for each subject. Actually, you are taking only one mark for each subject.
You are going wrong while reading a string. Here's how to properly do that:
How to read string from keyboard using C?
You are trying to print a char instead of a string in the final print.
Please do a better naming of your variables and your functions.
I'm gonna attach here a working code for your question:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define stringSize 256
char * readGender() {
fseek(stdin, 0, SEEK_END);
char * gender = (char *) malloc(stringSize);
char g;
if (!gender) exit(EXIT_FAILURE);
printf("\n >>> Enter your gender (M/F/T): ");
g = getchar();
if (g == 'M') {
strcpy(gender, "Male");
}else if(g == 'F') {
strcpy(gender, "Female");
}else {
strcpy(gender, "Transgender");
}
gender = (char *) realloc(gender, strlen(gender) + 1);
if (!gender) exit(EXIT_FAILURE);
return gender;
}
float percentageCalculator() {
float math, physics, chemistry, english, other, percent;
printf("\n >>> Enter math mark: ");
scanf("%f", &math);
printf("\n >>> Enter english mark: ");
scanf("%f", &english);
printf("\n >>> Enter physics mark: ");
scanf("%f", &physics);
printf("\n >>> Enter chemistry mark: ");
scanf("%f", &chemistry);
printf("\n >>> Enter additional subject mark: ");
scanf("%f", &other);
percent = ((math + english + physics + chemistry + other) / 500) * 100;
return percent;
}
int main() {
char * gender;
int age;
float percentage;
char *name = (char *) malloc(stringSize);
if (!name) exit(EXIT_FAILURE);
printf(">>> Enter your name: ");
fgets(name, sizeof(stringSize - 1), stdin);
name = (char *) realloc(name, strlen(name));
if (!name) exit(EXIT_FAILURE);
fseek(stdin, 0, SEEK_END);
printf("\n >>> Enter your age: ");
scanf("%d", &age);
gender = readGender();
percentage = percentageCalculator();
if (percentage < 33) {
printf("\n name : %s \n age : %d \n gender : %s \n Percentage : %f \n Status : Failed\n", name, age, gender, percentage);
} else if (percentage >= 33) {
printf("\n name : %s \n age : %d \n gender : %s \n Percentage : %f \n Status : Passed\n", name, age, gender, percentage);
} else {
printf("\n Error");
}
return 0;
}
Note that I've used fseek(...) as suggested here: How to clear input buffer in C? in order to read correctly char and integers together.
fseek(...) works on some systems; if not, then it is not surprising as nothing guarantees that it will work when standard input is an interactive device (or a non-seekable device like a pipe or a socket or a FIFO, to name but a few other ways in which it can fail).
If you need that it has to be portable, then check the link that I've placed before. Hope that it was helpful.
Next steps:
Adding user input error handling
Use a switch in 'readGender(...)' instead of the final triple if
Cheers, Denny
#include<stdio.h>
#include<stdlib.h>
/*
# Mistakes to avoid in future
1. use fgets(name_of_inputVariable,size,stdin) instead of scanf
2. use %s for strings rather than %c
3. use *name_of_variable for the strings
4. use fflush(stdin) only when program is skipping a certain step.
5. use switch statements for the character stuff.
*/
char* getGender(){
char *gender,g;
printf(">>> enter a gender (m/f/t): ");
scanf("%c",&g);
switch(g){
case 'm':
gender = "Male";
break;
case 'f':
gender = "Female";
break;
case 't':
gender = "Transgender";
break;
default:
gender = "prefer not to say";
}
return gender;
}
float percentageCalculator(){
float math,physics,chemistry,english,other,percent;
printf("\n >>> Enter maths marks: ");
scanf("%f",&math);
printf("\n >>> Enter english marks: ");
scanf("%f",&english);
printf("\n >>> Enter physics marks: ");
scanf("%f",&physics);
printf("\n >>> Enter chemistry marks: ");
scanf("%f",&chemistry);
printf("\n >>> Enter additional subject marks: ");
scanf("%f",&other);
percent = ((math+english+physics+chemistry+other)/500)*100;
return percent;
}
void main(){
char name[35];
char *gender;
int age,count;
float percent;
printf("\n >>> Enter your name: ");
fgets(name,35,stdin); //fgets is way better than scanf. you can use exact value 35
//scanf("%34s",&name); // scanf is a little buggy. you need to add one value less to prefent buffer overflow i.e. "%34s"
//fflush(stdin); // fflush do solved problem as it took entire name but only printed first character in output. It's fixed via fgets now.
printf("\n >>> Enter your age: ");
scanf("%d",&age);
fflush(stdin); // It was skipping gender ask step, so fflush fixed it by cleaning the overflow buffer. (fixed)
gender = getGender(); // getGender have some problem. It prints very absurd values in output console. (fixed)
percent = percentageCalculator(); // percentage calculator is working 100% fine. I got my accurate percentage and status lol ;)
if(percent < 33){
printf(" name : %s \n age : %d \n gender : %s \n Percentage : %f \n Status : Failed",name,age,gender,percent);
}else if(percent >= 33){
printf(" name : %s \n age : %d \n gender : %s \n Percentage : %f \n Status : Passed",name,age,gender,percent);
}else{
printf("\n Error");
}
}
Here I have finally fixed all issues in my code and now my program is working correctly. Thanks for some ideas stackoverflow community.
My code works fine, except for one line which keeps printing out the data that supposed to be for the next line only.
The output is supposed to look something like this:
Address Details
Street number: 100
Street name: Bedrock
Apartment: 14
Postal code: Z8Z 7R7
City: Markham
Instead my output is this:
Address Details
Street number: 100
Street name: Bedrock
Apartment: 14
Postal code: Z8Z 7R7Markham
City: Markham
As you can see Markham is printed along the same line of postal code.
Below are the potential files which might have caused this error. All help is much appreciated!
a1ms4.c file:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
// This source file needs to "know about" the structures you declared
// in the header file before referring to those new types:
// HINT: put the header file name in double quotes so the compiler knows
// to look for it in the same directory/folder as this source file
// #include your contacts header file on the next line:
#include "contacts.h"
int main(void)
{
// Declare variables here:
// Create a variable of type Contact and call it something self-describing like "contact"
// - HINT: Be sure to initialize the values to 0 and empty C strings
struct Contact contact = {
{ "", "", "" },
{0," ", 0, " ", " "},
{ "", "", "" }
};
// Display the title
printf("Contact Management System\n");
printf("-------------------------\n");
// Call the Contact function getName to store the values for the Name member
getName(&contact.name);
// Call the Contact function getAddress to store the values for the Address member
getAddress(&contact.address);
// Call the Contact function getNumbers to store the values for the Numbers member
getNumbers(&contact.numbers);
// Display Contact summary details
printf("\nContact Details\n");
printf("---------------\n");
printf("Name Details\n");
printf("First name: %s", contact.name.firstName);
printf("\n");
printf("Middle initial(s): %s", contact.name.middleInitial);
printf("\n");
printf("Last name: %s", contact.name.lastName);
printf("\n");
printf("\n");
printf("Address Details\n");
printf("Street number: %d", contact.address.streetNumber);
printf("\n");
printf("Street name: %s", contact.address.street);
printf("\n");
printf("Apartment: %d", contact.address.apartmentNumber);
printf("\n");
printf("Postal code: %s", contact.address.postalCode);
printf("\n");
printf("City: %s", contact.address.city);
printf("\n");
printf("\n");
printf("Phone Numbers:");
printf("\n");
printf("Cell phone number: %s", contact.numbers.cell);
printf("\n");
printf("Home phone number: %s", contact.numbers.home);
printf("\n");
printf("Business phone number: %s", contact.numbers.business);
printf("\n");
printf("\n");
// Display Completion Message
printf("Structure test for Contact using functions done!\n");
return 0;
}
contacts.c file
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
// This source file needs to "know about" the structures you declared
// in the header file before referring to those new types:
// HINT: put the header file name in double quotes so the compiler knows
// to look for it in the same directory/folder as this source file
// #include your contacts header file on the next line:
#include "contacts.h"
// Get and store from standard input the values for Name
// Put your code here that defines the Contact getName function:
void getName(struct Name * name) {
//Variable/Struct declaration:
char notify;
// Contact Name Input:
printf("Please enter the contact's first name: ");
scanf("%30s", name->firstName);
//Prompts the user to see if they want to enter a middle name using y, yes or n, no.
printf("Do you want to enter a middle initial(s)? (y or n): ");
scanf("%s", ¬ify);
if (notify == 'y' || notify == 'Y')
{
printf("Please enter the contact's middle initial(s): ");
scanf("%6s", name->middleInitial);
}
printf("Please enter the contact's last name: ");
scanf("%30s", name->lastName);
}
// Get and store from standard input the values for Address
// Put your code here that defines the Contact getAddress function:
void getAddress(struct Address * address) {
//Variable/Structure declaration:
char notify;
// Contact Address Input:
printf("Please enter the contact's street number: ");
scanf("%d",& (address->streetNumber));
printf("Please enter the contact's street name: ");
scanf("%40s", address->street);
//Prompts the user to see if they want to enter an apartment number using yes, or no.
printf("Do you want to enter an apartment number? (y or n): ");
scanf("%s", ¬ify);
if (notify == 'y' || notify == 'Y')
{
printf("Please enter the contact's apartment number: ");
scanf("%d",& (address->apartmentNumber));
}
printf("Please enter the contact's postal code: ");
scanf(" %[^\n]", address->postalCode);
printf("Please enter the contact's city: ");
scanf(" %40s", address->city);
}
// Get and store from standard input the values for Numbers
// Put your code here that defines the Contact getNumbers function:
void getNumbers(struct Numbers *numbers) {
//Variable/Structure declaration:
char notify;
// Contact Numbers Input:
printf("Do you want to enter a cell phone number? (y or n): "); //Prompt the user to see if they want to enter a cellphone.
scanf(" %s", ¬ify);
if (notify == 'y' || notify == 'Y')
{
printf("Please enter the contact's cell phone number: ");
scanf("%20s", numbers->cell);
}
printf("Do you want to enter a home phone number? (y or n): ");
scanf(" %s", ¬ify);
if (notify == 'y' || notify == 'Y')
{
printf("Please enter the contact's home phone number: ");
scanf("%20s", numbers->home);
}
printf("Do you want to enter a business phone number? (y or n): ");
scanf(" %s", ¬ify);
if (notify == 'y' || notify == 'Y')
{
printf("Please enter the contact's business phone number: ");
scanf("%20s", numbers->business);
}
}
contacts.h
// Structure type Name declaration (Milestone 1)
struct Name {
char firstName[31];
char middleInitial[7];
char lastName[36];
};
// Structure type Address declaration
// Place your code here... (from Milestone 1)
struct Address {
int streetNumber;
char street[40];
int apartmentNumber;
char postalCode[7];
char city[40];
};
// Structure type Numbers declaration
// Place your code here... (from Milestone 1)
struct Numbers {
char cell[20], home[20], business[20];
};
you should have a null terminator on the end of the postal code:
struct Address {
int streetNumber;
char street[40];
int apartmentNumber;
char postalCode[8];// add space for the null terminator
char city[40];
};
printf("Please enter the contact's postal code: ");
scanf(" %[^\n]", address->postalCode);
address->postalCode[7] = '\0'; // add a null terminator
this should be enough to solve the issue
I've got a program for taking input and storing it into structs. The structs are for contact details, Name, Address, and Phone numbers. My program works just fine, I can enter all my information into the program however when I try to printf the results the program crashes halfway through. I think they may be an issue with memory or corruption, or something. The reason I think it may be with corruption is because if I cut off some of my program and compile it, instead of the program crashing I get a 'Run-Time check failure #2 Stack around the variable 'optionAddress' was corrupted' error. Here's my program
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include "contacts.h"
int main(void)
{
// Declare variables here:
struct Name names;
char optionName;
struct Address addresses;
char optionAddress;
struct Numbers number;
char optionCell;
char optionHome;
char optionBusiness;
// Display the title
printf("Contact Management System\n");
printf("-------------------------\n");
// Contact Name Input:
printf("Please enter the contact's first name: ");
scanf("%s", &names.firstName);
printf("Do you want to enter a middle initial(s)? (y or n): ");
scanf("%s", &optionName);
while (optionName == 'y' || optionName == 'Y') {
printf("Please enter the contact's middle initial(s): ");
scanf("%s", &names.middleInitial);
break;
}
printf("Please enter the contact's last name: ");
scanf("%s", &names.lastName);
// Contact Address Input:
printf("Please enter the contact's street number: ");
scanf("%s", &addresses.streetNumber);
printf("Please enter the contact's street name: ");
scanf("%s", &addresses.street);
printf("Do you want to enter an apartment number? (y or n): ");
scanf("%s", &optionAddress);
while (optionAddress == 'y' || optionAddress == 'Y') {
printf("Please enter the contact's apartment number: ");
scanf(" %c", &addresses.apartmentNumber);
break;
}
printf("Please enter the contact's postal code: ");
scanf("%s", &addresses.postalCode);
printf("Please enter the contact's city: ");
scanf("%s", &addresses.city);
// Contact Numbers Input:
printf("Do you want to enter a cell phone number? (y or no): ");
scanf("%s", &optionCell);
while (optionCell == 'y' || optionCell == 'Y') {
printf("Please enter the contact's cell phone number: ");
scanf(" %c", number.cell);
break;
}
printf("Do you want to enter a home phone number? (y or n): ");
scanf("%s", &optionHome);
while (optionHome == 'y' || optionHome == 'Y') {
printf("Please enter the contact's home phone number: ");
scanf(" %c", &number.home);
break;
}
printf("Do you want to enter a business phone number? (y or n): ");
scanf("%s", &optionBusiness);
while (optionBusiness == 'y' || optionBusiness == 'Y') {
printf("Please enter the contact's business phone number: ");
scanf(" %c", number.business);
break;
}
// Display Contact Summary Details
printf("Contact Details\n");
printf("---------------\n");
printf("Name Details\n");
printf("First name: ");
printf("%s", names.firstName);
printf("\nMiddle initials(s): ");
printf("%s", names.middleInitial);
printf("\nLast name: ");
printf("%s", names.lastName);
printf("\n\nAddress Details\n");
printf("Street number: ");
printf("%s", addresses.streetNumber);
printf("\nStreet name: ");
printf("%s", addresses.street);
printf("\nApartment: ");
printf("%s", addresses.apartmentNumber);
printf("\nPostal code: ");
printf("%s", addresses.postalCode);
printf("\nCity: ");
printf("%s", addresses.city);
printf("\n\nPhone Numbers: ");
printf("\nCell phone number: ");
printf("%s", number.cell);
printf("\nHome phone number: ");
printf("%s", number.home);
printf("\nBusiness phone number: ");
printf("%s", number.business);
// Display Completion Message
printf("\n\nStructure test for Name, Address, and Numbers Done!");
return 0;
}
And the structs in the header file:
// Structure type Name declaration
struct Name {
char firstName[31];
char middleInitial[7];
char lastName[36];
};
// Structure type Address declaration
// Place your code here...
struct Address {
char streetNumber;
char street[41];
char apartmentNumber;
char postalCode[8];
char city[41];
};
// Structure type Numbers declaration
// Place your code here...
struct Numbers {
char cell[21];
char home[21];
char business[21];
};
My program reaches the point of printing out "Street number: " then stops working. It is a windows error window that shows up, not a compiler window.
My program reaches the point of printing out "Street number: " then
stops working.
Take a look at:
struct Address {
char streetNumber; // declared as character!
char street[41];
char apartmentNumber; // character
char postalCode[8];
char city[41];
};
char optionName;
char optionAddress;
char optionCell;
char optionHome;
char optionBusiness;
Later on you try to read the values to the characters:
scanf("%s", &optionName);
scanf("%s", &optionAddress);
scanf("%s", &optionCell);
scanf("%s", &optionHome);
scanf("%s", &optionBusiness);
scanf("%s", &addresses.streetNumber); // <-------------- string read
using string format %s. That invokes UB since you are reading at least 2 bytes. "y" is a string with string null terminator '\0'. That extra byte overwrites the memory location.
Change declarations of variables to strings or reading format to character read as you already do here:
scanf(" %c", &addresses.apartmentNumber);
scanf("%s", &optionName);
This invites undefined behavior which could lead to run time error you are getting as optionName is char one byte and scanf() with %s will try to write '\0' which will surely go beyond bounds
This is a program that asks the user to input Shipping information about selling bikes, pretty lame. At the end when it prints out the number of bikes order, and the total cost, the numbers get screwed up. The previously entered amounts seem to be sticking in the memory. How do I fix this? If that is not the problem I would not mind being told so :)
#include <stdio.h>
#include <math.h>
//structure
typedef struct
{char cust_name[25];
char add_one[20];
char add_two[20];
}ORDER;
ORDER order;
int main(void){
fflush(stdin);
system ( "clear" );
//initialize variables
double number_ordered = 0;
double price;
char bike;
char risky;
double m = 359.95;
double s = 279.95;
//inputs for order
printf("Enter Customer Information\n");
printf("Customer Name: ");
scanf(" %[^\n]s", &order.cust_name);
printf("\nEnter Street Address: ");
scanf(" %[^\n]s", &order.add_one);
printf("\nEnter City, State, and ZIP: ");
scanf(" %[^\n]s", &order.add_two);
printf("\nHow Many Bicycles Are Ordered: ");
scanf(" %d", &number_ordered);
printf("\nWhat Type Of Bike Is Ordered\n M Mountain Bike \n S Street Bike");
printf("\nChoose One (M or S): ");
scanf(" %c", &bike);
printf("\nIs The Customer Risky (Y/N): ");
scanf(" %c", &risky);
system ( "clear" );
//print order
printf("\n**********Shipping Instructions**********");
printf("\nTo: %s\n %s\n %s", order.cust_name, order.add_one, order.add_two);
if (bike == 'M' || bike == 'm')
printf("\n\nShip: %d Mountain Bikes", number_ordered);
else
printf("\n\nShip: %d Street Bikes", number_ordered);
if (bike == 'M' || bike == 'm')
price = number_ordered * m;
else
price = number_ordered * s;
if (risky == 'Y' || risky == 'y')
printf("\nBy Freight, COD %d\n", price);
else
printf("\nBy Freight, And Bill The Customer %d\n", price);
printf("*****************************************\n");
return 0;
}
You are printing number_ordered and price, which are doubles, using %d. %d is only for integer types. Use %lf to printf or scanf doubles.
The formats for both your scanf and your printf are wrong, so you're neither reading nor writing your values properly.