简体   繁体   中英

Hello, the program on Functions (pass by reference/pass by value) and I'm not entirely sure what is wrong with my program

Can someone give advice or guidance on what is wrong with my program? I do have the program fully written out, I am not sure where to add the function definition and function prototype to my program. This program involves pass by value and pass by reference. Also, any helpful notices on small errors will be appreciated

#include "pch.h"
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

int main()
{
    //Declare Variables
    double amount = 22.66;
    int num;
    int dollars;
    int quarters;
    int dimes;
    int nickels;
    int pennies;
    double x = 22.25;
    double y = 55.55;

    //Input
    cout << "Please enter your dollar amount: ";
    cin >> amount;


    void showMenu() //function prototype
    {
        cout << "A. Money Exchange function" << endl;
        cout << "B. Find Max Solution" << endl;
        cout << "E. Exit" << endl;
        cout <<"Please enter your choice:"<<endl;
    }


    void MoneyExchange(float amount, int& dollars, int& quarters, int& dimes, 
    int & nickels, int & pennies)   // function prototype
    {
        int amount = 0, Remainder;
        amount = amount * 100;
        dollars = amount / 100;
        Remainder = amount - (dollars * 100);
        quarters = Remainder / 25;
        Remainder = Remainder - (quarters * 25);
        dimes = Remainder / 10;
        Remainder = Remainder - (dimes * 10);
        nickels = Remainder / 5;
        Remainder = Remainder - (nickels * 5);
        pennies = Remainder / 1;
    }


    double max(double x, double y)  //function prototype
    {
        double max;
        if (x >= y)
            max = x;
        else
            max = y;

        system("Pause");
        return 0;
    }

To use a function, you need to have a function method declaration (tell compiler/linker that this function exists) and implementation (stuff that function method does). Here is a barebones example

void doStuff(); //im function declaration/prototype
void doMoreStuff(); //im function declaration/prototype



int main()
{
  void doMoreStuff() //dont nest me in here!
  {
     cout << "doMoreStufff runs" << endl; 
  }
  doStuff();
  doMoreStuff();
  return 1;
}
void doStuff() //im function implementation
{
   cout << "doStuff runs" << endl; 
}

Key take aways: 1) What you call a function prototype in your code is function implementation 2) No nesting implementation. for example: Dont do this

int main()
{
  void doMoreStuff() //dont nest me in here!
  {
     cout << "doMoreStufff runs" << endl; 
  }
  doStuff();
  doMoreStuff();
  return 1;
}
void doStuff() //im function implementation
{
   cout << "doStuff runs" << endl; 
}

(side note: one could nest anonymous/lambda functions inside)

3) In your case it doesnt matter if you stick method implementation above or below main{} implementation, just make sure you have your functions declared above main{} implementation (where these methods are used)

TLDR Poor naming conventions, not prototyping/declaring and defining properly, mismatched variable types, hiding value of amount (22.66, then cin, then int amount = 0 in MoneyExchange), unused code (max function), menu is not functional.

You are trying to define functions inside the main function like such:

int main() {
    // ...
    void showMenu() {
        // code to do stuff
    }
    // ...
    return 0;
}

It does not work like this. If you want to define and declare showMenu, so it can be used in main, try this. (See above)

void showMenu() {
    // code to do stuff
}

int main() {
    // ...
    showMenu();
    // ...
}

If you do not prototype the function, you must declare it above the main function. Otherwise it won't compile. (See above)

Try this (See below) if you want to prototype. You can have your prototypes in the main file or in a header file which you can include in main.

void showMenu(); // prototype

int main() {
    // ...
    showMenu();
    // ...
}

void showMenu() {
    // code to show the menu
}

If you want to define a function inside another, you should use a lambda expression.

Problems with your program:

-You define amount as a double of value 22.66 and then write over it when you cin >> amount. You only need to set the value once, so remove the cin or (preferably) change it to

double amount;

-Your functions and procedures, showMenu, MoneyExchange and max need to be moved outside of main, and prototyped or defined before main.

-You should find a naming convention which works for you and stick to it. You have procedure names starting with a capital MoneyExchange and then one starting with lower case, showMenu. Stick to the same, I'd use moneyExchange and showMenu. You have done the same thing with variables, have a look here https://code.tutsplus.com/articles/9-confusing-naming-conventions-for-beginners--net-15584 this explains some naming conventions.

-Max function does not return anything. It must return a double, as you've declared. EG

double max(double x, double y) {
    // ...

    return 0.0;
}

-In MoneyExchange you declare a new int called amount, which you have declared locally in main as a double. You also set the value to 0, not the amount you inputted using cin.

-When you declare MoneyExchange, amount is taken as a float. So you pass a double, receive it as a float and then make a new int called amount... Stick to one data type. You also don't pass it by reference.

-You don't use the max function anywhere in this code.

-You don't get input from the menu, so the user does not have an option. I would use a switch statement. See this link http://www.cplusplus.com/forum/general/156582/ .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM