GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 02-Mar-2008, 23:34
mysticacxwolf mysticacxwolf is offline
New Member
 
Join Date: Feb 2008
Posts: 2
mysticacxwolf is on a distinguished road

Function creating help


Disclaimer: this is a homework assignment.

I am trying to write a program for an imaginary widget producing company. Each month, Widget Co. produces 123 new widgets from its production line during a good month, but only 52 widgets from its production line during a bad month. In addition, each month, its sales team takes orders for some number of widgets. If they are on-hand, Widget Co. reduces its inventory of widgets. If not on-hand, it compiles a back-order for the requested widgets. In order to receive full credit, you must use functions for this calculation and pass parameters and work with return values.

The program should produce this when executed:

Welcome to Widget Co.!
Month: 0 Number of widgets on-hand: 0 Number back-ordered: 0
Was it a good or bad month (g/b)?
How many did the sales force sell?
Continue(y/n)? y

If you continue to press Y it should increment each month and continue calculating the number of widgets produced, sold, and on back order.

This is what I have so far:

CPP / C++ / C Code:
#include <iostream>

using namespace std;
int widget_calculator();

int main ()
{
    int month= 0;
    int input= 0;
    char ans;
    do {
    cout << "Welcome to Widget Inc.!\n";
    cout << "Month: " << month << endl;
    cout << "Number of widgets on-hand: " << widget_calculator(input);
    month ++; 
         }
         while( ans == 'y' || ans== 'Y');
    return 0;
}
    


// Function to calculate the number of widgets produced and sold for each month.
int widget_calculator(char pickup, int widgets, int sell, int number_of_widgets)
{
           
    cout <<" Was it a good or a bad month(g/b).\n";
    cin >> pickup;
        
    if( pickup== 'g' || pickup=='G')
    {
        widgets= 123;
        }
    else {
         widgets= 59;
         }
    cout << "How many widgets did you sell.\n";
    cin >> sell; 
        
    number_of_widgets= widgets - sell;
    return number_of_widgets;
}

I get 2 errors so far. On line 4 my compiler complains I have too many arguments in my function. On line 14 it says at this point in file, which is where I call my function so I figure if I alleviate the first error the second should disappear. I guess I have more parameters than have defined in the function, but I am not sure how else I could get what I want.

Just looking for someone to point me in the right direction.

I run on Windows XP Professional.

My compile software is called Dev-C++. This is my second semester of programming.

thanks

Howard
  #2  
Old 03-Mar-2008, 05:32
cable_guy_67's Avatar
cable_guy_67 cable_guy_67 is offline
Senior Member
 
Join Date: Oct 2004
Location: Nescopeck, PA
Posts: 1,109
cable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the rough

Re: Function creating help


Howard,

You are thinking along the right track. Each time you refer to widget_calculator you need to use the same number of arguments. With the way you are using it I would guess you just need one, an integer for passing the number of widgets on hand.

Then you will also need to declare the other variables local to your widget_calculator function it its body.

Mark
__________________
"Opportunity is missed by most people because it comes dressed in overalls and looks like work."
--Thomas Alva Edison
"Those who would give up essential liberty to purchase a little temporary safety, deserve neither liberty nor safety."
--Benjamin Franklin
"A happy person is not a person in a certain set of circumstances, but rather a person with a certain set of attitudes."
--Hugh Downs
  #3  
Old 06-Mar-2008, 00:29
mysticacxwolf mysticacxwolf is offline
New Member
 
Join Date: Feb 2008
Posts: 2
mysticacxwolf is on a distinguished road

Re: Function creating help


Well I have modified my program where I don't get any compile errors. Now I just get a fatal logic error.

CPP / C++ / C Code:
#include <iostream>

using namespace std;
int widget_calculator(int widgets, int sell);

int main ()
{
    int month= 0, widgets1= 0, sell1= 0; 
    int input= 0;
    char ans,pickup;
    int total= 0;
    
    do {
    cout << "Welcome to Widget Inc.!\n";
    cout << "Month: " << month << endl;
    cout << "Number of widgets on-hand: " << total;
    cout << "\n";
    cout <<"Was it a good or a bad month(g/b).\n";
    cin >> pickup;
        
    if( pickup== 'g' || pickup=='G')
    {
        widgets1= 123;
        }
    else 
    {
        widgets1= 59;
         }
    cout << "How many did the sales force sell: \n";
    cin >> sell1;
    
    total= widget_calculator(widgets1,sell1);
    
    month ++; 
    cout << "Continue [y/n]?\n";
    cin >> ans;
         }
         while( ans == 'y' || ans== 'Y');
    
    cout << "At the end of month" << month;
    cout << ",you wound up with" << total;
    cout << "on hand and some amount on back order.\n";
    return 0;
}
  
    


