2

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:

  1. price

  2. number of months for instalment (can only choose 24 or 36 months, please do input validation)

  3. 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

6
  • You forgot to ask a question and tell us exactly what is failing and how. Commented Oct 1, 2013 at 13:10
  • And what is your problem with that code? Besides not compiling (I guess). Please edit your question to include complete and unedited error messages. Commented Oct 1, 2013 at 13:10
  • How many cars or customers are you supposed to process? Commented Oct 1, 2013 at 13:10
  • 1
    A couple of small tips: Read more about arrays, pointers and local versus global variables. Commented Oct 1, 2013 at 13:12
  • "I'm not good at programming but its a mandatory module." - and this is our fault? Commented Oct 1, 2013 at 14:28

2 Answers 2

0

You are getting a Car* in the function. Use that, i.e.

void collecInfo(Car *p){ //Pre-define in question, can't change
    printf("Price of Car : ");
    scanf("%d",&(p->price));
    ...
Sign up to request clarification or add additional context in comments.

Comments

0

Shouldn't this: scanf("%d",&customer1[i].numofmonth);

be

scanf("%u",&customer1[i].numofmonth);

And also:

scanf("%d",&customer1[i].price);

be

scanf("%f",&customer1[i].price);

The format specifier should match the type of variable. unsigned int used %u and float uses %f.

1 Comment

But it never uses p... So we must make the question clear first.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.