| | |
Concerning C++ calculations using printf
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
You are calling this function the wrong way. You are passing the wrong arguments.
This is how the function is declared.
float price( const float ticket, int *num );
accept first an unchangeable float that's what const means, and a pointer to an int variable. OK? Now let's take a piece of your code where you call this function.
DailyChild = price( &DailyChildNum, DChild );
use are passing &DailyChildNum as the first argument. What is it?. The address of an int.
price only accepts a const float. Can you understand? Wrong type of variable and it cannot be the address.
Let's look at the second argument: DChild.
What is it? const float DChild = 1.50; A const float. What should be there? A pointer to an int. Something like &DChild if that were an int, which is not.
It seems that you have them switched.
By the way there's not reason for that function to not be declared after main like the others and prototyped at the beginning like the others.
This is how the function is declared.
float price( const float ticket, int *num );
accept first an unchangeable float that's what const means, and a pointer to an int variable. OK? Now let's take a piece of your code where you call this function.
DailyChild = price( &DailyChildNum, DChild );
use are passing &DailyChildNum as the first argument. What is it?. The address of an int.
price only accepts a const float. Can you understand? Wrong type of variable and it cannot be the address.
Let's look at the second argument: DChild.
What is it? const float DChild = 1.50; A const float. What should be there? A pointer to an int. Something like &DChild if that were an int, which is not.
It seems that you have them switched.
By the way there's not reason for that function to not be declared after main like the others and prototyped at the beginning like the others.
Last edited by Aia; Aug 10th, 2007 at 12:42 am.
•
•
Join Date: Aug 2007
Posts: 58
Reputation:
Solved Threads: 0
I lastly wanted to ask, although its not really needed, but for future purposes, how would I be able to save the input I entered. For example when the program runs and the buyer purchases a yearly ticket and then purchase a daily ticket both totals are displayed. But lets say the customer purchases 2 or more daily tickets it only shows the total for the last number entered. This is my current code.
[/INLINECODE][/code]
C Syntax (Toggle Plain Text)
[INLINECODE][CODE]/*+*+*+*+*+*+*+* 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; int TotalChildNum; int TotalAdultNum; int TotalSeniorNum; 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( DChild, &DailyChildNum ); printf("\n\n"); printf("#of Adults: " ); DailyAdult = price( DAdult, &DailyAdultNum ); printf("\n\n"); printf("#of Seniors: " ); DailySenior = price( DSenior, &DailySeniorNum ); printf("\n\n"); return; } /*+*+*+*+*+*+*+* Allows the user to enter yearly tickets *+*+*+*+*+*+*+*/ void YearlyTickets(void) { printf("\n\n"); printf("#of Children: " ); YearlyChild = price( YChild, &YearlyChildNum); printf("\n\n"); printf("#of Adults: " ); YearlyAdult = price( YAdult, &YearlyAdultNum); printf("\n\n"); printf("#of Seniors: " ); YearlySenior = price( YSenior, &YearlySeniorNum); 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("\t\t\t\t Number\t\t\t\t Charge%s \n", NumberName); printf("\n\n"); printf( "Child:\t\t\t\t%5d\t\t\t\t$%6.2f\n", DailyChildNum, DailyChild ); printf("\n"); printf( "Adult:\t\t\t\t%5d\t\t\t\t$%6.2f\n", DailyAdultNum, DailyAdult ); printf("\n"); printf( "Senior:\t\t\t\t%5d\t\t\t\t$%6.2f\n", DailySeniorNum, DailySenior ); printf("\n"); printf(" ======= ======= %s \n", Underlines); printf("\n\n"); printf( "Total:\t\t\t\t\t\t\t\t$%6.2f\n", DailyCost ); printf("\n"); printf( "Less Discount:\t\t\t\t\t\t\t$%6.2f\n", DailyDiscount ); printf("\n"); printf( "Total After Discount:\t\t\t\t\t\t$%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("\t\t\t\t Number\t\t\t\t Charge%s \n", NumberName); printf("\n\n"); printf( "Child:\t\t\t\t%5d\t\t\t\t$%6.2f\n", YearlyChildNum, YearlyChild ); printf("\n"); printf( "Adult:\t\t\t\t%5d\t\t\t\t$%6.2f\n", YearlyAdultNum, YearlyAdult ); printf("\n"); printf( "Senior:\t\t\t\t%5d\t\t\t\t$%6.2f\n", YearlySeniorNum, YearlySenior ); printf("\n"); printf(" ======= ======= %s \n", Underlines); printf("\n\n"); printf( "Total:\t\t\t\t\t\t\t\t$%6.2f\n", YearlyCost ); printf("\n"); printf( "Less Discount:\t\t\t\t\t\t\t$%6.2f\n", YearlyDiscount ); printf("\n"); printf( "Total After Discount:\t\t\t\t\t\t$%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); TotalChildNum = DailyChildNum + YearlyChildNum; TotalAdultNum = DailyAdultNum + YearlyAdultNum; TotalSeniorNum = DailySeniorNum + YearlySeniorNum; return; } /*+*+*+*+*+*+*+* 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("%d Child: $%6.2f\n", TotalChildNum, TotalChild ); printf("\n"); printf( "%d Adult: $%6.2f\n", TotalAdultNum, TotalAdult ); printf("\n"); printf( "%d Senior: $%6.2f\n", TotalSeniorNum, 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 |
#include adobe ansi api append array arrays asterisks binarysearch changingto char character cm copyimagefile cprogramme creafecopyofanytypeoffileinc csyntax database directory dynamic execv feet fgets file fork framework frequency function getlasterror givemetehcodez global grade gtkgcurlcompiling hacking hardware highest histogram include incrementoperators infiniteloop input interest kernel keyboard kilometer license linked linkedlist linux linuxsegmentationfault list lists locate logical_drives looping loopinsideloop. lowest match matrix meter microsoft motherboard mqqueue number odf opensource overwrite owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research reversing scripting segmentationfault sequential socket socketprograming standard string systemcall testing threads turboc unix user voidmain() wab windows.h windowsapi






