Thread: Need some help with a beginner functions program!

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    1

    Need some help with a beginner functions program!

    Hi everyone, I could really use your help trying to figure this out! I've had no problem understanding everything leading up to functions (printf, scanf, calculations, validation, etc.) but I cannot figure out how to do these functions for the life of me!

    I was just finally able to get rid of all the bugs and run the program, but it only gets to "enter a quantity greater than or equal to 0" and no matter what number I enter it goes to the error and loops back.


    Here is the assignment and code:


    The program will track the amount and price of raffle tickets sold, and then add to a grand total. The price per raffle ticket has to be equal to or greater than $5.00, amount of tickets has to be equal to or greater than 0.

    I've planned the program out to look something like this at the end (aside from changes in character strings)--

    WELCOME TO THE RAFFLE!

    Enter price per raffle ticket (must be at least $5.00):

    Enter number of raffle tickets (must be at least 0):

    Number of tickets ---------->
    Price per ticket ------------->
    Total amount due ---------->

    Would you like to make another raffle ticket purchase? (y/n)

    /* If y, loop back to beginning. If n, proceed to next. */

    Grand total of all tickets ---->

    /*==============================================*/


    My code:

    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <stdlib.h>
    
    void heading(void);
    void quanerror(double min);
    void costerror(double min);
    double getfloat(char prompt[], double min);
    double getdouble(char prompt[], double min);
    double total(double ticketquan, double ticketcost);
    void show(double ticketquan, double ticketcost, double tickettotal, double grandtotal);
    int contyn(char msg[]);
    
    int main()
    {
    	double quanmin, ticketquan, ticketcost, tickettotal, costmin, grandtotal;
    	double multtotal;
    	
    	char choice;
    
    	quanmin = 0;
    	costmin = 5;
    
    do
    {
    heading();
    	ticketquan = getdouble("quantity", quanmin);
    	ticketcost = getfloat("price", costmin);
    	tickettotal= total(ticketquan, ticketcost);
    	grandtotal+= tickettotal;
    	choice  = contyn("\nWould you like to purchase more raffle tickets?");
    	show(ticketquan, ticketcost, tickettotal, grandtotal);
    	
    	}
    while (choice == 'y' || choice == 'Y');
    return 0;
    }
    /* ================================================================ */
    
    void heading()
    {
    	system("cls");  // Linux/Unix use: system("clear");
    	printf("WELCOME TO THE RAFFLE!\n");
    	return;
    }
    /* ================================================================ */
    double getdouble(char item[], double min)
    {
       int err;
       double ticketquan;
       min = 0;
    do
       {
       printf("\nEnter a ticket %s greater than or equal to %d: ", item, min);
       scanf_s("%d%*c", &ticketquan);
       err = (ticketquan < min);
       if (err) quanerror(min);
       }
    while (err);
    return (ticketquan);
    }
    /* ================================================================ */
    double getfloat(char item[], double min)
    {
       int err;
       double ticketcost;
       min = 5;
    do
       {
       printf("\nEnter a ticket %s greater than or equal to %.2lf: ", item, min);
       scanf_s("%lf%*c", &ticketcost);
       err = (ticketcost < min);
       if (err) quanerror(min);
       }
    while (err);
    return (ticketcost);
    }
    /* ================================================================ */
    void quanerror(double min)
    {
    
    printf("\nOut of range.  Enter a quantity of %d or higher. ", min);
    return;
    }
    /* ================================================================ */
    void costerror(double min)
    {
    
    printf("\nOut of range.  Enter a price of %.2lf or higher. ", min);
    return;
    }
    /* ================================================================ */
    double total(double ticketquan, double ticketcost)
    {
    return (ticketquan * ticketcost);
    }
    /* ================================================================ */
    void show(double ticketquan, double ticketcost, double tickettotal, double grandtotal)
    {
    	printf("\nAmount of tickets: %d", ticketquan);
    	printf("\nPrice per ticket : %.2lf", ticketcost);
    	printf("\nTotal amount due : %.2lf", tickettotal);
    	printf("\nGrand total of all purchases: %.2lf", grandtotal);
    	printf("\n\n");
    	printf("Press Enter to continue ");
    getchar();
    }
    /* ================================================================ */
    int contyn(char msg[])
    {
    	int c;
    printf("%s (Y/N):", msg);
    c = getchar();
    c = toupper(c);
    return c;
    }
    /* ================================================================ */

  2. #2
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    Code:
    scanf_s("%d%*c", &ticketquan);
    You need to use %lf for doubles instead of %d.
    Code:
    scanf_s("%lf%*c", &ticketquan);
    Don't quote me on that... ...seriously

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Printf should also match format to var type:
    printf("\nAmount of tickets: &#37;d", ticketquan);

    Code:
    printf("\nAmount of tickets: %f", ticketquan);
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You need to use &#37;lf for doubles instead of %d.
    And, conversely and confusingly:
    Code:
    printf("\nOut of range.  Enter a price of %.2lf or higher. ", min);
    printf() uses %f for floats and doubles, even though scanf() uses %f for floats and %lf (ell eff) for doubles. It's a long story.

    Code:
    while (choice == 'y' || choice == 'Y');
    You have ctype.h -- why not use it?
    Code:
    while(tolower(choice) == 'y');
    Code:
    int contyn(char msg[])
    {
    	int c;
    printf("%s (Y/N):", msg);
    c = getchar();
    c = toupper(c);
    return c;
    }
    That works, but it's unnecessarily verbose.
    Code:
    int contyn(char msg[])
    {
        printf("%s (Y/N):", msg);
        return toupper(getchar());
    }
    But that's just me.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Whilst changing the relevant markers for "ticketQuan" to %lf/%f respectively is the right thing in the sense that the code will now work right, but I doubt that using "double" to signify the number of tickets is really necessary. The number of tickets should be a whole number (and never negative), so unsigned integer should be the choice here. For most purposes, even an older 16-bit machines unsigned integers will be sufficient for the number of tickets in nearly all places - that allows 65535 tickes to be purchased.

    So, I would suggest an alternative fix: Use unsigned integer, using %u for both input and output.

    Always use the simplest, smallest type that can be used to represent the data you are using. [Although using short int instead of int doesn't always make sense].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    If 65535 isn't big enough, the Standard guarantees that unsigned long goes up to at least 2^32 - 1 == 4294967295.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 10-23-2006, 07:45 AM
  2. Adding functions to a program
    By Finchie_88 in forum Windows Programming
    Replies: 9
    Last Post: 03-04-2005, 11:10 AM
  3. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  4. Help starting a C program with functions
    By jlmac2001 in forum C Programming
    Replies: 6
    Last Post: 10-12-2002, 02:43 PM
  5. arrays and functions (beginner q)
    By eazhar in forum C++ Programming
    Replies: 4
    Last Post: 07-13-2002, 05:39 AM