| | |
Concerning C++ calculations using printf
Thread Solved
![]() |
•
•
Join Date: Aug 2007
Posts: 58
Reputation:
Solved Threads: 0
I'm currently having a problem with having where it says DailyChild on my code to go up when the user adds a number. For example if they type 1 it should say 1.50. If they enter 2, it should say 3.00. But no matter what I do, it keeps printing coming up to 1.50. Can someone show me what I'm doing wrong please, really in a jam on trying to get this to work. Also may someone be able to tell me why when I compile my program it shows no errors but when I'm bout to exit the program it says theres an error. You'll notice it when you enter in the code. Also if you notice any other problems with my code, please let me know. Thanks for the help, I'm patiently hoping for a reply soon!
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <iostream> void PrintAStarLine(void); void GetInput(void); void GetTicket(void); void HowManyChildren(void); void HowManyAdults(void); void HowManySeniors(void); void ChildrenCalculations(void); float DailyChild; float YearlyChild; float DailyAdult; float YearlyAdult; float DailySenior; float YearlySenior; char customerName[30]; char tickettype[8]; int childrenNum; int adultNum; int seniorNum; main() { { PrintAStarLine(); printf(" Home Aquarium Data Entry \n"); printf("\n\n"); PrintAStarLine(); GetInput(); } GetTicket(); return 0; } void PrintAStarLine(void) { printf("**************************************************************************** \n"); printf("\n\n"); return; } void GetInput(void) { printf("Customer Name (First Middle Last): "); scanf(" %s %s %s", &customerName); printf("\n\n\n"); return; } void GetTicket(void) { printf("Type of ticket (Daily(D) or Yearly(Y)): "); scanf(" %s", &tickettype); if (strcmp (tickettype,"D") == 0) { HowManyChildren(); HowManyAdults(); HowManySeniors(); printf("Children total cost: %10.2f\n", DailyChild); printf("Adult total cost: %10.2f\n", DailyAdult); printf("Senior total cost: %10.2f\n", DailySenior); } else if (strcmp (tickettype,"d") == 0) { HowManyChildren(); HowManyAdults(); HowManySeniors(); printf("Children total cost: %10.2f\n", DailyChild); printf("Adult total cost: %10.2f\n", DailyAdult); printf("Senior total cost: %10.2f\n", DailySenior); } else if (strcmp (tickettype,"Daily") == 0) { HowManyChildren(); HowManyAdults(); HowManySeniors(); printf("Children total cost: %10.2f\n", DailyChild); printf("Adult total cost: %10.2f\n", DailyAdult); printf("Senior total cost: %10.2f\n", DailySenior); } else if (strcmp (tickettype,"daily") == 0) { HowManyChildren(); HowManyAdults(); HowManySeniors(); printf("Children total cost: %10.2f\n", DailyChild); printf("Adult total cost: %10.2f\n", DailyAdult); printf("Senior total cost: %10.2f\n", DailySenior); } else if (strcmp (tickettype,"Y") == 0) { HowManyChildren(); HowManyAdults(); HowManySeniors(); printf("Children total cost: %10.2f\n", YearlyChild); printf("Adult total cost: %10.2f\n", YearlyAdult); printf("Senior total cost: %10.2f\n", YearlySenior); } else if (strcmp (tickettype, "y") == 0) { HowManyChildren(); HowManyAdults(); HowManySeniors(); printf("Children total cost: %10.2f\n", YearlyChild); printf("Adult total cost: %10.2f\n", YearlyAdult); printf("Senior total cost: %10.2f\n", YearlySenior); } else if (strcmp (tickettype,"Yearly") == 0) { HowManyChildren(); HowManyAdults(); HowManySeniors(); printf("Children total cost: %10.2f\n", YearlyChild); printf("Adult total cost: %10.2f\n", YearlyAdult); printf("Senior total cost: %10.2f\n", YearlySenior); } else if (strcmp (tickettype,"yearly") == 0) { HowManyChildren(); HowManyAdults(); HowManySeniors(); printf("Children total cost: %10.2f\n", YearlyChild); printf("Adult total cost: %10.2f\n", YearlyAdult); printf("Senior total cost: %10.2f\n", YearlySenior); } else { printf("\nIncorrect Input, please try again.\n"); printf("\n\n\n"); GetTicket(); } printf("\n\n"); return; } void HowManyChildren(void) { DailyChild=1.50; YearlyChild = 5.50; printf("\n"); printf("#of Children: "); scanf(" %d", &childrenNum); printf("\n"); if (childrenNum <= 999) { } else { printf("Invalid Number Entered, please try again."); printf("\n\n\n"); HowManyChildren(); } return; } void HowManyAdults(void) { DailyAdult = 2.00; YearlyAdult = 6.50; printf("\n"); printf("#of Adults: "); scanf(" %d", &adultNum); printf("\n"); if (adultNum <= 999) { } else { printf("Invalid Number Entered, please try again."); printf("\n\n\n"); HowManyAdults(); } return; } void HowManySeniors(void) { DailySenior = 1.25; YearlySenior= 3.75; printf("\n"); printf("#of Seniors: "); scanf(" %d", &seniorNum); printf("\n"); if (seniorNum <= 999) { } else { printf("Invalid Number Entered, please try again."); printf("\n\n\n"); system("pause"); HowManySeniors(); } return ; } void ChildrenCalculations(void) { return; }
Last edited by Ancient Dragon; Aug 4th, 2007 at 6:05 pm. Reason: add code tags
This isn't C++ code, its C. (The two languages are very different)
Probably the reason that you get an error when your code exits, is that your declaration of main() is ill-formed.
Change your main declaration so that it looks like this -
As for your issue of the wrong output, it looks as if your program doesn't do anything with the number of children after you prompt the user for it.
I think your program has a lot of design issues - you're using alot of global variables, this is one way to make your program very messy and hard to follow. Try rearranging your program so that variables are declared where they're needed, and passed or returned to/from functions instead. You should find that the logic of the program is much easier to trace by doing this.
Probably the reason that you get an error when your code exits, is that your declaration of main() is ill-formed.
Change your main declaration so that it looks like this -
C Syntax (Toggle Plain Text)
int main(void)
As for your issue of the wrong output, it looks as if your program doesn't do anything with the number of children after you prompt the user for it.
I think your program has a lot of design issues - you're using alot of global variables, this is one way to make your program very messy and hard to follow. Try rearranging your program so that variables are declared where they're needed, and passed or returned to/from functions instead. You should find that the logic of the program is much easier to trace by doing this.
Last edited by Bench; Aug 4th, 2007 at 5:50 pm.
¿umop apisdn upside down? •
•
Join Date: Aug 2007
Posts: 58
Reputation:
Solved Threads: 0
As far as the arranging of my code, I'll work on that right away. I'm still new to it, and havent gotten any help concerning C programming until now. I'm in a C++ class atm, but it seems we are starting out with C coding first. I'm still trying to figure out how to get the calculations to work in my program. If someone can shed some light on that area for me, it would be greatly appreciated. Thanks again.
Look at this section of code, where you ask the user to enter the number of children
After asking the user to input the number of children, your program doesn't do anything else with childrenNum aside from checking that its less than 999.
Somewhere you want to calculate the ticket cost using this number and your cost-per-ticket. Maybe you intended to put that in your ChildrenCalculations() function.
- With regards to C vs C++ - These days, its unusual for a C++ course to use things like printf, scanf, strcmp (They're valid in C++, but only for the sake of compatibility with C). Most modern C++ courses don't teach the language as 'a better C' - although there's nothing wrong with learning both languages, you'll find alot of things done differently when you move onto C++.
void HowManyChildren(void)
{
DailyChild=1.50;
YearlyChild = 5.50;
printf("\n");
printf("#of Children: ");
scanf(" %d", &childrenNum);
printf("\n");
if (childrenNum <= 999)
{
}
else
{
printf("Invalid Number Entered, please try again.");
printf("\n\n\n");
HowManyChildren();
}
return;
}Somewhere you want to calculate the ticket cost using this number and your cost-per-ticket. Maybe you intended to put that in your ChildrenCalculations() function.
- With regards to C vs C++ - These days, its unusual for a C++ course to use things like printf, scanf, strcmp (They're valid in C++, but only for the sake of compatibility with C). Most modern C++ courses don't teach the language as 'a better C' - although there's nothing wrong with learning both languages, you'll find alot of things done differently when you move onto C++.
¿umop apisdn upside down? •
•
•
•
I'm still trying to figure out how to get the calculations to work in my program. If someone can shed some light on that area for me, it would be greatly appreciated. Thanks again.
My suggestion is that you should work little by little in the steps that
you need to take for you code to work. Compile each of those steps in separate stages and if they work implement them together. That way the bugs would have a harder time compounding.
Let's look at any of your functions. All seems to be suffering of the same problems.
C Syntax (Toggle Plain Text)
void HowManySeniors(void) { DailySenior = 1.25; YearlySenior= 3.75; printf("\n"); printf("#of Seniors: "); scanf(" %d", &seniorNum); printf("\n"); if (seniorNum <= 999) { } else { printf("Invalid Number Entered, please try again."); printf("\n\n\n"); system("pause"); HowManySeniors(); } return ; }
DailySenior = 1.25;
YearlySenior= 3.75;
Nothing is using those values inside the function, therefore no reason to be there.
scanf(" %d", &seniorNum);
scanf() is nothing but a lot of grief for any one that uses it. Specially, if you read strings with it. Avoid using that function.
Here's a couple of links that would help you to understand better.
http://www.daniweb.com/tutorials/tutorial45806.htm
http://www.daniweb.com/code/snippet266.htmli
http://www.gidnetwork.com/b-59.html
if (seniorNum <= 999)
{
}
What are you trying to do with that if? That construction is incorrect.
If you want the if() to do nothing it must be something like:
c Syntax (Toggle Plain Text)
if (seniorNum <= 999) /* if Seniors are less or iquals to 999 */ { ; /* do nothing */ }
system("pause");
Avoid that too. Read here about it.
•
•
Join Date: Aug 2007
Posts: 58
Reputation:
Solved Threads: 0
Thanks for the info guys. I changed some stuff around in my code and got rid of some of the stuff I dont need. I would use a different code but my instructor wants us to use printf/scanf coding specifically. I'm stil having trouble with getting the calculation to work. I think I'm close though. Also I'm trying to figure out how I can make my program deny letters if its only asking for numbers. For example when it asks for the Customer's Name, I'm trying to make it reject the input if the user were to enter a number for their name, same thing with when it asks for number of children and so forth. I appreciate the help you guys have given me and will try my best to get better. This is the code again below.
c Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <iostream> void PrintAStarLine(void); void GetInput(void); void GetTicket(void); void HowManyChildren(void); void HowManyAdults(void); void HowManySeniors(void); void TotalCost(void); float DailyChild; float YearlyChild; float DailyAdult; float YearlyAdult; float DailySenior; float YearlySenior; char customerName[30]; char tickettype[8]; int main(void) { { PrintAStarLine(); printf(" Home Aquarium Data Entry \n"); printf("\n\n"); PrintAStarLine(); GetInput(); } GetTicket(); HowManyChildren(); HowManyAdults(); HowManySeniors(); TotalCost(); return 0; } void PrintAStarLine(void) { printf("**************************************************************************** \n"); printf("\n\n"); return; } void GetInput(void) { printf("Customer Name (First Middle Last): "); scanf(" %s %s %s", &customerName); printf("\n\n\n"); return; } void GetTicket(void) { printf("Type of ticket (Daily(D) or Yearly(Y)): "); scanf(" %s", &tickettype); if (strcmp (tickettype,"D") == 0) { } else if (strcmp (tickettype,"d") == 0) { } else if (strcmp (tickettype,"Daily") == 0) { } else if (strcmp (tickettype,"daily") == 0) { } else if (strcmp (tickettype,"Y") == 0) { } else if (strcmp (tickettype, "y") == 0) { } else if (strcmp (tickettype,"Yearly") == 0) { } else if (strcmp (tickettype,"yearly") == 0) { } else { printf("\nIncorrect Input, please try again.\n"); printf("\n\n"); GetTicket(); } printf("\n\n"); return; } void HowManyChildren(void) { DailyChild = 1.50; YearlyChild = 5.50; printf("\n"); printf("#of Children: "); scanf(" %f", &DailyChild); printf("\n"); if (DailyChild <= 999) { ; } else { printf("Invalid Number Entered, please try again."); printf("\n\n\n"); printf("\n"); printf("#of Children: "); scanf(" %f", &YearlyChild); printf("\n"); HowManyChildren(); } if (YearlyChild <= 999) { ; } else { printf("Invalid Number Entered, please try again."); printf("\n\n\n"); HowManyChildren(); } return; } void HowManyAdults(void) { DailyAdult = 2.00; YearlyAdult = 6.50; printf("\n"); printf("#of Adults: "); scanf(" %f", &DailyAdult); printf("\n"); if (DailyAdult <= 999) { ; } else { printf("Invalid Number Entered, please try again."); printf("\n\n\n"); HowManyAdults(); } return; } void HowManySeniors(void) { DailySenior = 1.25; YearlySenior= 3.75; printf("\n"); printf("#of Seniors: "); scanf(" %f", &DailySenior); printf("\n"); if (DailySenior <= 999) { ; } else { printf("Invalid Number Entered, please try again."); printf("\n\n\n"); system("PAUSE"); HowManySeniors(); } return ; } void TotalCost(void) { printf("Children total cost: %10.2f\n", DailyChild); printf("Adult total cost: %10.2f\n", DailyAdult); printf("Senior total cost: %10.2f\n", DailySenior); return ; }
Last edited by Ancient Dragon; Aug 5th, 2007 at 1:02 am. Reason: add code tags -- please start using them
>I would use a different code but my instructor wants us to use printf/scanf coding specifically
Well, if you must use scanf() let's learn about it correctly, shall we?
Let's look at this function, as an example:
scanf(" %s %s %s", &customerName);
There's not chance this is going to fly.
First: You are trying to read a string. The proper arguments for that are:
scanf( "%s", customerName ); you don't want the & operator there. & is only for integers and solo chars.
I suppose you could write something like:
scanf( "%s%s%s", first_array, second_array, third_array );
passing to it three different arrays but don't do it.
scanf() stops reading from the buffer as soon as encounters a space.
That implies that if the user enters name, middle name and last name. You code goes where many codes has gone before.
What can you do?. One way is to tell scanf how much you want to read. It is not pretty but doable.
scanf( "%[^\n]", customerName ); This statement tells scanf to read until it encounters a newline. But it leaves that newline behind in the stdin buffer.
To pick that newline we could write it like:
char newline;
scanf( "%[^\n]%c", customerName, &newline );
Don't mention to anyone I told you so.
Well, if you must use scanf() let's learn about it correctly, shall we?
Let's look at this function, as an example:
c Syntax (Toggle Plain Text)
void GetInput(void) { printf("Customer Name (First Middle Last): "); scanf(" %s %s %s", &customerName); printf("\n\n\n"); return; }
scanf(" %s %s %s", &customerName);
There's not chance this is going to fly.
First: You are trying to read a string. The proper arguments for that are:
scanf( "%s", customerName ); you don't want the & operator there. & is only for integers and solo chars.
I suppose you could write something like:
scanf( "%s%s%s", first_array, second_array, third_array );
passing to it three different arrays but don't do it.
scanf() stops reading from the buffer as soon as encounters a space.
That implies that if the user enters name, middle name and last name. You code goes where many codes has gone before.
What can you do?. One way is to tell scanf how much you want to read. It is not pretty but doable.
scanf( "%[^\n]", customerName ); This statement tells scanf to read until it encounters a newline. But it leaves that newline behind in the stdin buffer.
To pick that newline we could write it like:
char newline;
scanf( "%[^\n]%c", customerName, &newline );
Don't mention to anyone I told you so.
Last edited by Aia; Aug 5th, 2007 at 1:47 am.
>Now just to figure out this calculation problem
I think I know what your want to do, but I am not sure.
Perhaps this piece of code will demostrate an example of how to do it.
I think I know what your want to do, but I am not sure.
Perhaps this piece of code will demostrate an example of how to do it.
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 */
![]() |
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 asterisks binarysearch calculate centimeter changingto char convert copyanyfile copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile createprocess() csyntax database directory fflush file fork forloop frequency givemetehcodez global grade gtkgcurlcompiling hacking highest histogram homework i/o inches infiniteloop input interest iso kernel kilometer km linked linkedlist linux linuxsegmentationfault list locate looping loopinsideloop. lowest match meter microsoft mysql number open opendocumentformat openwebfoundation owf pattern pdf performance posix power probleminc process program programming pyramidusingturboccodes radix read recv repetition research reversing scanf scheduling segmentationfault send sequential socket socketprograming socketprogramming stack standard string suggestions systemcall threads turboc unix user variable voidmain() wab win32api windows.h windowsapi






