c++ - 这个函数期望返回什么?

标签 c++

程序指定以下内容:

  • 编写一个程序,将总找零金额作为整数输入,使用最少的硬币输出找零,每行一种硬币类型。
  • 硬币类型有美元、四分之一、一角硬币、五分硬币和便士。
  • 根据需要使用单数和复数硬币名称,例如 1 penny 与 2 penny。
  • 您的程序必须定义并调用以下函数。
    void ExactChange(int userTotal, vector& coinVals)
  • coinVals 的位置 0-4 应分别包含美元、四分之一、一角硬币、五分硬币和便士的数量。

  • 我的代码如下:
    #include <iostream>
    #include <vector>
    #include <sstream>
    using namespace std;
    /*
     1) Write a program with total change amount as an integer input that outputs the change using the fewest coins, one coin type per line.
     2) The coin types are dollars, quarters, dimes, nickels, and pennies.
     3) Use singular and plural coin names as appropriate, like 1 penny vs. 2 pennies.
     
     4) Your program must define and call the following function.
                void ExactChange(int userTotal, vector<int>& coinVals)
     
     5) Positions 0-4 of coinVals should contain the number of dollars, quarters, dimes, nickels, and pennies, respectively.
     */
    void ExactChange(int userTotal, vector<int>& coinVals);
    const int PENNIES_IN_DOLLAR = 100, PENNIES_IN_QUARTER = 25, PENNIES_IN_DIME = 10, PENNIES_IN_NICKEL = 5;
    
    int main() {
       int userTotal, changeRemaining;
       cin >> userTotal;
        changeRemaining = userTotal;
        
        int dollars = changeRemaining / PENNIES_IN_DOLLAR;
        changeRemaining = changeRemaining % PENNIES_IN_DOLLAR;
        int quarters = changeRemaining / PENNIES_IN_QUARTER;
        changeRemaining = changeRemaining % PENNIES_IN_QUARTER;
        int dimes = changeRemaining / PENNIES_IN_DIME;
        changeRemaining = changeRemaining % PENNIES_IN_DIME;
        int nickels = changeRemaining / PENNIES_IN_NICKEL;
        changeRemaining = changeRemaining % PENNIES_IN_NICKEL;
        int pennies = changeRemaining;
        
        vector<int> changeAmount;
        
        vector<int> coinVals{dollars, quarters, dimes, nickels, pennies};
        changeAmount = coinVals;
        ExactChange(userTotal, changeAmount);
        
      return 0;
    }
    void ExactChange(int userTotal, vector<int>& coinVals) {
        
        if (userTotal == 0) {
           cout << "no change" << endl;
        }
          if(coinVals.at(0) > 0) {
           cout << coinVals.at(0);
           if(coinVals.at(0) > 1) {
               cout << " dollars" << endl;
           }else {
               cout << " dollar" << endl;
           }
        }
        if(coinVals.at(1) > 0) {
           cout << coinVals.at(1);
           if(coinVals.at(1) > 1) {
               cout << " quarters" << endl;
           } else {
               cout << " quarter" << endl;
           }
        }
        if(coinVals.at(2) > 0) {
           cout << coinVals.at(2);
           if(coinVals.at(2) > 1) {
               cout << " dimes" << endl;
           }else {
               cout << " dime" << endl;
           }
        }
        if(coinVals.at(3) > 0) {
           cout << coinVals.at(3);
           if(coinVals.at(3) > 1) {
               cout << " nickels" << endl;
           }else {
               cout << " nickel" << endl;
           }
        }
        if(coinVals.at(4) > 0) {
           cout << coinVals.at(4);
           if(coinVals.at(4) > 1) {
               cout << " pennies" << endl;
           }else {
               cout << " penny" << endl;
           }
        }
    }
    
    
    然而,zybooks,我们的大学类(class)进行实验室的网站给了我这些消息,表明我的代码存在一些问题:
    enter image description here
    我的问题是,这些“消息”是什么意思?我怎么能解决这些问题? 在我看来,他们是说在给定某个输入的情况下,该函数正在输出错误的内容,但是,他们也没有给我一个正确的输出来进行比较。

    最佳答案

    您的代码正在接受用户输入,直接在 main() 中手动转换调用前 ExactChange() ,然后将该转换的结果传递给 ExactChange()使其按原样显示。
    我阅读说明的方式,以及屏幕截图显示正在执行的测试的方式,更有可能是 ExactChange()预计将接受用户输入并将其转换为 vector硬币数量作为输出。
    这与 ExactChange() 的事实是一致的。需要一个 vector来自 非常量引用 ,即可以自由修改vector的内容.如 ExactChange()用于文本输出,采用 vector 会更有意义。来自 常量引用 相反,它不能修改 vector ,只能查看。
    如果是这样,那么您的程序将通过为它提供用户定义输入并查找特定文本结果的测试是有道理的,但您的程序将无法通过执行 ExactChange() 的测试。直接使用特定输入并查找特定 vector输出。这就是单元测试的重点 - 直接测试预期行为的功能,而不是测试整个程序。
    您的代码可能需要看起来更像以下内容:

    #include <iostream>
    #include <vector>
    #include <sstream>
    using namespace std;
    
    /*
     1) Write a program with total change amount as an integer input that outputs the change using the fewest coins, one coin type per line.
     2) The coin types are dollars, quarters, dimes, nickels, and pennies.
     3) Use singular and plural coin names as appropriate, like 1 penny vs. 2 pennies.
     
     4) Your program must define and call the following function.
                void ExactChange(int userTotal, vector<int>& coinVals)
     
     5) Positions 0-4 of coinVals should contain the number of dollars, quarters, dimes, nickels, and pennies, respectively.
     */
    
    void ExactChange(int userTotal, vector<int>& coinVals);
    const int PENNIES_IN_DOLLAR = 100, PENNIES_IN_QUARTER = 25, PENNIES_IN_DIME = 10, PENNIES_IN_NICKEL = 5;
    
    int main() {
       int userTotal;
       cin >> userTotal;
            
       if (userTotal == 0) {
          cout << "no change" << endl;
       }
       else {
          vector<int> coinVals;
          ExactChange(userTotal, coinVals);
    
          if (coinVals[0] > 0) {
             cout << coinVals[0];
             if (coinVals[0] > 1) {
                cout << " dollars" << endl;
             } else {
                cout << " dollar" << endl;
             }
          }
    
          if (coinVals[1] > 0) {
             cout << coinVals[1];
             if (coinVals[1] > 1) {
                cout << " quarters" << endl;
             } else {
                cout << " quarter" << endl;
             }
          }
    
          if (coinVals.at(2) > 0) {
             cout << coinVals[2];
             if (coinVals[2] > 1) {
                cout << " dimes" << endl;
             }else {
                cout << " dime" << endl;
             }
          }
    
          if (coinVals[3] > 0) {
             cout << coinVals[3];
             if (coinVals[3] > 1) {
                cout << " nickels" << endl;
             }else {
                cout << " nickel" << endl;
             }
          }
    
          if (coinVals[4] > 0) {
             cout << coinVals[4];
             if (coinVals[4] > 1) {
                cout << " pennies" << endl;
             }else {
                cout << " penny" << endl;
             }
          }
       }
    
       return 0;
    }
    
    void ExactChange(int userTotal, vector<int>& coinVals) {    
        int dollars = userTotal / PENNIES_IN_DOLLAR;
        userTotal %= PENNIES_IN_DOLLAR;
        int quarters = userTotal / PENNIES_IN_QUARTER;
        userTotal %= PENNIES_IN_QUARTER;
        int dimes = userTotal / PENNIES_IN_DIME;
        userTotal %= PENNIES_IN_DIME;
        int nickels = userTotal / PENNIES_IN_NICKEL;
        userTotal %= PENNIES_IN_NICKEL;
        int pennies = userTotal;
        
        coinVals.resize(5);
        coinVals[0] = dollars;
        coinVals[1] = quarters;
        coinVals[2] = dimes;
        coinVals[3] = nickels;
        coinVals[4] = pennies;
    }
    

    关于c++ - 这个函数期望返回什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63081625/

    相关文章:

    c++ - 一台 Linux 机器上的段错误,但另一台使用 C++ 代码的机器上没有

    c++ - 为什么 GetErrorMessage 返回 "wrong password",当用户名错误时?

    c++ - 重用可变类型

    c++ - 你如何找到四个顶点之间的点的 Y 位置? HLSL

    c++ - std::map 和函数指针作为具有不同签名的值

    C++显式整数用户自定义转换

    c++ - Mac OS X Lion 上的 GNU GCC 4.6(.1) 编译器

    c++ - 在arduino上编程atmega328p的UART

    c++ - 使 QT Widgets 半透明

    c++ - 设计问题——静态变量继承