I am seriously so bummed out right now, and I feel like a complete idiot. I have been sitting here for seriously 7 hours now trying to figure out how to do this program, and the book I have for the class has been of no help whatsoever. I posted my problem in C++ and had someone tell me that this is a C Program, so after a minute I looked at my course website and the book and realized that the reason the book makes no sense at all is that it is C++ Primer Plus, instead of C Primer Plus. I am so upset because I have been working on this assignment for so long, and it's due at midnight tonight, and I feel like I just set myself back really far. If anyone would be willing to help me I would appreciate it so much. I've understood every other thing I've learned in C so far (printf, scanf, validation, calculations, etc.) but I can't figure out how to write these functions, and now I don't even have the right book to look in. I know the assignment isn't too difficult because all of the assignments before this have been fairly easy for me. I just really need some help!

The program is to 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 hopefully look like this at the end--

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. */

Total cost of all tickets ---->

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

Can someone please help me with this? I know it's not that difficult but I'm totally lost and so exhausted! Here is the code I've written so far:

``````/* ================================================================== */
/* >  Program:      Exam1.c                                         < */
/* >                                                                < */
/* >  Description:  printf, scanf, loop                             < */
/* >                                                                < */
/* >     Name           Date      Comment                           < */
/* >     -------------  --------  ------------------------------    < */
/* >     Janelle Wood    2/27/08   Author                           < */
/* ================================================================== */

#include <stdio.h>
#include <stdlib.h>

void error(double min, double max);
double getfloat(char prompt[], double min);
double grandtotal(double tickettotal);
double total(double ticketquan, double ticketcost);
void show(double ticketquan, double ticketcost, double tickettotal, double grandtotal);
int contyn(char msg[]);

int main()
{
double ticketcost, ticketquan, tickettotal, grandtotal, costmin;
int quanmin;
char choice;

do
{
ticketquan = getfloat("quantity", quanmin);
ticketcost = getfloat("price", costmin);
tickettotal= total(ticketquan, ticketcost);
grandtotal = grandtotal(tickettotal);
show(ticketquan, ticketcost, tickettotal, grandtotal);
choice  = contyn("\nWould you like to purchase more raffle tickets?");
}
while (choice == 'y' || choice == 'Y');
return 0;
}
/* ================================================================ */

{
system("cls");  // Linux/Unix use: system("clear");
printf("WELCOME TO THE RAFFLE!");
return;
}
/* ================================================================ */
double getfloat(char item[], double min)
{
int err;
double ticketcost;
do
{
printf("\nEnter a ticket %s greater than or equal to %.0lf: ", item, min);
scanf("%lf%*c", &ticketcost);
err = (ticketcost < min);
if (err) error(min, max);
}
while (err);
return (ticketcost);
}
/* ================================================================ */
void error(double min, double max)
{
printf("\nOut of range, try %.0lf to %.0lf, press any key ", min, max);
return;
}
/* ================================================================ */
double total(double ticketnum, double ticketcost)
{
return (ticketnum * ticketcost);
}
/* ================================================================ */``````

grandtotal us used both as a function and a variable name. You can't do that. See lines 17 and 34 (as well as others)

line 59: max is undefined. But I guess you would have know that had you read your compiler's error messages.

See my earlier post in the C++ section:
http://www.daniweb.com/forums/post549484-2.html
It applies to the C language as well and it looks like you still have a lot of the same code that will give you the errors. Try to rename some variables so there is less confusion. Also, your "heading" function declaration doesn't match your "heading" function implementation:

``void heading(void);``

versus:

``void heading()``

That may actually compile, but I'd take the "void" out of the parentheses above because I don't think you want it or need it.

First problem you have grandtotal define as a function and variable

More to follow

``````double getfloat(char item[], double min)
{
int err;
double ticketcost;
do
{
printf("\nEnter a ticket %s greater than or equal to %.0lf: ", item, min);
scanf("%lf%*c", &ticketcost);
err = (ticketcost < min);
if (err) error(min, max);
}
while (err);
return (ticketcost);
}``````

max is not define in this function. You can:
1 define as a global const if doesn't change
2 define it in the function

Note value like min max are usually global const
set at the beginning of the programi.e.

const int max = some value;
const int min = some value;