// Function to calculate the number of widgets produced and sold for each month.
int widget_calculator(int widgets, int sell)
{
    int number_of_widgets;
    
    number_of_widgets= widgets - sell;
    return (number_of_widgets);
}

When I run this modified version, it works fine for the first loop. But when you go into the second loop it produces results that I don't want.

For instance, if you typed in "g" for the first month being good, and the sales team selling 15 widgets the end result for Number of Widgets on Hand for the next month will be 108. However when you type in 'B' for bad month and a sell of 150 widgets you get -91 on hand for the next month.

I think the problem is that I have switched the order for my arguments I am not sure. I have been fiddling around with it for some time and can't seem to get the correct arrangement.


thanks again

Howard
  #4  
Old 06-Mar-2008, 05:02
rajeev nair rajeev nair is offline
Junior Member
 
Join Date: Aug 2006
Posts: 97
rajeev nair is an unknown quantity at this point

Re: Function creating help


Hi,

I would suggest one small change in your code :

CPP / C++ / C Code:
 if( pickup== 'g' || pickup=='G')
    {
        widgets1+= 123; 
     }
    else 
    {
        widgets1+= 59;
     }



You have been assigning a particular value for the variable (widgets1) everytime you call the loop and therefore the results were not what you expected when loop called after the first execution.

Regards,
Rajeev
  #5  
Old 06-Mar-2008, 13:40
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,242
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Function creating help


Quote:
Originally Posted by mysticacxwolf
For instance, if you typed in "g" for the first month being good, and the sales team selling 15 widgets the end result for Number of Widgets on Hand for the next month will be 108. However when you type in 'B' for bad month and a sell of 150 widgets you get -91 on hand for the next month.
Well, let's go through your code for the second loop:

CPP / C++ / C Code:
#include <iostream>

using namespace std;
int widget_calculator(int widgets, int sell);

int main ()
{
    int month= 0, widgets1= 0, sell1= 0; 
    int input= 0;
    char ans,pickup;
    int total= 0;
    
    do {
    cout << "Welcome to Widget Inc.!\n";
    cout << "Month: " << month << endl;
    cout << "Number of widgets on-hand: " << total;
    cout << "\n";
    cout <<"Was it a good or a bad month(g/b).\n";
    cin >> pickup;                      // 'b' is entered
        
    if( pickup== 'g' || pickup=='G')
    {
        widgets1= 123;
        }
    else 
    {
        widgets1= 59;                   // widgets1 is set to 59
         }
    cout << "How many did the sales force sell: \n";
    cin >> sell1;                       // enter 150
    
    total= widget_calculator(widgets1,sell1);  // returns 59 - 150
    
    month ++; 
    cout << "Continue [y/n]?\n";
    cin >> ans;
         }
         while( ans == 'y' || ans== 'Y');
    
    cout << "At the end of month" << month;
    cout << ",you wound up with" << total;
    cout << "on hand and some amount on back order.\n";
    return 0;
}
  


// Function to calculate the number of widgets produced and sold for each month.
int widget_calculator(int widgets, int sell)
{
    int number_of_widgets;
    
    number_of_widgets= widgets - sell;
    return (number_of_widgets);
}

So what's 59 - 150?
__________________

Age is unimportant -- except in cheese
  #6  
Old 06-Mar-2008, 18:28
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 425
Peter_APIIT is on a distinguished road

Re: Function creating help


Your function prototype doesn't match with function call and function definition.

Please look at it.
__________________
Linux is the best OS in the world.
 
 

Recent GIDBlogMeeting the local Iraqis by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Flex and bison coding lucky88star C++ Forum 5 24-Dec-2007 11:57
Need Help with input files. Efferus C++ Forum 2 24-Nov-2007 16:19
[Include] Doubly-linked List dsmith C Programming Language 6 14-Apr-2006 13:12
[Tutorial] Function Pointers aaroncohn C++ Forum 4 17-Feb-2006 11:33
Revising Script style ?????? pepee MySQL / PHP Forum 4 14-Apr-2004 04:59

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 05:17.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.