Be honest, it's a homework. However I've spend 2 days and got no result. I'm not good at programming but its a mandatory module.
The question required to build a struct with typedef, then write a function for collect information with the struct and use pointer method.
Below is the code, only including the struct and function part, for other things I still want do myself. Please trust me, I've try my best to do it
//Assignment.cpp
#include <stdio.h> //Pre-define in question, can't change
#include <ctype.h> //Pre-define in question, can't change
#include <stdlib.h> //Pre-define in question, can't change
//Requirement: Use typedef to create a struct called Car
typedef struct Car{
float price;
unsigned int numofmonth;
unsigned char member;
}Car;
Car *p
struct Car customer1;
struct Car *p=&customer1;
//Function phototype
void collectInfo(Car *p); //pre-define in question, can't change
void printInfo(Car p); //Pre-define in question, can't change //Once the Collect info part done, I will do it myself
int main(){ //Pre-define in question, can't change
Car customer1; //Pre-define in question, can't change
collectInfo(&customer1); //Pre-define in question, can't change
printInfo(customer1); //Pre-define in question, can't change
system("pause"); //Pre-define in question, can't change
return 0; //Pre-define in question, can't change
}
//Function defintions // The problem I want to ask, how to make it work? Thanks
void collecInfo(Car *p){ //Pre-define in question, can't change
for(int i = 0; i < 3;i++){
printf("Price of Car : ");
scanf("%d",&customer1[i].price);
printf("Perferred Months for Installment : ");
scanf("%d",&customer1[i].numofmonth);
printf("Perferred Months for Installment : ");
scanf("%c",&customer1[i].member);
printf("\n");
}
}
Thanks for all your comments.
Actually, it's a question about "loan calculation". The requirement is use typedef, struct, pointer and function to complete the program and I just have simple idea about pointer. As I know it is for point to address permanently.
Here is the full question and the code I've done until now.
Q: have below requirement and do a program for car instalment like below screendump:
price
number of months for instalment (can only choose 24 or 36 months, please do input validation)
if the customer joined member.
Variable: - 24 months interest rate will be 10%
36 months interest rate will be 15%
if joined member, have a lump sum $3000 refund on total loan
First month payment will be 20% of total loan
Sample screendump:
Price of car: 30000
Preferred months for instalment: 36
Had you join our member (y/n): y
=================
Print out detail:
=================
Total loan: 31500
First Month payment: 6300
Monthly Payment: 700
And below is the code I still doing now
//Assignment.cpp
#include <stdio.h> //Pre-define in question, can't change
#include <ctype.h> //Pre-define in question, can't change
#include <stdlib.h> //Pre-define in question, can't change
//Use typedef to create a struct called Car
typedef struct Car{
float price;
unsigned int numofmonth;
unsigned char member;
}Car;
Car *p
struct Car customer1;
struct Car *p=&customer1;
//Function phototype
void collectInfo(Car *p); //pre-define in question, can't change
void printInfo(Car p); //Pre-define in question, can't change
int main(){ //Pre-define in question, can't change
Car customer1; //Pre-define in question, can't change
collectInfo(&customer1); //Pre-define in question, can't change
printInfo(customer1); //Pre-define in question, can't change
system("pause"); //Pre-define in question, can't change
return 0; //Pre-define in question, can't change
}
//Function defintions
void collecInfo(Car *p){ //Pre-define in question, can't change
int interest;
int lumpsum;
printf("Price of Car : ");
scanf("%f",&(p->price));
//check if the installment is 24 or 36
printf("Perferred Months for Installment : ");
scanf("%u",&(p->numofmonth));
if(p->numofmonth == 24)
interest=0.1;
else if(p->numofmonth == 36)
interest=0.15;
else
printf("Sorry, we only accept 24 or 36 months installment");
printf("Are you our member (y/n) : ");
scanf("%u",(p->member));
//check if the member = y or n
if(p->member == 'y')
lumpsum=-3000;
else if(p->member == 'n')
lumpsum=0;
else
printf("Please only input 'y' or 'n'");
printf("\n");
}
//Show result on screen, still doing, have problem to display result of pointer...
void printInfo(Car p){
printf("Price of the Car: %.2f\n", customer1.price);
printf("Preferred Months for Installment : %u\n", customer1.numofmonth);
printf("Are you our member (y/n) : %u\n", customer1.member);
printf("========================================\n");
printf("See the installment details below\n");
printf("========================================\n\n");
float total;
total= // still doing, have problem to display result of pointer...
}
After lot of google, I trying to use malloc in the function collecInfo