# Thread: Need some help with a beginner functions program!

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 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
{
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;
}
/* ================================================================ */

{
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. Code:
`scanf_s("%d%*c", &ticketquan);`
You need to use %lf for doubles instead of %d.
Code:
`scanf_s("%lf%*c", &ticketquan);`

3. Printf should also match format to var type:
printf("\nAmount of tickets: &#37;d", ticketquan);

Code:
`printf("\nAmount of tickets: %f", ticketquan);`

4. 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);
}```
But that's just me.

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

6. If 65535 isn't big enough, the Standard guarantees that unsigned long goes up to at least 2^32 - 1 == 4294967295.