| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
I just started C programming class... first time doing this and really my first program. I got a 0 on the first one because the professor doesn't accept anything late. Anyway, I need help with this one... this is what I want to do:
1. loop through main until the user enters 0, then exit program. 2. call functions and loop through them, display error if someone enteres the wrong value I had the program working, and then now it will not display a thing. when i enter something, it loops and doesn't quit. -------------MY WORST NIGHTMARE PROGRAM----------- #include <stdio.h> #include <ctype.h> #define CO_NAME " Donny's Doggy Den\n\n\n Enter the number of dogs that will be boarded - maximum 3\n (enter 0 to quit the program\n\n " #define NUM_DOGS "\n\tNumber of dogs to be boarded: " #define NUM_DAYS "\n\tNumber of days: " #define DOG1_COST 20.00 #define DOG2_COST 30.00 #define DOG3_COST 35.00 #define OUTPUT_MSG "\tCost for boarding is: $%.2lf \n" #define ERR_MSG_DOGS "\n\tERROR, enter between 1 and 3 dogs. \n\n" #define ERR_MSG_DAYS "\n\tERROR, enter between 3 and 10 days. \n\n" #define THANKS "\n\n\tThanks for using Donny's Doggy Den Customer System\n\n" //************************************************** * // prototypes //************************************************** ** int getDogs(); int getDays(); int displayCost(double ans); //************************************************** ****** // main function of the program //************************************************** ****** int main() { int numDogs, numDays; double costPerDay, answer; //display company name printf (CO_NAME); //call fx to get number of dogs, assign return value to a variable numDogs = getDogs(); //call fx to get number of days, assign return value to a variable numDays = getDays(); //calculate cost if (numDogs == 1) { costPerDay = DOG1_COST; } else if (numDogs == 2) { costPerDay = DOG2_COST; } else if (numDogs == 3) { costPerDay = DOG3_COST; } answer = costPerDay * numDays; //call fx to display cost displayCost(answer); //thank user printf(THANKS); system("pause"); } return 0; } /************************************************ function getDogs() prompts the user for the number of dogs and returns that value *************************************************/ int getDogs() { int numberOfDogs; do { printf(NUM_DOGS); scanf(" %d",&numberOfDogs); if ((numberOfDogs > 3) || (numberOfDogs < 1)){ printf(ERR_MSG_DOGS); } while ((numberOfDogs > 3) || (numberOfDogs < 1)); return numberOfDogs; } /************************************************ function getDays() prompts the user for the number of days and returns that value *************************************************/ int getDays() { int numberOfDays; do { printf(NUM_DAYS); scanf(" %d",&numberOfDays); if ((numberOfDays > 10) || (numberOfDays < 3)) { printf(ERR_MSG_DAYS); } while((numberOfDays > 10) || (numberOfDays < 3)); return numberOfDays; } /************************************************ function display answer receives the cost as an input parameter and displays it *************************************************/ int displayCost(double ans) { printf(OUTPUT_MSG, ans); return 0; } |
|
#2
|
||||
|
||||
|
There's a closing brace too many in the main(), but that would just give a compiler error. What exactly is the problem?
You say you want to loop through the main program until a user enters a '0', but there is no input in the main(), so where do you plan to catch that zero?
__________________
This is my code. Is it not nifty? "The biggest problem encountered while trying to design a system that was completely foolproof, was, that people tended to underestimate the ingenuity of complete fools." ---Douglas Adams Join the Itsacon fanclub! Zero Tolerance: Spammers banned so far: 280
![]() |
|
#3
|
|||
|
|||
|
Quote:
I tried my best and changed, deleted, and moved the braces and still get the errors. I guess I need to do an if statement in the main program.... this is what I did in the beginning... int main() { int num; while (num != 0) { all that other stuff then I want to put the if statement somewhere in Main (I don't know where!) if (num != 0) { |
|
#4
|
|||||
|
|||||
|
Let's start from the top.
Quote:
The code pasted in the original post had an extra brace before the return 0. Also, in looking at your functions, I see some problems. For instance, Quote:
Notice that the while loop will continue as long as numberOfDogs > 3 or numberOfDogs is < 1. Essentially, it will never end. Change the while ((numberOfDogs > 3) || (numberOfDogs < 1)); to while ((numberOfDogs > 3) && (numberOfDogs < 1)); In regard to the number of days function, well Quote:
I see two problems: 1) The do while loop in the number of days functions has the same problem as above. Change the "while((numberOfDays > 10) || (numberOfDays < 3));" to while((numberOfDays > 10) && (numberOfDays < 3)); while 2) The function is missing a closing brace for the do while loop. There is a opening and closing brace for the if statment but not the do while loop. And finally, one of the best things that you can do is ensure your code is formatted properly. Readability is a big portion of software development and your code isn't easy to read. Use indentions and braces, try to ensure lines are lined-up, etc. |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Help w/ simple program |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|