Ok sorry guys, here is what I have now after working on it for a while. Hopefully I'm getting closer to the program!

``````/* ================================================================== */
/* >  Program:      Exam1.c                                         < */
/* >                                                                < */
/* >  Description:  printf, scanf, loop                             < */
/* >                                                                < */
/* >     Name           Date      Comment                           < */
/* >     -------------  --------  ------------------------------    < */
/* >     Janelle Wood    2/27/08   Author                           < */
/* ================================================================== */

#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 grandtotal(double tickettotal);
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, finaltotal, costmin;
double multtotal;

char choice;

quanmin = 0;
costmin = 5;

do
{
ticketquan = getdouble("quantity", quanmin);
ticketcost = getfloat("price", costmin);
tickettotal= total(ticketquan, ticketcost);
multtotal += total;
finaltotal = grandtotal(tickettotal);
choice  = contyn("\nWould you like to purchase more raffle tickets?");
show(ticketquan, ticketcost, tickettotal, finaltotal);

}
while (choice == 'y' || choice == 'Y');
return 0;
}
/* ================================================================ */

{
system("cls");  // Linux/Unix use: system("clear");
printf("WELCOME TO THE RAFFLE!");
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)
{
min = 0;

printf("\nOut of range, enter a quantity of %d or higher, press any key ", min);
return;
}
/* ================================================================ */
void costerror(double min)
{
min = 5;

printf("\nOut of range, enter a price of %.2lf or higher, press any key ", min);
return;
}
/* ================================================================ */
double total(double ticketquan, double ticketcost)
{
return (ticketquan * ticketcost);
}
/* ================================================================ */
double grandtotal(double multtotal)
{
return (multtotal);
}
/* ================================================================ */
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;
}
/* ================================================================ */``````

Line 41:

``multtotal += total;``

multtotal has not been initialized and variable total does not exist.

How would you recommend getting the grand total? I figured that probably wasn't right, but I don't know how to add together multiple totals in the function....

How would you recommend getting the grand total? I figured that probably wasn't right, but I don't know how to add together multiple totals in the function....

I would get rid of the grandtotal FUNCTION and go back to a grandtotal VARIABLE. I would initialize this variable called grandtotal to 0.0 in the very beginning (before any loop). I think you have the right idea. Just replace line 41 with this line:

``grandtotal += tickettotal;``

which I imagine was your original intent here. tickettotal is the cost of the current ticket purchase. This lines adds it to the running grand total.

Ok, I think I fixed that... 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's the new 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;
}
/* ================================================================ */``````

Fix the flawed scanf_s() call to be like below:

