C++ 为什么我的程序要编译,但不能产生正确的输出?这些数字毫无意义

C++ 为什么我的程序要编译,但不能产生正确的输出?这些数字毫无意义,c++,C++,我已经为我的班级创建了这段代码,我已经在这段代码上工作了一个星期,但是没有工作。程序可以编译,但输出不正确。我相信我的参数是个问题,但我在这个论坛和另外两个论坛上研究了很多问题。被问的问题对我的处境没有帮助 */ #include <iostream> #include <cmath> using namespace std ; void Instructions () ; int numBooks (int) ; float totalCost (int, floa

我已经为我的班级创建了这段代码,我已经在这段代码上工作了一个星期,但是没有工作。程序可以编译,但输出不正确。我相信我的参数是个问题,但我在这个论坛和另外两个论坛上研究了很多问题。被问的问题对我的处境没有帮助

*/
#include <iostream>
#include <cmath>

using namespace std ;

void Instructions () ;
int numBooks (int) ;
float totalCost (int, float) ;
float percentoff (float, float) ;
float finalCost (float, float, float) ;
float receipt (int, float, float, float) ;

int main ()
{
    int books ;
    float total, cost, discount, novels ;
    Instructions () ;
    numBooks (books) ;
    totalCost (books, total) ;
    percentoff (total, discount) ;
    finalCost (discount, total, cost) ;
    receipt (books, total, discount, cost) ;    
}

void Instructions ()
{
    cout << "This program will ask for the number of books being purchased. It will then calculate your discount, and provide the total cost." << endl ;
}

int numBooks (int books)
{
    cout << endl << "Enter the number of books you wish to purchase." << endl ;
    cin >> books ;
    return books ;
}

float totalCost (int books, float total)
{
    float price = 8.99 ;
    total = books * price ;
    return (total) ;
}

float percentoff (float total, float discount)
{
    float percent = 0.15 ;
    discount = percent * total ;
    return (discount) ;
}

float finalCost (float discount, float total, float cost)
{
    cost = total - discount ;
    return (cost) ;
}

float receipt (int books, float total, float discount, float cost)
{
    cout << books << endl ;
    cout << "$" << total << endl ;
    cout << discount << "% off" << endl ;
    cout << "$" << cost << endl ;
}
*/
#包括
#包括
使用名称空间std;
无效指令();
int-numBooks(int);
浮动总成本(整数,浮动);
浮动百分比(浮动,浮动);
浮动最终成本(浮动,浮动,浮动);
浮动收据(整数、浮动、浮动、浮动);
int main()
{
国际图书;
浮动总额、成本、折扣、小说;
说明();
麻木书;
总成本(书籍,总成本);
百分比(总额、折扣);
最终成本(折扣、总额、成本);
收据(帐簿、总额、折扣、成本);
}
无效指令()
{

cout您的函数几乎都是通过copy传递的,您对返回的值不做任何操作,例如,此函数:

int numBooks (int books)
{
    cout << endl << "Enter the number of books you wish to purchase." << endl ;
    cin >> books ;
    return books ;
}
您可以改为传递引用,以便函数内的更改对作为参数传递的变量生效,例如:

int numBooks (int& books) {....
然后你的函数不再需要返回任何值,所以它们可以被重写为


void numBooks(int&books){..

您没有使用函数的返回值

内部
main

int main ()
{
    int books ;
    float total, cost, discount, novels ;
    Instructions () ;
    books = numBooks (books) ;
    total = totalCost (books, total) ;
    discount=percentoff (total, discount) ;
    cost = finalCost (discount, total, cost) ;
    receipt (books, total, discount, cost) ;    
}

函数返回一个主程序无法捕获的值


有些函数有返回类型,但不返回值。

“我认为我的参数是问题所在”
-你为什么相信?是什么调试让你得出了这个结论?首先,编辑问题并粘贴到你期望的输出和你在这里得到的输出中。你的函数参数是按值传递的。每个函数参数都是原始值的副本。分配给它不会改变原始值l传递给function.Debugger的变量。使用调试器。调试器允许您单步执行程序,观察变量值并探索执行路径。使用调试器通常比正确过帐到StackOverflow并等待代码检查或有人为您调试程序要快。建议这个解决方案意味着许多函数参数都是无用的。我觉得应该解决这个问题。为了补充这一点,如果你通过引用传递它,那么你就不需要这些函数的返回类型。“void”可以通过……很好的观察……感谢你的评论,可能值得编辑,以便向提问者添加一个礼貌的提醒,提醒他们y必须同时更改函数定义和原型。
int main ()
{
    int books ;
    float total, cost, discount, novels ;
    Instructions () ;
    books = numBooks (books) ;
    total = totalCost (books, total) ;
    discount=percentoff (total, discount) ;
    cost = finalCost (discount, total, cost) ;
    receipt (books, total, discount, cost) ;    
}