简体   繁体   English

您好,有关函数的程序(按引用传递/按值传递),我不完全确定我的程序出了什么问题

[英]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. 关键要点:1)在代码中称为function prototype的是函数实现2)没有嵌套实现。 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) (旁注:可以在其中嵌套匿名/ lambda函数)

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) 3)在您的情况下,将方法实现放在main{}实现之上还是之下都没关系,只需确保在main{}实现之上声明了您的函数(使用这些方法的地方)

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. TLDR不良的命名约定,未正确原型设计/声明和定义,变量类型不匹配,金额隐藏值(MoneyExchange中为22.66,然后是cin,然后是int数量= 0),未使用的代码(max函数),菜单不起作用。

You are trying to define functions inside the main function like such: 您试图像这样在main函数中定义函数:

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. 如果要定义并声明showMenu,以便可以在main中使用它,请尝试此操作。 (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. 如果您没有对该函数进行原型设计,则必须在main函数之上声明它。 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. 如果要在另一个函数内部定义函数,则应使用lambda表达式。

Problems with your program: 您的程序存在问题:

-You define amount as a double of value 22.66 and then write over it when you cin >> amount. -您将数量定义为值22.66的两倍,然后在输入>> >>时将其覆盖。 You only need to set the value once, so remove the cin or (preferably) change it to 您只需要设置一次该值,因此删除cin或(最好)将其更改为

double amount;

-Your functions and procedures, showMenu, MoneyExchange and max need to be moved outside of main, and prototyped or defined before main. -您的函数和过程showMenu,MoneyExchange和max需要移到main之外,并在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. 您的过程名称以大写的MoneyExchange开头,然后以小写的showMenu开头。 Stick to the same, I'd use moneyExchange and showMenu. 坚持一样,我会使用moneyExchange和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. 您已经对变量做了同样的事情,在这里看看https://code.tutsplus.com/articles/9-confusing-naming-conventions-for-beginners--net-15584这解释了一些命名约定。

-Max function does not return anything. -Max函数不返回任何内容。 It must return a double, as you've declared. 如您所声明的,它必须返回一个double。 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. -在MoneyExchange中,您声明一个新的int称为amount,您在main中本地将其声明为double。 You also set the value to 0, not the amount you inputted using cin. 您还将值设置为0,而不是使用cin输入的值。

-When you declare MoneyExchange, amount is taken as a float. -当您声明MoneyExchange时,金额被视为浮点数。 So you pass a double, receive it as a float and then make a new int called amount... Stick to one data type. 因此,您传递了一个double值,将其作为浮点数接收,然后创建一个名为count的新int ...坚持一种数据类型。 You also don't pass it by reference. 您也不会通过引用传递它。

-You don't use the max function anywhere in this code. -在此代码中的任何地方都不要使用max函数。

-You don't get input from the menu, so the user does not have an option. -您不会从菜单中获得输入,因此用户没有选择权。 I would use a switch statement. 我会使用switch语句。 See this link http://www.cplusplus.com/forum/general/156582/ . 请参阅此链接http://www.cplusplus.com/forum/general/156582/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用按引用传递和按值传递参数的故障执行程序 - Trouble executing program that uses both pass by reference and pass by value parameter C ++通过引用程序 - C++ Pass by Reference Program 我正在尝试使用 std::signal 干净地结束我的多线程程序,我做错了什么? - I'm trying to used std::signal to cleanly end my multithreaded program, what am I doing wrong? 我不明白我的程序出了什么问题 - i dont understand what is wrong in my program (C ++)试图完成一个快速程序,但是我不确定哪里出错了? - (C++) Trying to finish up a quick program but I'm not sure where I went wrong? 我收到警告,阻止了我运行程序,我不确定如何修复它 - I'm getting a warning that is preventing me from running my program and I'm not sure how to fix it 我的线程程序有什么问题? - What is wrong with my thread program? 引用传递和值传递会导致C++中程序的时间复杂度发生变化吗? - Does pass by reference and pass by value cause a change in time complexity of a program in C++? 我的程序没有错误就停止了,我做错了什么? - My program stops with no errror, what did I do wrong? 想知道我的程序做错了什么[暂停] - Wondering what I did wrong with my program [on hold]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM