| | |
Concerning C++ calculations using printf
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Aug 2007
Posts: 58
Reputation:
Solved Threads: 0
Thanks for the help. I was able to get the calculations in the program right. The old coding I had for it doesnt really work like it did before. Basically I'm trying to have it where the numbers I put in show an error if the number entered is greater than 999 and also show an error if it is a non number aswell. Also trying to see how I can make the information in yearly show only when I type in yearly or "Y" and the information for daily only show when I type in daily or "D". This is the current code I have atm.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
void PrintAStarLine(void);
void GetInput(void);
void Tickets(void);
float price( const float item ){
int piece = 0;
char newline2;
scanf( "%d%c", &piece, &newline2 );
return item * piece;
}
char newline;
char customerName[30];
char tickettype[];
char TicketNumber[] = "D, d";
int main( void )
{
PrintAStarLine();
printf(" Home Aquarium Data Entry \n");
printf("\n\n");
PrintAStarLine();
GetInput();
Tickets();
const float DChild = 1.50;
float DailyChild = 0.00;
const float YChild = 5.50;
float YearlyChild = 0.00;
float TotalChild;
const float DAdult = 2.00;
float DailyAdult = 0.00;
const float YAdult = 6.50;
float YearlyAdult = 0.00;
float TotalAdult;
const float DSenior = 1.25;
float DailySenior = 0.00;
const float YSenior = 3.75;
float YearlySenior = 0.00;
float TotalSenior;
printf("#of Children for daily tickets: " );
fflush( stdout );
DailyChild = price( DChild );
printf("\n\n");
printf("#of Children for yearly tickets: " );
fflush( stdout );
YearlyChild = price( YChild );
printf("\n\n");
printf("#of Adults for daily tickets: " );
fflush( stdout );
DailyAdult = price( DAdult );
printf("\n\n");
printf("#of Adults for yearly tickets: " );
fflush( stdout );
YearlyAdult = price( YAdult );
printf("\n\n");
printf("#of Seniors for daily tickets: " );
fflush( stdout );
DailySenior = price( DSenior );
printf("\n\n");
printf("#of Seniors for yearly tickets: " );
fflush( stdout );
YearlySenior = price( YSenior );
system("pause");
printf("\n\n\n");
TotalChild = (DailyChild + YearlyChild);
TotalAdult = (DailyAdult + YearlyAdult);
TotalSenior = (DailySenior + YearlySenior);
printf( "Children Total Cost: $%6.2f\n", TotalChild );
printf("\n");
printf( "Adult Total Cost: $%6.2f\n", TotalAdult );
printf("\n");
printf( "Senior Total Cost: $%6.2f\n", TotalSenior );
printf("\n");
return 0;
}
void PrintAStarLine(void)
{
printf("**************************************************************************** \n");
printf("\n\n");
return;
}
void GetInput(void)
{
printf("Customer Name (First Middle Last): ");
scanf( "%[^\n]%c", customerName, &newline );
printf("\n\n\n");
return;
}
void Tickets(void)
{
printf("\n\nType of ticket (Daily(D) or Yearly(Y): ");
scanf(" %s", &TicketNumber);
if(strcmp(TicketNumber,"D")==0)
{
printf("#of Children for daily tickets: " ); ;
}
else if(strcmp(TicketNumber,"d")==0)
{
;
}
else if(strcmp(TicketNumber,"Daily")==0)
{
;
}
else if(strcmp(TicketNumber,"daily")==0)
{
;
}
else if(strcmp(TicketNumber,"Y")==0)
{
;
}
else if(strcmp(TicketNumber,"y")==0)
{
;
}
else if(strcmp(TicketNumber,"Yearly")==0)
{
;
}
else if(strcmp(TicketNumber,"yearly")==0)
{
;
}
else
{
printf("\n Incorrect Ticket Number Entered, please try again! \n");
printf("\n");
Tickets();
}
printf("\n");
return;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
void PrintAStarLine(void);
void GetInput(void);
void Tickets(void);
float price( const float item ){
int piece = 0;
char newline2;
scanf( "%d%c", &piece, &newline2 );
return item * piece;
}
char newline;
char customerName[30];
char tickettype[];
char TicketNumber[] = "D, d";
int main( void )
{
PrintAStarLine();
printf(" Home Aquarium Data Entry \n");
printf("\n\n");
PrintAStarLine();
GetInput();
Tickets();
const float DChild = 1.50;
float DailyChild = 0.00;
const float YChild = 5.50;
float YearlyChild = 0.00;
float TotalChild;
const float DAdult = 2.00;
float DailyAdult = 0.00;
const float YAdult = 6.50;
float YearlyAdult = 0.00;
float TotalAdult;
const float DSenior = 1.25;
float DailySenior = 0.00;
const float YSenior = 3.75;
float YearlySenior = 0.00;
float TotalSenior;
printf("#of Children for daily tickets: " );
fflush( stdout );
DailyChild = price( DChild );
printf("\n\n");
printf("#of Children for yearly tickets: " );
fflush( stdout );
YearlyChild = price( YChild );
printf("\n\n");
printf("#of Adults for daily tickets: " );
fflush( stdout );
DailyAdult = price( DAdult );
printf("\n\n");
printf("#of Adults for yearly tickets: " );
fflush( stdout );
YearlyAdult = price( YAdult );
printf("\n\n");
printf("#of Seniors for daily tickets: " );
fflush( stdout );
DailySenior = price( DSenior );
printf("\n\n");
printf("#of Seniors for yearly tickets: " );
fflush( stdout );
YearlySenior = price( YSenior );
system("pause");
printf("\n\n\n");
TotalChild = (DailyChild + YearlyChild);
TotalAdult = (DailyAdult + YearlyAdult);
TotalSenior = (DailySenior + YearlySenior);
printf( "Children Total Cost: $%6.2f\n", TotalChild );
printf("\n");
printf( "Adult Total Cost: $%6.2f\n", TotalAdult );
printf("\n");
printf( "Senior Total Cost: $%6.2f\n", TotalSenior );
printf("\n");
return 0;
}
void PrintAStarLine(void)
{
printf("**************************************************************************** \n");
printf("\n\n");
return;
}
void GetInput(void)
{
printf("Customer Name (First Middle Last): ");
scanf( "%[^\n]%c", customerName, &newline );
printf("\n\n\n");
return;
}
void Tickets(void)
{
printf("\n\nType of ticket (Daily(D) or Yearly(Y): ");
scanf(" %s", &TicketNumber);
if(strcmp(TicketNumber,"D")==0)
{
printf("#of Children for daily tickets: " ); ;
}
else if(strcmp(TicketNumber,"d")==0)
{
;
}
else if(strcmp(TicketNumber,"Daily")==0)
{
;
}
else if(strcmp(TicketNumber,"daily")==0)
{
;
}
else if(strcmp(TicketNumber,"Y")==0)
{
;
}
else if(strcmp(TicketNumber,"y")==0)
{
;
}
else if(strcmp(TicketNumber,"Yearly")==0)
{
;
}
else if(strcmp(TicketNumber,"yearly")==0)
{
;
}
else
{
printf("\n Incorrect Ticket Number Entered, please try again! \n");
printf("\n");
Tickets();
}
printf("\n");
return;
}
Last edited by NycNessyness; Aug 6th, 2007 at 4:51 am.
Remember these?
Last edited by Ancient Dragon : 1 Day Ago at 22:05. Reason: add code tags
Last edited by Ancient Dragon : 1 Day Ago at 05:02. Reason: add code tags -- please start using them
Or what about this
http://www.daniweb.com/forums/announcement118-3.html
Not to mention the watermark at the back of the edit window.
If all these escape your attention, then give up programming.
Last edited by Ancient Dragon : 1 Day Ago at 22:05. Reason: add code tags
Last edited by Ancient Dragon : 1 Day Ago at 05:02. Reason: add code tags -- please start using them
Or what about this
http://www.daniweb.com/forums/announcement118-3.html
Not to mention the watermark at the back of the edit window.
If all these escape your attention, then give up programming.
•
•
Join Date: Aug 2007
Posts: 58
Reputation:
Solved Threads: 0
Oh sorry about that guys. I will do it from now on. As far as my code goes I'm almost done with it. The last problem I have is my calculation. Its currenty messed up because I'm trying different methods by trial and error. What I'm trying to do is make it so that when a ticket is purchased for a child that is accomapanied by 2 or more adults or if accompanied by 3 or more seniors they get 10 percent off of the total purchase. If I can an example like Aia did for me earlier I think I'll be able to finish this off. Thanks again guys for the help. You've helped me more than you can imagine, because I'm learning this online so it's kind of difficult.
C Syntax (Toggle Plain Text)
#include <string.h> #include <iostream> void PrintAStarLine(void); void GetInput(void); void Tickets(void); void AskQuestion(void); float price( const float item ){ int piece = 0; char newline2; scanf( "%d%c", &piece, &newline2 ); return item * piece; } char newline; char customerName[30]; char tickettype[]; char TicketNumber[] = "D, d"; char NumberName[10]; char Underlines[10]; char aQuestion; int main( void ) { PrintAStarLine(); printf(" Home Aquarium Data Entry \n"); printf("\n\n"); PrintAStarLine(); GetInput(); Tickets(); const float DChild = 1.50; float DailyChild = 0.00; const float YChild = 5.50; float YearlyChild = 0.00; float TotalChild; const float DAdult = 2.00; float DailyAdult = 0.00; const float YAdult = 6.50; float YearlyAdult = 0.00; float TotalAdult; const float DSenior = 1.25; float DailySenior = 0.00; const float YSenior = 3.75; float YearlySenior = 0.00; float TotalSenior; float TotalCost; float LessDiscount; float TotalDiscount; printf("#of Children for daily tickets: " ); fflush( stdout ); DailyChild = price( DChild ); printf("\n\n"); printf("#of Children for yearly tickets: " ); fflush( stdout ); YearlyChild = price( YChild ); printf("\n\n"); printf("#of Adults for daily tickets: " ); fflush( stdout ); DailyAdult = price( DAdult ); printf("\n\n"); printf("#of Adults for yearly tickets: " ); fflush( stdout ); YearlyAdult = price( YAdult ); printf("\n\n"); printf("#of Seniors for daily tickets: " ); fflush( stdout ); DailySenior = price( DSenior ); printf("\n\n"); printf("#of Seniors for yearly tickets: " ); fflush( stdout ); YearlySenior = price( YSenior ); system("pause"); printf("\n\n\n"); system("cls"); PrintAStarLine(); printf(" Home Aquarium Contract \n"); printf("\n\n"); PrintAStarLine(); printf("CustomerName: %s \n", customerName); if(strcmp(TicketNumber,"D")==0) { printf("Type of ticket: Daily \n", TicketNumber); printf("\n\n"); } else if(strcmp(TicketNumber,"d")==0) { printf("Type of ticket: Daily \n", TicketNumber); printf("\n\n"); } else if(strcmp(TicketNumber,"Daily")==0) { printf("Type of ticket: Daily \n", TicketNumber); printf("\n\n"); } else if(strcmp(TicketNumber,"daily")==0) { printf("Type of ticket: Daily \n", TicketNumber); printf("\n\n"); } else if (TicketNumber == "Y") { printf("Type of ticket: Yearly \n", TicketNumber); printf("\n\n"); } else if(strcmp(TicketNumber,"y")==0) { printf("Type of ticket: Yearly \n", TicketNumber); printf("\n\n"); } else if(strcmp(TicketNumber,"Yearly")==0) { printf("Type of ticket: Yearly \n", TicketNumber); printf("\n\n"); } else if(strcmp(TicketNumber,"yearly")==0) { printf("Type of ticket: Yearly \n", TicketNumber); printf("\n\n"); } if (DailyAdult >= 2) TotalChild = TotalCost * .10; else if (YearlyAdult >= 2) TotalChild = TotalCost * .10; else if (DailySenior >= 3) TotalChild = TotalCost * .10; else if (YearlySenior >=3) TotalChild = TotalCost * .10; else TotalChild = (DailyChild + YearlyChild); TotalAdult = (DailyAdult + YearlyAdult); TotalSenior = (DailySenior + YearlySenior); TotalCost = (TotalAdult) + (TotalSenior) + (TotalChild); LessDiscount = (TotalCost); TotalDiscount = (TotalCost) - (LessDiscount); system("pause"); printf(" Charge Number %s \n", NumberName); printf("\n\n"); printf( "Child: $%6.2f\n", TotalChild ); printf("\n"); printf( "Adult: $%6.2f\n", TotalAdult ); printf("\n"); printf( "Senior: $%6.2f\n", TotalSenior ); printf("\n"); printf(" ======= ======= %s \n", Underlines); printf("\n\n"); printf( "Total: $%6.2f\n", TotalCost ); printf("\n"); printf( "Less Discount: $%6.2f\n", LessDiscount ); printf("\n"); printf( "Total After Discount: $%6.2f\n", TotalDiscount ); printf("\n"); aQuestion = 'N'; while ((aQuestion != 'Y') && (aQuestion != 'y')) AskQuestion(); system("cls"); return 0; } void PrintAStarLine(void) { printf("******************************************************************************** \n"); printf("\n\n"); return; } void GetInput(void) { printf("Customer Name (First Middle Last): "); scanf( "%[^\n]%c", customerName, &newline ); printf("\n\n\n"); return; } void Tickets(void) { printf("\n\nType of ticket (Daily(D) or Yearly(Y): "); scanf(" %s", &TicketNumber); if(strcmp(TicketNumber,"D")==0) { } else if(strcmp(TicketNumber,"d")==0) { ; } else if(strcmp(TicketNumber,"Daily")==0) { ; } else if(strcmp(TicketNumber,"daily")==0) { ; } else if(strcmp(TicketNumber,"Y")==0) { ; } else if(strcmp(TicketNumber,"y")==0) { ; } else if(strcmp(TicketNumber,"Yearly")==0) { ; } else if(strcmp(TicketNumber,"yearly")==0) { ; } else { printf("\n Incorrect Ticket Number Entered, please try again! \n"); printf("\n"); Tickets(); } printf("\n"); return; } void AskQuestion(void) { printf("\n\nAre we done for the day? (Y/N)"); scanf(" %c", &aQuestion); fflush(stdin); return ; }
•
•
•
•
What I'm trying to do is make it so that when a ticket is purchased for a child that is accomapanied by 2 or more adults or if accompanied by 3 or more seniors they get 10 percent off of the total purchase.
Your code is pretty hard to read, right now.
It is custumary to declare the functions after the closing bracket of main and declare the prototypes of those functions before main.
Right now you have a function at the top and the rest at the bottom.
#include <iostream>
is for C++. C should be <stdio.h>
A few lines of comments indicating what expressions do, would help you also. Specially functions.
Bonus example:
C Syntax (Toggle Plain Text)
/* * line.c * Shows how to use an independent function to print a line of * stars and newlines. */ #include <stdio.h> void PrintAStarLine( int star, int newline ); /* prototype */ int main( void ) { PrintAStarLine( 80, 4 ); /* give it any arguments you want */ getchar(); return 0; } /* * PrintAStarLine: Prints a line of star characters * parameters: a int for the amount of char stars, a int for amount of newlines * return void. */ void PrintAStarLine( int star, int newline ) { while( star-- ) { putchar( '*' ); } while( newline-- ) { putchar( '\n' ); } }
Last edited by Aia; Aug 6th, 2007 at 10:47 pm.
•
•
Join Date: Aug 2007
Posts: 58
Reputation:
Solved Threads: 0
I was able to do the calculations and did the labeling of my code. I'm pretty much done with the program but still having a bit of difficulty with displaying data. The only error I have atm now is trying to get the program to display the number i entered for each ticket alongside the ticket cost. May someone be kind enough to give me a clue as to how I can do this.
C Syntax (Toggle Plain Text)
/*+*+*+*+*+*+*+* Preprocessor Directives *+*+*+*+*+*+*+*/ #include <stdio.h> #include <stdlib.h> #include <string.h> /*+*+*+*+*+*+*+* Function Prototypes *+*+*+*+*+*+*+*/ void PrintAStarLine(void); void GetInput(void); void Tickets(void); void DailyTickets(void); void YearlyTickets(void); void NewPageDisplay(void); void SecondPageDisplay(void); void Calculations(void); void AskQuestion(void); /*+*+*+*+*+*+*+* Variables *+*+*+*+*+*+*+*/ float price( const float item ){ int piece = 0; char newline2; scanf( "%d%c", &piece, &newline2 ); return item * piece; } char newline; char customerName[30]; char tickettype[]; char TicketNumber[] = "D, d"; char NumberName[10]; char Underlines[10]; char aQuestion; char TotalIncome[10]; const float DChild = 1.50; float DailyChild = 0.00; const float YChild = 5.50; float YearlyChild = 0.00; float TotalChild; const float DAdult = 2.00; float DailyAdult = 0.00; const float YAdult = 6.50; float YearlyAdult = 0.00; float TotalAdult; const float DSenior = 1.25; float DailySenior = 0.00; const float YSenior = 3.75; float YearlySenior = 0.00; float TotalSenior; float TotalCost; float LessDiscount; float TotalDiscount; /*+*+*+*+*+*+*+*^ The Main Module ^*+*+*+*+*+*+*+*/ int main( void ) { PrintAStarLine(); printf(" Home Aquarium Data Entry \n"); printf("\n\n"); PrintAStarLine(); GetInput(); Tickets(); Calculations(); NewPageDisplay(); aQuestion = 'N'; /* It will loop until user enter Y */ while ((aQuestion != 'Y') && (aQuestion != 'y')) /*sets value to go into loop the first time */ AskQuestion(); system("cls"); SecondPageDisplay(); return 0; } /*+*+*+*+*+*+*+* Avoids making the program look real plain *+*+*+*+*+*+*+*/ void PrintAStarLine(void) { printf("******************************************************************************** \n"); printf("\n\n"); return; } /*+*+*+*+*+*+*+* Asks The User for their name *+*+*+*+*+*+*+*/ void GetInput(void) { printf("Customer Name (First Middle Last): "); scanf( "%[^\n]%c", customerName, &newline ); printf("\n\n\n"); return; } /*+*+*+*+*+*+*+* Asks The User what ticket type they want *+*+*+*+*+*+*+*/ void Tickets(void) { printf("\n\nType of ticket (Daily(D) or Yearly(Y): "); scanf(" %s", &TicketNumber); if(strcmp(TicketNumber,"D")==0) { DailyTickets(); } else if(strcmp(TicketNumber,"d")==0) { DailyTickets(); } else if(strcmp(TicketNumber,"Daily")==0) { DailyTickets(); } else if(strcmp(TicketNumber,"daily")==0) { DailyTickets(); } else if(strcmp(TicketNumber,"Y")==0) { YearlyTickets(); } else if(strcmp(TicketNumber,"y")==0) { YearlyTickets(); } else if(strcmp(TicketNumber,"Yearly")==0) { YearlyTickets(); } else if(strcmp(TicketNumber,"yearly")==0) { YearlyTickets(); } else { printf("\n Incorrect Ticket Number Entered, please try again! \n"); printf("\n"); Tickets(); } printf("\n"); return; } /*+*+*+*+*+*+*+* Allows the user to enter daily tickets *+*+*+*+*+*+*+*/ void DailyTickets(void) { printf("\n\n"); printf("#of Children for daily tickets: " ); fflush( stdout ); DailyChild = price( DChild ); printf("\n\n"); printf("#of Adults for daily tickets: " ); fflush( stdout ); DailyAdult = price( DAdult ); printf("\n\n"); printf("#of Seniors for daily tickets: " ); fflush( stdout ); DailySenior = price( DSenior ); printf("\n\n"); return; } /*+*+*+*+*+*+*+* Allows the user to enter yearly tickets *+*+*+*+*+*+*+*/ void YearlyTickets(void) { printf("\n\n"); printf("#of Children for yearly tickets: " ); fflush( stdout ); YearlyChild = price( YChild ); printf("\n\n"); printf("#of Adults for yearly tickets: " ); fflush( stdout ); YearlyAdult = price( YAdult ); printf("\n\n"); printf("#of Seniors for yearly tickets: " ); fflush( stdout ); YearlySenior = price( YSenior ); system("pause"); printf("\n\n\n"); return; } /*+*+*+*+*+*+*+* Displays information that the user entered *+*+*+*+*+*+*+*/ void NewPageDisplay(void) { system("cls"); PrintAStarLine(); printf(" Home Aquarium Contract \n"); printf("\n\n"); PrintAStarLine(); printf("CustomerName: %s \n", customerName); if(strcmp(TicketNumber,"D")==0) { printf("Type of ticket: Daily \n", TicketNumber); printf("\n\n"); } else if(strcmp(TicketNumber,"d")==0) { printf("Type of ticket: Daily \n", TicketNumber); printf("\n\n"); } else if(strcmp(TicketNumber,"Daily")==0) { printf("Type of ticket: Daily \n", TicketNumber); printf("\n\n"); } else if(strcmp(TicketNumber,"daily")==0) { printf("Type of ticket: Daily \n", TicketNumber); printf("\n\n"); } else if (TicketNumber == "Y") { printf("Type of ticket: Yearly \n", TicketNumber); printf("\n\n"); } else if(strcmp(TicketNumber,"y")==0) { printf("Type of ticket: Yearly \n", TicketNumber); printf("\n\n"); } else if(strcmp(TicketNumber,"Yearly")==0) { printf("Type of ticket: Yearly \n", TicketNumber); printf("\n\n"); } else if(strcmp(TicketNumber,"yearly")==0) { printf("Type of ticket: Yearly \n", TicketNumber); printf("\n\n"); } system("pause"); printf(" Charge Number %s \n", NumberName); printf("\n\n"); printf( "Child: $%6.2f\n", TotalChild ); printf("\n"); printf( "Adult: $%6.2f\n", TotalAdult ); printf("\n"); printf( "Senior: $%6.2f\n", TotalSenior ); printf("\n"); printf(" ======= ======= %s \n", Underlines); printf("\n\n"); printf( "Total: $%6.2f\n", TotalCost ); printf("\n"); printf( "Less Discount: $%6.2f\n", LessDiscount ); printf("\n"); printf( "Total After Discount: $%6.2f\n", TotalDiscount ); printf("\n"); return; } /*+*+*+*+*+*+*+* Performs Calculations *+*+*+*+*+*+*+*/ void Calculations(void) { TotalChild = (DailyChild + YearlyChild); TotalAdult = (DailyAdult + YearlyAdult); TotalSenior = (DailySenior + YearlySenior); TotalCost = (TotalAdult) + (TotalSenior) + (TotalChild); if (DailyAdult > 2) LessDiscount = TotalCost * .10; else if (YearlyAdult > 2) LessDiscount = TotalCost * .10; else if (DailySenior > 3) LessDiscount = TotalCost * .10; else if (YearlySenior > 3) LessDiscount = TotalCost * .10; else LessDiscount = TotalCost - TotalCost; TotalDiscount = (TotalCost) - (LessDiscount); return; } /*+*+*+*+*+*+*+* Asks The User A Question *+*+*+*+*+*+*+*/ void AskQuestion(void) { printf("\n\nAre we done for the day? (Y/N)"); scanf(" %c", &aQuestion); fflush(stdin); return; } /*+*+*+*+*+*+*+* Displays Final Cost to User *+*+*+*+*+*+*+*/ void SecondPageDisplay(void) { system("cls"); PrintAStarLine(); printf(" Home Aquarium Daily Totals \n"); printf("\n\n"); PrintAStarLine(); printf("Total Total \nNumber Income %s \n", TotalIncome); printf("\n\n"); printf(" Child: $%6.2f\n", TotalChild ); printf("\n"); printf( " Adult: $%6.2f\n", TotalAdult ); printf("\n"); printf( " Senior: $%6.2f\n", TotalSenior ); printf("\n"); printf("________________________________________________________________________________%s \n", Underlines); printf( "Grand Total: $%6.2f\n", TotalCost ); printf("\n"); printf( "Grand Total After Discount: $%6.2f\n", TotalDiscount ); printf("\n"); return; }
char tickettype[];
This array needs a subscript limit inside the brackets.
To improve your code and skills I suggest you do some reading
about:
Scope. The life and range of variable declarations.
Global variables.
And the key word switch.
At some point you will have to star using pointers in the
functions to avoid all those Global variables.
This array needs a subscript limit inside the brackets.
To improve your code and skills I suggest you do some reading
about:
Scope. The life and range of variable declarations.
Global variables.
And the key word switch.
At some point you will have to star using pointers in the
functions to avoid all those Global variables.
•
•
Join Date: Aug 2007
Posts: 58
Reputation:
Solved Threads: 0
Thanks for the help Aia. I got my project back and wanted to know how would I be able to display the amount of either nails or screws purchased in your program below. For it to be next to the cost of them. For example it should look something like this.
This is the code you did earlier
C Syntax (Toggle Plain Text)
/* Number Price Nails: 2 $0.50 Screws: 2 $1.00 */
C Syntax (Toggle Plain Text)
/* * HomeDepot.c * Shows how to match item with price. */ #include <stdio.h> float price( const float item ) { int piece = 0; char newline; scanf( "%d%c", &piece, &newline ); /* amount of items */ return item * piece; } int main( void ) { const float screw = .50; /* this never should change */ const float nail = .25; /* this never should change */ float price_screws = 0.00; /* final total price items */ float price_nails = 0.00; /* final total price items */ /* ask for nails */ printf( " Enter amount of nails: " ); fflush( stdout ); /* refresh the screen */ /* get & compute the total price of nails */ price_nails = price( nail ); /* ask for screws */ printf( "Enter amount of screws: " ); fflush( stdout ); /* refresh the screen */ /* get & compute the total price of screws */ price_screws = price( screw ); /* final display */ puts( "\n\tYour total order" ); puts( "\t================\n"); printf( " Price for nails: %6.2f\n", price_nails ); printf( "Price for screws: %6.2f\n", price_screws ); printf( " Grand total: %6.2f\n", price_nails + price_screws ); getchar(); return 0; } /* my input / output. Enter amount of nails: 34 Enter amount of screws: 23 Your total order ================ Price for nails: $ 8.50 Price for screws: $ 11.50 Grand total: $ 20.00 */
Last edited by NycNessyness; Aug 9th, 2007 at 6:56 pm.
>how would I be able to display the amount of either nails or screws purchased
Here's where you need some knowledge about variable scope and pointers.
First: piece is a local variable declared in the price function which it will be destroyed as soon as the function exits.
We need to keep those values obtained by the function price alive. How?. We can declare in main some variables to hold the values, and them pass those variables as pointers to the function.
Example:
Here's where you need some knowledge about variable scope and pointers.
First: piece is a local variable declared in the price function which it will be destroyed as soon as the function exits.
We need to keep those values obtained by the function price alive. How?. We can declare in main some variables to hold the values, and them pass those variables as pointers to the function.
Example:
C Syntax (Toggle Plain Text)
/* * HomeDepot.c * Shows how to match item with price. */ #include <stdio.h> /* function price has now two parameters * a float for the price * a pointer type int for the item * returns a float for total price times item */ float price( const float item, int *piece ) { char newline; scanf( "%d%c", piece, &newline ); /* amount of items */ return item * (*piece); /* piece is a pointer to another variable, to access what's in that variable you have to use the * operand to dereference it */ } int main( void ) { const float screw = .50; /* this never should change */ const float nail = .25; /* this never should change */ float price_screws = 0.00; /* final total price items */ float price_nails = 0.00; /* final total price items */ /* new variables */ int A_nails = 0; /* amount of nails wanted */ int A_screws = 0; /* amount of screws requested */ /* ask for nails */ printf( " Enter amount of nails: " ); fflush( stdout ); /* refresh the screen */ /* get & compute the total price of nails */ price_nails = price( nail, &A_nails ); /* the & is "address of" operand */ /* ask for screws */ printf( "Enter amount of screws: " ); fflush( stdout ); /* refresh the screen */ /* get & compute the total price of screws */ price_screws = price( screw, &A_screws );/* Same here, the & is "address of" operand */ /* final display */ puts( "\n\tYour total order" ); puts( "\t================\n"); puts( "\tNumber\tPrice"); printf("Nails:\t%5d\t%.2f\n", A_nails, price_nails ); printf("Screws:\t%5d\t%.2f\n", A_screws, price_screws ); printf("Grand total: %.2f\n", price_nails + price_screws ); getchar(); return 0; } /* My input/output: Enter amount of nails: 2 Enter amount of screws: 3 Your total order ================ Number Price Nails: 2 0.50 Screws: 3 1.50 Grand total: 2.00 */
Last edited by Aia; Aug 9th, 2007 at 8:08 pm.
•
•
Join Date: Aug 2007
Posts: 58
Reputation:
Solved Threads: 0
Thanks Aia. I tried using the example you gave me, but came up with error messages saying "cannot convert parameter 1 from 'int *' to 'const float'". I'm still trying to make my program display the number entered by the price. Not sure what to do about it, but heres how my code is looking.
C Syntax (Toggle Plain Text)
/*+*+*+*+*+*+*+* Preprocessor Directives *+*+*+*+*+*+*+*/ #include <stdio.h> #include <stdlib.h> #include <string.h> /*+*+*+*+*+*+*+* Function Prototypes *+*+*+*+*+*+*+*/ void PrintAStarLine(void); void GetInput(void); void Tickets(void); void DailyTickets(void); void YearlyTickets(void); void DailyPageDisplay(void); void YearlyPageDisplay(void); void TotalPageDisplay(void); void DailyCalculations(void); void YearlyCalculations(void); void TotalCalculation(void); void AskQuestion(void); /*+*+*+*+*+*+*+* Variables *+*+*+*+*+*+*+*/ const float DChild = 1.50; float DailyChild = 0.00; int DailyChildNum = 0; const float YChild = 5.50; float YearlyChild = 0.00; int YearlyChildNum = 0; const float DAdult = 2.00; float DailyAdult = 0.00; int DailyAdultNum = 0; const float YAdult = 6.50; float YearlyAdult = 0.00; int YearlyAdultNum = 0; const float DSenior = 1.25; float DailySenior = 0.00; int DailySeniorNum = 0; const float YSenior = 3.75; float YearlySenior = 0.00; int YearlySeniorNum = 0; float DailyCost; float YearlyCost; float TotalCost; float DailyDiscount; float YearlyDiscount; float TotalDiscount; float TotalChild; float TotalAdult; float TotalSenior; float DailyTotal; float YearlyTotal; float Total; char customerName[30]; char tickettype[8]; char TicketNumber[8]; char NumberName[10]; char Underlines[10]; char aQuestion; char TotalIncome[10]; float price( const float ticket, int *num ) { char newline; scanf( "%d%c", num, &newline ); return ticket * (*num); } /*+*+*+*+*+*+*+*^ The Main Module ^*+*+*+*+*+*+*+*/ int main( void ) { PrintAStarLine(); printf(" Home Aquarium Data Entry \n"); printf("\n\n"); PrintAStarLine(); aQuestion = 'N'; /* It will loop until user enter YES or YES */ while ((aQuestion != 'Y') && (aQuestion != 'y')) /*sets value to go into loop the first time */ { GetInput(); Tickets(); AskQuestion(); } system("cls"); TotalCalculation(); TotalPageDisplay(); return 0; } /*+*+*+*+*+*+*+* Avoids making the program look real plain *+*+*+*+*+*+*+*/ void PrintAStarLine(void) { printf("******************************************************************************** \n"); printf("\n\n"); return; } /*+*+*+*+*+*+*+* Asks The User for their name *+*+*+*+*+*+*+*/ void GetInput(void) { printf("Customer Name: "); scanf( "%[^\n]", customerName); printf("\n\n\n"); return; } /*+*+*+*+*+*+*+* Asks The User what ticket type they want *+*+*+*+*+*+*+*/ void Tickets(void) { printf("\n\nType of ticket: "); scanf(" %s", &TicketNumber); if(strcmp(TicketNumber,"D")==0) { DailyTickets(); DailyCalculations(); DailyPageDisplay(); } else if(strcmp(TicketNumber,"d")==0) { DailyTickets(); DailyCalculations(); DailyPageDisplay(); } else if(strcmp(TicketNumber,"daily")==0) { DailyTickets(); DailyCalculations(); DailyPageDisplay(); } else if(strcmp(TicketNumber,"Daily")==0) { DailyTickets(); DailyCalculations(); DailyPageDisplay(); } else if(strcmp(TicketNumber,"Y")==0) { YearlyTickets(); YearlyCalculations(); YearlyPageDisplay(); } else if(strcmp(TicketNumber,"y")==0) { YearlyTickets(); YearlyCalculations(); YearlyPageDisplay(); } else if(strcmp(TicketNumber,"Yearly")==0) { YearlyTickets(); YearlyCalculations(); YearlyPageDisplay(); } else if(strcmp(TicketNumber,"yearly")==0) { YearlyTickets(); YearlyCalculations(); YearlyPageDisplay(); } else { printf("\n Incorrect Ticket Number Entered, please try again! \n"); printf("\n"); Tickets(); } printf("\n"); return; } /*+*+*+*+*+*+*+* Allows the user to enter daily tickets *+*+*+*+*+*+*+*/ void DailyTickets(void) { printf("\n\n"); printf("#of Children: " ); DailyChild = price( &DailyChildNum, DChild ); printf("\n\n"); printf("#of Adults: " ); DailyAdult = price( &DailyAdultNum, DAdult ); printf("\n\n"); printf("#of Seniors: " ); DailySenior = price( &DailySeniorNum, DSenior ); printf("\n\n"); return; } /*+*+*+*+*+*+*+* Allows the user to enter yearly tickets *+*+*+*+*+*+*+*/ void YearlyTickets(void) { printf("\n\n"); printf("#of Children: " ); YearlyChild = price( &YearlyChildNum, YChild ); printf("\n\n"); printf("#of Adults: " ); YearlyAdult = price( &YearlyAdultNum, YAdult ); printf("\n\n"); printf("#of Seniors: " ); YearlySenior = price( &YearlySeniorNum, YSenior ); system("pause"); printf("\n\n\n"); return; } /*+*+*+*+*+*+*+* Displays information that the user entered *+*+*+*+*+*+*+*/ void DailyPageDisplay(void) { system("cls"); PrintAStarLine(); printf(" Home Aquarium Contract \n"); printf("\n\n"); PrintAStarLine(); printf("Customer: %s \n", customerName); if(strcmp(TicketNumber,"D")==0) { printf("Type of ticket: Daily \n", TicketNumber); printf("\n\n"); } else if(strcmp(TicketNumber,"d")==0) { printf("Type of ticket: Daily \n", TicketNumber); printf("\n\n"); } else if(strcmp(TicketNumber,"Daily")==0) { printf("Type of ticket: Daily \n", TicketNumber); printf("\n\n"); } else if(strcmp(TicketNumber,"daily")==0) { printf("Type of ticket: Daily \n", TicketNumber); printf("\n\n"); } system("pause"); printf(" Number Charge %s \n", NumberName); printf("\n\n"); printf( "Child: %5d $%6.2f\n", DailyChildNum, DailyChild ); printf("\n"); printf( "Adult: %5d $%6.2f\n", DailyAdultNum, DailyAdult ); printf("\n"); printf( "Senior: %5d $%6.2f\n", DailySeniorNum, DailySenior ); printf("\n"); printf(" ======= ======= %s \n", Underlines); printf("\n\n"); printf( "Total: $%6.2f\n", DailyCost ); printf("\n"); printf( "Less Discount: $%6.2f\n", DailyDiscount ); printf("\n"); printf( "Total After Discount: $%6.2f\n", DailyTotal ); printf("\n"); return; } /*+*+*+*+*+*+*+* Displays information that the user entered *+*+*+*+*+*+*+*/ void YearlyPageDisplay(void) { system("cls"); PrintAStarLine(); printf(" Home Aquarium Contract \n"); printf("\n\n"); PrintAStarLine(); printf("Customer: %s \n", customerName); if (TicketNumber == "Y") { printf("Type of ticket: Yearly \n", TicketNumber); printf("\n\n"); } else if(strcmp(TicketNumber,"y")==0) { printf("Type of ticket: Yearly \n", TicketNumber); printf("\n\n"); } else if(strcmp(TicketNumber,"Yearly")==0) { printf("Type of ticket: Yearly \n", TicketNumber); printf("\n\n"); } else if(strcmp(TicketNumber,"yearly")==0) { printf("Type of ticket: Yearly \n", TicketNumber); printf("\n\n"); } system("pause"); printf(" Number Charge %s \n", NumberName); printf("\n\n"); printf( "Child: %5d $%6.2f\n", YearlyChildNum, YearlyChild ); printf("\n"); printf( "Adult: %5d $%6.2f\n", YearlyAdultNum, YearlyAdult ); printf("\n"); printf( "Senior: %5d $%6.2f\n", YearlySeniorNum, YearlySenior ); printf("\n"); printf(" ======= ======= %s \n", Underlines); printf("\n\n"); printf( "Total: $%6.2f\n", YearlyCost ); printf("\n"); printf( "Less Discount: $%6.2f\n", YearlyDiscount ); printf("\n"); printf( "Total After Discount: $%6.2f\n", YearlyTotal ); printf("\n"); return; } /*+*+*+*+*+*+*+* Performs Calculations *+*+*+*+*+*+*+*/ void DailyCalculations(void) { /* Performs Daily Calculations */ DailyCost = (DailyChild) + (DailyAdult) + (DailySenior); if ((DailyAdult > 2) && (DailyChild > 1)) DailyDiscount = float(DailyCost * .10); else if ((DailySenior > 3) && (DailyChild > 1)) DailyDiscount = float(DailyCost * .10); else DailyDiscount = (DailyCost - DailyCost); DailyTotal = (DailyCost - DailyDiscount); return; } void YearlyCalculations(void) { /* Performs Yearly Calculations */ YearlyCost = (YearlyChild) + (YearlyAdult) + (YearlySenior); if ((YearlyAdult > 2) && (YearlyChild > 1)) YearlyDiscount = float(YearlyCost * .10); else if ((YearlySenior > 3) && (YearlyChild > 1)) YearlyDiscount = float(YearlyCost * .10); else YearlyDiscount = (YearlyCost - YearlyCost); YearlyTotal = (YearlyCost - YearlyDiscount); return; } void TotalCalculation(void) { /* Performs Yearly Calculations */ TotalChild = (DailyChild + YearlyChild) ; TotalAdult = (DailyAdult + YearlyAdult); TotalSenior = (DailySenior + YearlySenior); TotalCost = (DailyCost + YearlyCost); TotalDiscount = (DailyDiscount + YearlyDiscount); Total = (TotalCost - TotalDiscount); } /*+*+*+*+*+*+*+* Asks The User A Question *+*+*+*+*+*+*+*/ void AskQuestion(void) { printf("\n\nAre we done for the day? (Yes/No)"); scanf(" %c", &aQuestion); fflush(stdin); return; } /*+*+*+*+*+*+*+* Displays Final Cost to User *+*+*+*+*+*+*+*/ void TotalPageDisplay(void) { system("cls"); PrintAStarLine(); printf(" Home Aquarium Daily Totals \n"); printf("\n\n"); PrintAStarLine(); printf("Total Total \nNumber Income %s \n", TotalIncome); printf("\n\n"); printf(" Child: $%6.2f\n", TotalChild ); printf("\n"); printf( " Adult: $%6.2f\n", TotalAdult ); printf("\n"); printf( " Senior: $%6.2f\n", TotalSenior ); printf("\n"); printf("________________________________________________________________________________%s \n", Underlines); printf( "Grand Total: $%6.2f\n", TotalCost ); printf("\n"); printf( "Grand Total After Discount: $%6.2f\n", Total ); printf("\n"); return; }
![]() |
Similar Threads
- Source code for printf, scanf, cin, cout? (C)
- Handling Invalid User Input? (C)
- Java Multidimensional Arrays (Java)
- simple calculations How? intHrlyRate, etc Help me (ASP)
- Math problem in C (C)
- Using printf with a file (C++)
- hm.. wiered.. (C)
- printf buffer strange behaviur (C)
Other Threads in the C Forum
- Previous Thread: Delay
- Next Thread: Computer Oriented Numerical Methods
| Thread Tools | Search this Thread |
adobe ansi api array arrays bash binarysearch centimeter char convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork frequency getlasterror getlogicaldrivestrin givemetehcodez global graphics gtkgcurlcompiling hardware highest homework i/o ide inches infiniteloop initialization interest kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix meter microsoft motherboard multi mysql odf open opendocumentformat opensource openwebfoundation owf pattern pdf performance pointer pointers posix power probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling segmentationfault send shape single socketprograming socketprogramming stack standard strchr string strings suggestions systemcall test testautomation unix urboc user voidmain() wab win32api windows.h