``````double getdouble(char item[], double min)
{
...
printf("\nEnter a ticket %s greater than or equal to %d: ", item, min);
scanf_s("%[B]lf[/B]%*c", &ticketquan);
...``````

Some calls to printf() are erroneous in the same way, i.e. you use %d to output a value of type double.

I already fixed it... Still doesn't work :(

I tested it with a couple of modifications, got the following output:

Enter a ticket quantity greater than or equal to 0: 5
Enter a ticket price greater than or equal to 5.00: 8

Amount of tickets: 5.000000
Price per ticket : 8.00
Total amount due : 40.00
Grand total of all purchases: 40.00

...

#1 in the main() function, initialize "grandtotal=0"
#2 Switch order of the two lines to be like below
show(ticketquan, ticketcost, tickettotal, grandtotal);
choice = contyn("\nWould you like to purchase more raffle tickets?");

Ok, I changed all of those but I still can't even run the program past the "enter a quantity" prompt. It still gives me the error message, no matter what number I enter, and then it goes back to the beginning. GRRR!!!

Here is the version I used (and please go through the %d's and change to %lf where-ever you output a double using printf())

``````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;
char choice;
quanmin = 0;
costmin = 5;
[B]grandtotal = 0;[/B]

do
{
ticketquan = getdouble("quantity", quanmin);
ticketcost = getfloat("price", costmin);
tickettotal= total(ticketquan, ticketcost);
grandtotal+= tickettotal;
[B]	show(ticketquan, ticketcost, tickettotal, grandtotal);
choice  = contyn("\nWould you like to purchase more raffle tickets?");
[/B]	}
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 [B]%lf[/B]: ", item, min);
//scanf("%d%*c", &ticketquan);
scanf("%lf%*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("%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 [B]%lf[/B] or higher. ", min);
}
/* ================================================================ */
void costerror(double min)
{
printf("\nOut of range.  Enter a price of %.2lf or higher. ", min);
}
/* ================================================================ */
double total(double ticketquan, double ticketcost)
{
return (ticketquan * ticketcost);
}
/* ================================================================ */
void show(double ticketquan, double ticketcost, double tickettotal, double grandtotal)
{
printf("\nAmount of tickets: [B]%lf[/B]", 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;
}
/* ================================================================ */``````

did the grandtotal, but if I change the %ds to %lf then it adds decimal places to the quantity. What if I change the whole ticketquan part to an integer instead? Would there be a way for me to convert the integer to a double so I can multiply it with the price?

did the grandtotal, but if I change the %ds to %lf then it adds decimal places to the quantity. What if I change the whole ticketquan part to an integer instead? Would there be a way for me to convert the integer to a double so I can multiply it with the price?

ticketquan surely can be an integer (and is more natural in that way), so the answer is yes.

Here is the version I used (and please go through the %d's and change to %lf where-ever you output a double using printf())

Trivia: using `%lf` with `printf` is undefined behavior in C90, a common nonstandard extension, and standardized in the uncommonly-implemented C99 as ignoring the `l` .

Alllllright, I did it! Here is the code for the final working program, including the part I forgot, that if the total is more than \$500, the output has to stay at \$500. I feel like a champ for finally getting this done! Thank you soooooo much for all of your guys' help! I would not have been able to do it without you!

``````/* ================================================================== */
/* >  Program:      Exam1.c                                         < */
/* >                                                                < */
/* >  Description:  printf, scanf, loop                             < */
/* >                                                                < */
/* >     Name           Date      Comment                           < */
/* >     -------------  --------  ------------------------------    < */
/* >     Janelle Wood    2/27/08   Author                           < */
/* ================================================================== */

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>

void quanerror(double min);
void costerror(double min);
double getfloat(char prompt[], double min);
double getint(char prompt[], double min);
double total(double ticketquan, double ticketcost);
void show1(int ticketquan, double ticketcost, double tickettotal);
void show2(double grandtotal);
int contyn(char msg[]);

int main()
{
double ticketcost, tickettotal, costmin, grandtotal;

int quanmin, ticketquan;

char choice;

quanmin = 0;
costmin = 5;
grandtotal = 0;

do
{
ticketquan = getint("quantity", quanmin);
ticketcost = getfloat("price", costmin);
tickettotal= total(ticketquan, ticketcost);
grandtotal+= tickettotal;
show1(ticketquan, ticketcost, tickettotal);
choice  = contyn("\nWould you like to purchase more raffle tickets?");
}
while (choice == 'y' || choice == 'Y');

show2(grandtotal);

return 0;
}
/* ================================================================ */

{
system("cls");  // Linux/Unix use: system("clear");
printf("WELCOME TO THE RAFFLE!\n\n\n");
return;
}
/* ================================================================ */
double getint(char item[], double min)
{
int err;
int 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)
{
int err;
int maxtotal = 500;

err = (ticketquan * ticketcost > maxtotal);
if (err) return (maxtotal);
else
return (ticketquan * ticketcost);
}

/* ================================================================ */
void show1(int ticketquan, double ticketcost, double tickettotal)
{
printf("\nAmount of tickets: %d", ticketquan);
printf("\nPrice per ticket : \$%.2lf", ticketcost);
printf("\nTotal amount due : \$%.2lf\n\n", tickettotal);

}
/* ================================================================ */
void show2(double grandtotal)
{
printf("\nGrand total of all purchases: \$%.2lf\n\n", grandtotal);
}
/* ================================================================ */

int contyn(char msg[])
{
int c;
printf("%s (Y/N):", msg);
c = getchar();
c = toupper(c);
return c;
}
/* ================================================================ */``````

Trivia: using %lf with printf is undefined behavior in C90, a common nonstandard extension, and standardized in the uncommonly-implemented C99 as ignoring the l.

A good piece of trivia that one, thanks. Just wonder how come it has been so popular, apparently in practice, that is has been "standardized in the uncommonly-implemented C99 as ignoring the l"... hmmm.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.