Renegade Public Forums
C&C: Renegade --> Dying since 2003™, resurrected in 2024!
Home » Technical Support » Other » Need help with my C++ code
Need help with my C++ code [message #445206] Wed, 23 March 2011 12:15 Go to next message
_SSnipe_ is currently offline  _SSnipe_
Messages: 4121
Registered: May 2007
Location: Riverside Southern Califo...
Karma: 0
General (4 Stars)
ok here is the story; I am still new at c++. I have only been taking the class for about a month. one assignment is make a program so its kinda like a vending machine for a deep fried twinkie (lol?), anyways if you see the code, i have a variable called money_pool which is used in a while loop, and different functions throughout the code. however, my professor said we can;t do global variables, so he recommended using call-by-reference (pointers) which he has barley started teaching us for about 5 minutes. So in other words, I don;t know about them yet and i'm trying to get my program to work with them. So far I messed up my code, like I said before it worked as global but since we can;t do that Iv been trying to figure out pointers in order to get it working, so I ask if someone can help me get this working. The problem is the variable money_pool....I hope....

I only ask if people dont suggest other ways to re arrange my functions of programs since they are ok, I only need to figure out how to get this to work like it used to

so please help me,


#include <cstdlib>
#include <iostream>
#include <string.h>
using namespace std;

void money_message();
double insert_coin(double coin, double&);
void intro_message();
void end_message();
string selections(string string_pool);
void wrong_input(double&);
void welcome_banner();
void while_loop(double&);


char response; // to restart program

const int zer0 = 0;
const double three_fifty = 3.50;



int main()
{
    double money_pool = 0; 
    
    cout.setf(ios::fixed); 
    cout.setf(ios::showpoint); 
    cout.precision(2);     
    
      do {
          
        intro_message();
        while_loop(money_pool);
        end_message();
        
      } while ((response == 'y') || (response== 'Y'));
      
    system("PAUSE");
    return 0;
}


void while_loop(double& money_pool)
{
      while (money_pool < three_fifty)
        {                
         money_message();
         }
}


//displays what user to input then puts it into a string that calls for selections function
void money_message()
{
        using namespace std;
        string string_pool;
        cout << "Type quarter for .25" << endl;
        cout << "Type nickle for .05" << endl;
        cout << "Type dime for .10" << endl;
        cout << "Type dollar for 1.00" << endl; 
        cout << endl;
        cin >> string_pool;
        selections(string_pool);
}

//function gets called and has different value for coin depending on users entry, if current value is 3.50 or above, gives back change if not display current money and remaining and continues to loop
double insert_coin(double coin, double& money_pool)
{   
    double change;
    money_pool = money_pool + coin;   
    system("CLS");  
    
        if (money_pool > three_fifty)
          {
                welcome_banner();
                change = money_pool - three_fifty;
                cout << "Your change is: " << change << endl;     
                cout << endl;
          }

        else
          {
    
    welcome_banner();
    cout << "You have inserted: " << money_pool << " So far" << endl;
    cout << "Remaining Balance: " << three_fifty - money_pool << endl;    
    cout << endl;
           }

return money_pool;    
}

//if user at anytime types in wrong char or string

void wrong_input(double& money_pool)
{ 
    using namespace std;
    system("CLS");       
    welcome_banner();    
    cout << "***********************************" << endl;
    cout << "Not a valid entry, Please try again" << endl;
    cout << "***********************************" << endl;
    cout << endl;      
    cout << "Remaining Balance for a Deep Fried Twinkie: " << three_fifty - money_pool << endl;    
    cout << endl;     
}

// display intro message
void intro_message()
{
    welcome_banner();  
    cout << "Want a deep fried twinkie?" << endl;
    cout << "Only $3.50" << endl;
    cout << endl;
}

// display after program ends   
void end_message()
{
    cout << "Enjoy your deep fried twinkie" << endl;
    cout << endl;
    cout << "Want another? (type y for yes, anything else quits)" << endl;  
    cin >> response; 
    system("CLS"); 
}     

// my banner that should always be on top of screen to look pretty =]
void welcome_banner()
{
    cout << "****************************" << endl;
    cout << "*    Welcome to Tanner's   *" << endl;
    cout << "*    Deep Fried Twinkie    *" << endl;
    cout << "*         Dispenser        *" << endl;
    cout << "****************************" << endl;
    cout << endl;
}

// selection function takes result from money_message function and then runs coin function depending on which  string, if none runs error function
string selections(string string_pool)
{
    using namespace std;
    double coin = zer0;
    const double dollar = 1.00;
    const double nickle = .05;
    const double dime = .10;
    const double quarter = .25;
    
     if ((string_pool == "quarter") || (string_pool == "nickle") || (string_pool == "dime") || (string_pool == "dollar"))
     {          
          if (string_pool == "quarter")
          {              
              coin = quarter;
              insert_coin(coin);
          }
          if (string_pool == "nickle")
          {
              coin = nickle;
              insert_coin(coin);
          }
          if (string_pool == "dime")
          {
              coin = dime;
              insert_coin(coin);
          }
          if (string_pool == "dollar")
          {
              coin = dollar;
              insert_coin(coin);
          }                      
      }
      else
      {
         // wrong_input(double&);
      }     
}





Quote:


C:\Users\rcc\Documents\Untitled1.cpp In function `std::string selections(std::string)':
69 C:\Users\rcc\Documents\Untitled1.cpp too few arguments to function `double insert_coin(double, double&)'
154 C:\Users\rcc\Documents\Untitled1.cpp at this point in file
69 C:\Users\rcc\Documents\Untitled1.cpp too few arguments to function `double insert_coin(double, double&)'
159 C:\Users\rcc\Documents\Untitled1.cpp at this point in file
69 C:\Users\rcc\Documents\Untitled1.cpp too few arguments to function `double insert_coin(double, double&)'
164 C:\Users\rcc\Documents\Untitled1.cpp at this point in file
69 C:\Users\rcc\Documents\Untitled1.cpp too few arguments to function `double insert_coin(double, double&)'
169 C:\Users\rcc\Documents\Untitled1.cpp at this point in file

Re: Need help with my C++ code [message #445207 is a reply to message #445206] Wed, 23 March 2011 13:16 Go to previous messageGo to next message
_SSnipe_ is currently offline  _SSnipe_
Messages: 4121
Registered: May 2007
Location: Riverside Southern Califo...
Karma: 0
General (4 Stars)
Nevermind guys, a professor came into the lab and sat down with me and helped me fix it as you can see here

#include <cstdlib>
#include <iostream>
#include <string.h>
using namespace std;

void money_message(double&);
double insert_coin(double coin, double&);
void intro_message();
void end_message(char&);
void selections(string string_pool, double&);
void wrong_input(double&);
void welcome_banner();
void while_loop();

int zer0 = 0;
const double three_fifty = 3.50;

int main()
{
    char response; // to restart program
    double money_pool; 
    cout.setf(ios::fixed); 
    cout.setf(ios::showpoint); 
    cout.precision(2);     
    
      do {
        money_pool = 0;   
        intro_message();
        
        while (money_pool < three_fifty)
        {     

            money_message(money_pool);

         }

       end_message(response);
        
      } while ((response == 'y') || (response== 'Y'));
      
    system("PAUSE");
    return 0;
}

/*
void while_loop()
{
      while (money_pool < three_fifty)
        {             
         money_message();
         }
}
*/

//displays what user to input then puts it into a string that calls for selections function
void money_message(double& money_pool)
{
        using namespace std;
        string string_pool;
        cout << "Type quarter for .25" << endl;
        cout << "Type nickle for .05" << endl;
        cout << "Type dime for .10" << endl;
        cout << "Type dollar for 1.00" << endl; 
        cout << endl;
        cin >> string_pool;
        selections(string_pool, money_pool);
}

//function gets called and has different value for coin depending on users entry, if current value is 3.50 or above, gives back change if not display current money and remaining and continues to loop
double insert_coin(double coin, double& money_pool)
{   
    
    double change;
    money_pool = money_pool + coin;   
    system("CLS");  
    
    if (money_pool > three_fifty)
      {
            welcome_banner();
            change = money_pool - three_fifty;
            cout << "Your change is: " << change << endl;     
            cout << endl;
      }

    else
      {

            welcome_banner();
            cout << "You have inserted: " << money_pool << " So far" << endl;
            cout << "Remaining Balance: " << three_fifty - money_pool << endl;    
            cout << endl;
       }

return money_pool;    
}

//if user at anytime types in wrong char or string

void wrong_input(double& money_pool)
{ 
    using namespace std;
    system("CLS");       
    welcome_banner();    
    cout << "***********************************" << endl;
    cout << "Not a valid entry, Please try again" << endl;
    cout << "***********************************" << endl;
    cout << endl;      
    cout << "Remaining Balance for a Deep Fried Twinkie: " << three_fifty - money_pool << endl;    
    cout << endl;     
}

// display intro message
void intro_message()
{
    welcome_banner();  
    cout << "Want a deep fried twinkie?" << endl;
    cout << "Only $3.50" << endl;
    cout << endl;
}

// display after program ends   
void end_message(char& response)
{
    cout << "Enjoy your deep fried twinkie" << endl;
    cout << endl;
    cout << "Want another? (type y for yes, anything else quits)" << endl;  
    cin >> response; 
    system("CLS"); 
}     

// my banner that should always be on top of screen to look pretty =]
void welcome_banner()
{
    cout << "****************************" << endl;
    cout << "*    Welcome to Tanner's   *" << endl;
    cout << "*    Deep Fried Twinkie    *" << endl;
    cout << "*         Dispenser        *" << endl;
    cout << "****************************" << endl;
    cout << endl;
}

// selection function takes result from money_message function and then runs coin function depending on which  string, if none runs error function
void selections(string string_pool, double& money_pool)
{
    using namespace std;
    double coin = zer0;
    const double dollar = 1.00;
    const double nickle = .05;
    const double dime = .10;
    const double quarter = .25;
    
     if ((string_pool == "quarter") || (string_pool == "nickle") || (string_pool == "dime") || (string_pool == "dollar"))
     {          
          if (string_pool == "quarter")
          {              
              coin = quarter;
              insert_coin(coin, money_pool);
          }
          if (string_pool == "nickle")
          {
              coin = nickle;
              insert_coin(coin, money_pool);
          }
          if (string_pool == "dime")
          {
              coin = dime;
              insert_coin(coin, money_pool);
          }
          if (string_pool == "dollar")
          {
              coin = dollar;
              insert_coin(coin, money_pool);
          }                      
      }
      else
      {
         wrong_input(money_pool);
      }     
}




anyways im happy
Re: Need help with my C++ code [message #445210 is a reply to message #445206] Wed, 23 March 2011 15:16 Go to previous messageGo to next message
Hypnos is currently offline  Hypnos
Messages: 682
Registered: August 2009
Location: Scotland
Karma: 0
Colonel
There's an "Other" section at the bottom where this should of been posted.

Glad you got it fixed though.


http://i33.tinypic.com/2ls7bzb.png

Caveman wrote on Fri, 21 January 2011 08:26

Well this topic is still going on. I have to say I haven't watched much Anime recently (maybe a year or so) the last thing I saw was GITS (for the third time)

Im not too sure whether I just dont enjoy Anime anymore or whether its just I dont have time really to shit and watch it.






Re: Need help with my C++ code [message #445226 is a reply to message #445210] Wed, 23 March 2011 21:03 Go to previous messageGo to next message
_SSnipe_ is currently offline  _SSnipe_
Messages: 4121
Registered: May 2007
Location: Riverside Southern Califo...
Karma: 0
General (4 Stars)
Hypnos wrote on Wed, 23 March 2011 15:16

There's an "Other" section at the bottom where this should of been posted.

Glad you got it fixed though.


Ya but honestly hardly anyone checks that section
Re: Need help with my C++ code [message #445227 is a reply to message #445206] Wed, 23 March 2011 21:25 Go to previous messageGo to next message
Dover is currently offline  Dover
Messages: 2547
Registered: March 2006
Location: Monterey, California
Karma: 0
General (2 Stars)
With good reason, because nobody wants to do your homework for you.

DarkDemin wrote on Thu, 03 August 2006 19:19

Remember kids the internet is serious business.
Re: Need help with my C++ code [message #445231 is a reply to message #445206] Thu, 24 March 2011 01:46 Go to previous messageGo to next message
reborn is currently offline  reborn
Messages: 3231
Registered: September 2004
Location: uk - london
Karma: 0
General (3 Stars)
You're still using global variables (although there seems little need, I believe you really want to use DEFINE instead), and there are functions which declare that you're using the std namespace, which is weird, because you've aleady added that at the top just under your includes.


Re: Need help with my C++ code [message #445233 is a reply to message #445206] Thu, 24 March 2011 01:49 Go to previous messageGo to next message
danpaul88 is currently offline  danpaul88
Messages: 5795
Registered: June 2004
Location: England
Karma: 0
General (5 Stars)
Curiously, your insert_coin function takes money_pool as a reference, but also returns it as the result of the function? If its passed as a reference there's no need to return it's value since any changes made to its value will persist after the function call.

http://steamsignature.com/card/1/76561197975867233.png
Re: Need help with my C++ code [message #445235 is a reply to message #445206] Thu, 24 March 2011 02:12 Go to previous messageGo to next message
reborn is currently offline  reborn
Messages: 3231
Registered: September 2004
Location: uk - london
Karma: 0
General (3 Stars)
Yeah, DP's right.

When you call the function you're not even asking for a return value:

          if (string_pool == "quarter")
          {              
              coin = quarter;
              insert_coin(coin, money_pool);
          }


If you was doing this:

          if (string_pool == "quarter")
          {              
              coin = quarter;
              double return = insert_coin(coin, money_pool);
              printf("%d\n", return); //or cout, whatever
          }


Then I might understand, but even then, you would still be able to do:

          if (string_pool == "quarter")
          {              
              coin = quarter;
              insert_coin(coin, money_pool);
              printf("%d\n", money_pool); //or cout, whatever

          }


I believe that is the point DP was trying to make to you.



Re: Need help with my C++ code [message #445259 is a reply to message #445206] Thu, 24 March 2011 20:31 Go to previous messageGo to next message
Sir Kane
Messages: 1701
Registered: March 2003
Location: Angerville
Karma: 0
General (1 Star)
using namespace std;

That's your problem right there!


Proud N9500 and proud N6270 user. Creator of the IEE libraries (original bhs.dll) and the RB series software.
http://n00bstories.com/image.fetch.php?id=1189992501http://www.n00bstories.com/image.fetch.php?id=1257492907
Re: Need help with my C++ code [message #445264 is a reply to message #445259] Thu, 24 March 2011 20:50 Go to previous messageGo to next message
_SSnipe_ is currently offline  _SSnipe_
Messages: 4121
Registered: May 2007
Location: Riverside Southern Califo...
Karma: 0
General (4 Stars)
Sir Kane wrote on Thu, 24 March 2011 20:31

using namespace std;

That's your problem right there!

You know I hear good things about this, but bad yet people like you say comments like that, whats wrong with it?
Re: Need help with my C++ code [message #445269 is a reply to message #445206] Fri, 25 March 2011 01:54 Go to previous messageGo to next message
reborn is currently offline  reborn
Messages: 3231
Registered: September 2004
Location: uk - london
Karma: 0
General (3 Stars)
The std namepsace has a lot of identifiers, and they are called generic things like string or iterator. If you use another library then there could be a chance that it has the same identifier. Even if the other library doesn't use the same identifier at the moment, it could do in the future, so there's a conflict in your global namespace.
Really you should just do std::cin instead of using the global namespace, it also adds clarity to code and makes it easier to read.

However, I get the impression from Silent Kane that he doesn't like the std namespace itself, rather than what I just posted.



Re: Need help with my C++ code [message #445272 is a reply to message #445206] Fri, 25 March 2011 08:16 Go to previous messageGo to next message
Sir Kane
Messages: 1701
Registered: March 2003
Location: Angerville
Karma: 0
General (1 Star)
There are nice functions like scanf and printf that don't suck dick like std:: does.

Proud N9500 and proud N6270 user. Creator of the IEE libraries (original bhs.dll) and the RB series software.
http://n00bstories.com/image.fetch.php?id=1189992501http://www.n00bstories.com/image.fetch.php?id=1257492907
Re: Need help with my C++ code [message #445275 is a reply to message #445206] Fri, 25 March 2011 09:42 Go to previous messageGo to next message
_SSnipe_ is currently offline  _SSnipe_
Messages: 4121
Registered: May 2007
Location: Riverside Southern Califo...
Karma: 0
General (4 Stars)
Thanks guys, but maybe you guys can clarify something for me.


I am still confused between the difference of a call-by-value

example, void something(int a, int b)

compared to a call-by-reference (&)

example, void something(int& a, int& b)


the book sucks at explaining it.

[Updated on: Fri, 25 March 2011 09:42]

Report message to a moderator

Re: Need help with my C++ code [message #445291 is a reply to message #445206] Fri, 25 March 2011 14:30 Go to previous messageGo to next message
Omar007 is currently offline  Omar007
Messages: 1711
Registered: December 2007
Location: Amsterdam
Karma: 0
General (1 Star)
I'm not a C++ master nor am I writing this down with any reference. This is from the top of my head so it may contain faulty information.

---
A call by value makes a duplicate and does not affect the original.
Call by reference modifies the original value you pass.

Call by value
void main()
{
    int a = 10;
    somefunction(a);
    cout << a; //10
}


void somefunction(int a)
{
    a = 20;
}


Call by reference
void main()
{
    int a = 10;
    somefunction(a);
    cout << a; //20
}


void somefunction(int& a)
{
    a = 20;
}


http://tiberiumredux.omarpakker.nl/Old Unused Parts/Plaatjes/PromoteBanner_Hades_small.jpg
Re: Need help with my C++ code [message #445308 is a reply to message #445291] Sat, 26 March 2011 11:32 Go to previous messageGo to next message
_SSnipe_ is currently offline  _SSnipe_
Messages: 4121
Registered: May 2007
Location: Riverside Southern Califo...
Karma: 0
General (4 Stars)
Omar007 wrote on Fri, 25 March 2011 14:30

I'm not a C++ master nor am I writing this down with any reference. This is from the top of my head so it may contain faulty information.

---
A call by value makes a duplicate and does not affect the original.
Call by reference modifies the original value you pass.

Call by value
void main()
{
    int a = 10;
    somefunction(a);
    cout << a; //10
}


void somefunction(int a)
{
    a = 20;
}


Call by reference
void main()
{
    int a = 10;
    somefunction(a);
    cout << a; //20
}


void somefunction(int& a)
{
    a = 20;
}




See, that makes much more scene, thanks

Another problem I am having is I am lost on the difference between a functions formal parameters and arguments
Re: Need help with my C++ code [message #445313 is a reply to message #445206] Sat, 26 March 2011 17:31 Go to previous message
Omar007 is currently offline  Omar007
Messages: 1711
Registered: December 2007
Location: Amsterdam
Karma: 0
General (1 Star)
AFAIK 'arguments' is the same as 'actual parameters' so I''l be using that in my story Wink

Formal parameters are the parameters you define in your function prototype or definition.
Actual parameters are the parameters you supply to the function.

For instance:
void someFunction(int a); //'a' is a formal parameter

void main()
{
    int a = 1;
    someFunction(a); //'a' is a actual parameter
}


When an actual parameter is of a lower type (eg the formal parameter is a long and the actual parameter an int) it will be automatically converted.
The other way around requires you to typecast Wink


http://tiberiumredux.omarpakker.nl/Old Unused Parts/Plaatjes/PromoteBanner_Hades_small.jpg

[Updated on: Sat, 26 March 2011 17:34]

Report message to a moderator

Previous Topic: need help with php
Next Topic: Is it possible to replace the CPU Fans on laptops?
Goto Forum:
  


Current Time: Thu Mar 28 16:13:18 MST 2024

Total time taken to generate the page: 0.01476 seconds