The 50 years old is back for help

This is a discussion on The 50 years old is back for help within the C Programming forums, part of the General Programming Boards category; Hello guys, This is the 50 years old MOm who go back to school and get C .Well,still have problem ...

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    7

    Wink The 50 years old is back for help

    Hello guys, This is the 50 years old MOm who go back to school and get C .Well,still have problem with my assignment. I got an error message when I select number 4 to display my receipt The program will shut down and so I cannot do anything to place all this:
    This is what I need to do here:
    Should display all customer / rental input and computed information. The output should be nicely formatted and include a tabular summary of the costs. Any input codes should be displayed as the "string" value they represent (e.g. category code of N shuld display New and Y for coupon should display Yes). The output might look like the following:

    Days Coupon
    No. Video Title Category Rented Applied Late/Lost Charge
    === ============================== ========
    1. ======== New 0 No Lost 75.00
    2. ====== Recent 10 Yes 14.96
    3.======== Budget 91 No Late 19.00

    Subtotal: $108.96
    Tax (8.6%): $9.37
    Total Due: $118.33
    here is my program which give me an error when I select 4 so I cannot do anything. I tried to compute it before but still won't compile:
    //header
    #include <stdio.h>
    #include <stdlib.h>

    #define MAX_NAME 30
    #define TITLE "Mabuhay Video Store"
    #define MAX_VIDEO 10

    void getcustomer(char Fullname[MAX_NAME + 1],char Address[MAX_NAME + 1],char City[MAX_NAME + 1],char Y);
    void getVideos(char Fullname[MAX_NAME + 1],char Y,char VideoTitle[MAX_NAME + 1],char Category, int DaysRented);
    void computecharges(double price, int DaysRented, char Category, char Y);
    void displayreceipt(char Fullname[MAX_NAME + 1],char Address[MAX_NAME + 1],char City[MAX_NAME + 1],char Y, char VideoTitle[MAX_NAME + 1],char Category,int DaysRented,char CouponApplied[MAX_NAME + 1], char Late[MAX_NAME + 1], float Charge);

    int main(void)
    {
    int choice;
    char Fullname[MAX_NAME + 1];
    char Address[MAX_NAME + 1];
    char City[MAX_NAME + 1];
    char Y = 'N';
    char VideoTitle[MAX_NAME + 1];
    char Category = 'R';
    int DaysRented = 0;
    double price = 0.0;
    char No ='N';
    char CouponApplied[MAX_NAME + 1];
    char Late[MAX_NAME + 1];
    float Charge = 0.0f;

    do
    {
    system ("cls");
    printf("=============================\n");
    printf(" %s\n", TITLE);
    printf("=============================\n\n");
    printf("1 Enter Customer Information\n");
    printf("2 Enter Rental Information\n");
    printf("3 Compute Charges\n");
    printf("4 Display Receipt\n");
    printf("5 Exit Program\n\n");
    printf(" Selection: ");
    scanf("%d",&choice);
    getchar();

    if(choice==1)
    {
    getcustomer(Fullname,Address,City,Y);
    }
    else if(choice==2)
    {
    getVideos(Fullname,Y, VideoTitle,Category, DaysRented);
    }
    else if(choice==3)
    {
    computecharges(price, DaysRented, Category, Y);
    }
    else if(choice==4)
    {
    displayreceipt(Fullname, Address, City, Y, VideoTitle, Category, DaysRented, CouponApplied, Late, Charge);
    }
    else
    {
    break;
    }
    }while(choice!=5);

    return EXIT_SUCCESS;
    }
    //header
    void getcustomer(char Fullname[MAX_NAME + 1],char Address[MAX_NAME + 1],char City[MAX_NAME + 1],char Y)
    {
    system("cls");

    printf("Store Name: %s\n\n", TITLE);
    printf("Customer Name: ");
    gets(Fullname);
    printf(" Address: ");
    gets(Address);
    printf(" City/State/Zip: ");
    gets(City);
    printf("Coupon Presented (Y/N): ");
    scanf("%c", &Y);
    getchar();

    }
    //header
    void getVideos(char Fullname[MAX_NAME + 1],char Y,char VideoTitle[MAX_NAME + 1],char Category, int DaysRented)
    {
    int i;

    system("cls");


    printf("Store Name: %s\n\n", TITLE);
    printf("Customer Name: %s\n", Fullname);
    printf("Coupon Presented: %c\n\n", Y);
    for (i = 0; i < MAX_VIDEO; i++)
    {
    printf("%i. VideoTitle: ", i + 1);
    gets(VideoTitle);
    printf("Category(N,R,C,B): ");
    scanf("%c",&Category);
    printf("DaysRented: ");
    scanf("%d", &DaysRented);
    getchar();
    }

    }
    //header
    void computecharges(double price, int DaysRented, char Category,char Y)
    {

    if (Category == 'N')
    price = DaysRented * 2.99;
    else if (Category =='R')
    {
    price = DaysRented * 3.99;
    if (Y == 'Y')
    price = price - 1;
    }
    else if (Category == 'C')
    {
    price = DaysRented * 2.99;
    if (Y == 'Y')
    price = price - 1;
    }
    else if (Category == 'B')
    price = DaysRented * 1.00;

    else if (DaysRented == 0)
    price = 75;

    else if (DaysRented == 90)

    price = 75;
    price = price + price * 0.086;
    }

    //header
    void displayreceipt(char Fullname[MAX_NAME + 1],char Address[MAX_NAME + 1],char City[MAX_NAME + 1],char Y, char VideoTitle[MAX_NAME + 1],char Category,int DaysRented,char CouponApplied[MAX_NAME + 1], char Late[MAX_NAME + 1], float Charge)
    {
    int i;
    system("cls");

    printf("Store Name: %s\n\n", TITLE);
    printf("Customer Name: %s", Fullname);
    printf(" Address: %s", Address);
    printf(" City/State/Zip: %s", City);
    printf("Coupon Presented (Y/N): %c", Y);

    printf(" Days Coupon \n");
    printf("No. Video Title Category Rented Applied Late/Lost Charge\n");
    printf("=== =================== ======== ====== ======= ========= ======\n");
    for (i = 0; i < MAX_VIDEO; i++)
    {
    printf("%i. %s %s %d %s %s %f\n", i + 1, VideoTitle, Category, DaysRented, Y, Late, Charge);
    }
    }

    Please help as this is due tomorrow: Thank you again for all your help guys.

  2. #2
    Unregistered
    Guest
    Haven't had much time to look at all your code but spotted this in your displayreceipt function.

    printf("%i. %s %s %d %s %s %f\n", i + 1, VideoTitle, Category, DaysRented, Y, Late, Charge);

    the first format specifier should be %d not %i

    Let me know if this helps.

    Dav

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Unregistered
    Haven't had much time to look at all your code but spotted this in your displayreceipt function.

    printf("%i. %s %s %d %s %s %f\n", i + 1, VideoTitle, Category, DaysRented, Y, Late, Charge);

    the first format specifier should be %d not %i

    Let me know if this helps.

    Dav
    %d and %i are the same thing. Quoting [http://www.rt.com/man/printf.3.html]this source[/url]:

    diouxX
    The int (or appropriate variant) argument is con-
    verted to signed decimal (d and i), unsigned octal
    (o), unsigned decimal (u), or unsigned hexadecimal
    (x and X) notation. The letters abcdef are used
    for x conversions; the letters ABCDEF are used for
    X conversions. The precision, if any, gives the
    minimum number of digits that must appear; if the
    converted value requires fewer digits, it is padded
    on the left with zeros.
    D or I work for integers. They are interchangable.

    I'll correct as I go:

    Code:
    void displayreceipt(char Fullname[], char Address[],char City[],char Y, char VideoTitle[],char Category,int DaysRented,char CouponApplied[], char Late[], float Charge)
    {
        /**
        *** There is no real reason to
        *** specify the sizes of the arrays
        *** in the paraters, so I removed
        *** them.
        **/
    
        int i;
    
        /**
        *** I wouldn't use this, but that's
        *** just me.
        **/
        system("cls");
    
    
        printf("Store Name: %s\n\n", TITLE);
        printf("Customer Name: %s", Fullname);
        printf(" Address: %s", Address);
        printf(" City/State/Zip: %s", City);
        printf("Coupon Presented (Y/N): %c", Y);
        printf(" Days Coupon \n");
        printf("No. Video Title Category Rented Applied Late/Lost Charge\n");
        printf("=== =================== ======== ====== ======= ========= ======\n");
    
        /**
        *** You may be interested in this
        *** little tidbit which is used for
        *** OUTPUT streams. Here I
        *** use it to flush the output so it
        *** ends up getting to the screen.
        ***
        *** In this case, we use it as an
        *** indicator to see how far our
        *** program gets before crashing.
        **/
         fflush( stdout );
    
        for (i = 0; i < MAX_VIDEO; i++)
        {
            printf("%i. %s %s %d %s %s %f\n", i + 1, VideoTitle, Category, DaysRented, Y, Late, Charge);
        }
    }
    Well basicly that all looks good. (I'm on the phone, but you can generally throw printf/fflush(stdout) calls in your program to easily troubleshoot how far your program gets before it crashes.


    Quzah.
    Last edited by quzah; 07-30-2002 at 09:32 PM.
    Hope is the first step on the road to disappointment.

  4. #4
    Unregistered
    Guest
    sorreeee!

    memory isn't what it used to be. Or was it ever... I can't remember.

  5. #5
    Unregistered
    Guest
    Purely curiosity

    Is it possible to use %s when all you want to do is display a single character? (as in the printf() of the displayreceipt function)
    Though I imagine it's not desirable.

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Unregistered
    Purely curiosity

    Is it possible to use %s when all you want to do is display a single character? (as in the printf() of the displayreceipt function)
    Though I imagine it's not desirable.
    %s is for printing strings. If the string is only one character long (not including the null terminator), then it will print only one character. But, if the string is longer...... well, I think you can work that one out!
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Unregistered
    Guest
    Its not perfect but it works.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>  /*for toupper*/
    #include <string.h> /*needed for strcpy()*/
    
    #define MAX_NAME 30 
    #define TITLE "Mabuhay Video Store" 
    #define MAX_VIDEO 10 
    
    void getcustomer(char Fullname[],char Address[],char City[],
                     char CouponApplied[]);
    
    void getVideos(char Fullname[],char CouponApplied[],char VideoTitle[][MAX_NAME + 1],
                   char Category[], int DaysRented[]);
    
    void computecharges(float price[], int DaysRented[], char Category[]);
    
    void displayreceipt(char Fullname[],char Address[],char City[],
                        char VideoTitle[][MAX_NAME + 1],char Category[],
                        int DaysRented[],char CouponApplied[],
                        char Late[][5],float price[]);
    
    void initialise(char VideoTitle[][MAX_NAME + 1],char Category[],char Late[][5],
                    float price[],int DaysRented[]);
    
    float Tax, Charge;
    char Y;
    
    int main(void) 
    { 
      int choice;
      int DaysRented[MAX_VIDEO];
    
      float Tax, Charge;
      float price[MAX_VIDEO];
    
      char No ='N';               /*you don't use this in your prog*/
    
      char Fullname[MAX_NAME + 1];
      char Address[MAX_NAME + 1];
      char City[MAX_NAME + 1];
    
      char VideoTitle[MAX_VIDEO][MAX_NAME + 1]; /*allow ten titles*/
      char Category[MAX_VIDEO + 1];             /*allow ten categories*/
      char CouponApplied[4]; /*YES or NO    3 + null*/
      char Late[MAX_VIDEO][5]; /*does this string hold either late or lost, if
                                so then 4 + null is all you need
                                You don't seem to allow the user to input a value
                                for this but you use it in displayreceipt()*/
    
    
      do
      {
        system ("cls");
        printf("=============================\n");
        printf(" %s\n", TITLE);
        printf("=============================\n\n");
        printf("1 Enter Customer Information\n");
        printf("2 Enter Rental Information\n");
        printf("3 Compute Charges\n");
        printf("4 Display Receipt\n");
        printf("5 Exit Program\n\n");
        printf(" Selection: ");
        scanf("%d",&choice);
        getchar();
    
        if(choice==1)
        {
          /*intialise next customer*/
          initialise(VideoTitle,Category,Late,price,DaysRented);
          getcustomer(Fullname,Address,City,CouponApplied);
        }
        else if(choice==2)
        {
          getVideos(Fullname,CouponApplied, VideoTitle,Category, DaysRented);
        }
        else if(choice==3)
        {
          computecharges(price, DaysRented, Category);
        }
        else if(choice==4)
        {
          displayreceipt(Fullname, Address, City,VideoTitle, Category,
                         DaysRented, CouponApplied, Late,price);
        }
        else
        {
          break;
        }
    
      }
      while(choice!=5);
    
      return EXIT_SUCCESS;
    }
     
    //header 
    void getcustomer(char Fullname[], char Address[], char City[],
                     char CouponApplied[])
    { 
      system("cls");
    
      printf("Store Name:      %s\n\n", TITLE);
      printf("Customer Name:   ");
      gets(Fullname);
      printf("Address:         ");
      gets(Address);
      printf("City/State/Zip:  ");
      gets(City);
      printf("Coupon (Y/N):    ");
      /*this may be better it's up to you or anyone else who
      knows better*/
      /*scanf("%c", &Y);*/
      Y = toupper(getchar());
    
      /*guessing this is what you want*/
      if(Y == 'Y')
        strcpy(CouponApplied, "YES");
      else
        strcpy(CouponApplied, "NO");
    
    }
     
    //header 
    void getVideos(char Fullname[],char CouponApplied[],char VideoTitle[][MAX_NAME + 1],
                   char Category[], int DaysRented[])
    { 
      int i, count;
    
      char user_resp; /*users response*/
    
      system("cls");
    
      printf("Store Name:       %s\n\n", TITLE);
      printf("Customer Name:    %s\n", Fullname);
      printf("Coupon Presented: %s\n\n", CouponApplied);
    
    
      /*user has to rent 10 videos using for loop
      surely you have to allow for any number of
      videos between 1 and 10 inclusive*/
      printf("How many videos > ");
      scanf("%d",&count);
      getchar();
    
    
    
      for (i = 0; i < count; i++)
      {
    
        printf("%i. VideoTitle: ", i+1);
        gets(VideoTitle[i]);
        printf("Category(N,R,C,B): ");
        scanf("%c",&Category[i]);
        printf("DaysRented: ");
        scanf("%d", &DaysRented[i]);
        getchar();
      }
    
    }
     
    //header 
    void computecharges(float price[], int DaysRented[], char Category[])
    {
      int i;
    
      i = 0;
    
      Tax = Charge = 0.0;
    
      while((Category[i] != ' ') && (Category[i] != '\0'))
      {
      if (Category[i] == 'N')
        price[i] = DaysRented[i] * 2.99;
    
      else if (Category[i] =='R')
      {
        price[i] = DaysRented[i] * 3.99;
        if (Y == 'Y')
          price[i] = price[i] - 1.0;
      }
    
      else if (Category[i] == 'C')
      {
        price[i] = DaysRented[i] * 2.99;
        if (Y == 'Y')
          price[i] = price[i] - 1.0;
      }
    
      else if (Category[i] == 'B')
        price[i] = DaysRented[i] * 1.0;
    
      else if (DaysRented[i] == 0)  /*Don't know*/
        price[i] = 75.0;            /*why price*/
                                    /*should equal*/
      else if (DaysRented[i] == 90) /*75. Dollars I assume?*/
        price[i] = 75.0;
    
      Charge += price[i];
      i++;
        }
        Tax = 0.086 * Charge;
    
      /*  price = price + price * 0.086;  price + price * 86????*/
    
    } 
    
    //header 
    void displayreceipt(char Fullname[],char Address[],char City[],
                        char VideoTitle[][MAX_NAME + 1],char Category[],int DaysRented[],
                        char CouponApplied[], char Late[][5], float price[])
    {
      int i = 0;
      system("cls");
    
      printf("Store Name     : %s\n\n", TITLE);
      printf("Customer Name  : %s\n", Fullname);
      printf("Address        : %s\n", Address);
      printf("City/State/Zip : %s\n", City);
      printf("Coupon (Y/N)   : %c", Y);
    
      printf("Days Coupon\n");
      printf("No. Video Title                   Cat Rent Late/Lost Price\n");
      printf("=== ===========                   === ==== ========= =====\n");
      while((Category[i] != ' ') && (Category[i] != '\0'))
      {
        printf("%-4i%-31s%-5c%-4d%-10s%.2f\n", i + 1, VideoTitle[i], Category[i], DaysRented[i],
                                            Late[i], price[i]);
        i++;
      }
    
      printf("\n\nSubtotal    : %5.2f"
              "\nTax (8.6%%)  : %5.2f"
               "\nTotal Due   : %5.2f", Charge,Tax,Tax+Charge);
    
      printf("\n\nHit any key to continue");
      rewind(stdin);
      getch();
    
    }
    
    void initialise(char VideoTitle[][MAX_NAME + 1],char Category[],char Late[][5],
                    float price[],int DaysRented[])
    {
      /*you might want to add or remove items to be initialised, just did this to 
        avoid random results when accessing values*/
      int i;
    
      for(i = 0; i < MAX_VIDEO; i++)
      {
        strcpy(VideoTitle[i], "\0");
        strcpy(Late[i], " ");
        Category[i] = ' ';
        price[i] = 0.0;
        DaysRented[i] = 0;
      }
      Category[i] = '\0';
    }
    There are better ways to do this but I'm sure you haven't got that far in your studies yet.
    Good luck.

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    Is it possible to use %s when all you want to do is display a single character? (as in the printf() of the displayreceipt function)
    Certainly, it's easy. If you say %.1s then it will print out a single character from a multi-character string.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  9. #9
    Registered User
    Join Date
    Jul 2002
    Posts
    7

    Thumbs up Thanks unregistered 22723

    The 50 years old mom wants to thanks all who help me with my assignments and especially to this unregistered person who give his time to go all the way to make this code works.

    Thanks
    Mom from Seattle WA

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Delete Files Older than 2 years
    By Dragoon_42 in forum C++ Programming
    Replies: 10
    Last Post: 05-17-2007, 04:13 PM
  2. Video Games Industry. 5 years left.
    By Cheeze-It in forum A Brief History of Cprogramming.com
    Replies: 26
    Last Post: 12-10-2002, 09:52 PM
  3. Back to programming?
    By Korwin in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 01-23-2002, 05:51 AM
  4. Its a shame for most ppl, but.... Ill be back....
    By Null Shinji in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 12-09-2001, 01:59 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21