C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old March 3rd, 2006, 11:06 AM
Amber311 Amber311 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2006
Location: Florida
Posts: 2 Amber311 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 35 m 21 sec
Reputation Power: 0
Question Help w/ simple program

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;
}

Reply With Quote
  #2  
Old March 3rd, 2006, 01:19 PM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 1,001 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 1003874 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1003874 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1003874 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1003874 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1003874 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1003874 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1003874 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1003874 Folding Title: Super Ultimate Folder - Level 3
Time spent in forums: 6 Days 15 h 6 m 59 sec
Reputation Power: 6
Send a message via ICQ to Itsacon
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

Reply With Quote
  #3  
Old March 3rd, 2006, 01:48 PM
Amber311 Amber311 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2006
Location: Florida
Posts: 2 Amber311 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 35 m 21 sec
Reputation Power: 0
Unhappy

Quote:
Originally Posted by Itsacon
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?


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)
{

Reply With Quote
  #4  
Old March 14th, 2006, 12:28 AM
CarpeDiem CarpeDiem is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2006
Posts: 1 CarpeDiem User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 14 m 40 sec
Reputation Power: 0
Let's start from the top.

Quote:
//thank user
printf(THANKS);
system("pause");
}
return 0;
}


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:
do
{
printf(NUM_DOGS);
scanf(" %d",&numberOfDogs);
if ((numberOfDogs > 3) || (numberOfDogs < 1)){
printf(ERR_MSG_DOGS);
}
while ((numberOfDogs > 3) || (numberOfDogs < 1));


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:
do
{
printf(NUM_DAYS);
scanf(" %d",&numberOfDays);
if ((numberOfDays > 10) || (numberOfDays < 3))
{
printf(ERR_MSG_DAYS);
}
while((numberOfDays > 10) || (numberOfDays < 3));



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.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Help w/ simple program


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump



 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT