C++ 在c+中调用函数时遇到困难+;

C++ 在c+中调用函数时遇到困难+;,c++,function,function-call,C++,Function,Function Call,我无法调用printTotal函数。它说我缺少标识符。read函数工作正常,但变量没有从一个函数转移到另一个函数。任务说我不能在另一个函数中调用一个函数。我只能从主函数中调用函数。我还没有完成printTotal函数的编程,但我只需要得到调用atm的函数。我无法让它们在不声明变量的情况下运行。我一定是错过了调用函数的一些基本功能 错误消息是“标识符”spool、“库存”、“装运”未定义 #include <iostream> #include <iomanip> usin

我无法调用printTotal函数。它说我缺少标识符。read函数工作正常,但变量没有从一个函数转移到另一个函数。任务说我不能在另一个函数中调用一个函数。我只能从主函数中调用函数。我还没有完成printTotal函数的编程,但我只需要得到调用atm的函数。我无法让它们在不声明变量的情况下运行。我一定是错过了调用函数的一些基本功能

错误消息是“标识符”spool、“库存”、“装运”未定义

#include <iostream>
#include <iomanip>
using namespace std;

double readSpools() {
  int spool, stock;
  double shipping = 12.99;
  char choice;
  cout << "Spools to be ordered\n";
  cin >> spool;
  while (spool <= 0) {
    cout << "Spools order must be 1 or more";
    return false;
  }

  cout << "Spools in stock\n";
  cin >> stock;
  while (stock <= 0) {
    cout << "Spools in stock must be 0 or more";
    return false;
  }

  cout << "Special shipping and handling (y or n)\n";
  cin >> choice;
  if (choice == 'y' || choice == 'Y') {
    cout << "Shipping and handling amount\n";
    cin >> shipping;
    if (shipping <= 0) {
      cout << "The spool shipping and handling charge must be 0.0 or more";
      return false;
    }
    return shipping, spool, stock;
  }
}

void printTotal(int spool, int stock, double shipping) {
  if (stock >= spool) {
    cout << "spools ready to ship: " << spool << endl;
    cout << "The subtotal is $" << spool * 100 << ".\n";
    cout << "The shipping and handling charges are $" << spool * shipping << ".\n";
    cout << "The order total is $" << spool * (100 + shipping) << ".\n";
  } else {
    cout << "Spools ready to ship: " << stock << endl;
    cout << "Spools on backorder: " << 0 - (stock - spool) << endl;
    cout << "The subtotal is $" << stock * 100 << ".\n";
    cout << "Total shipping and handling charges: $" << stock * shipping << ".\n";
    cout << "The order total is $" << stock * (100 + shipping) << ".\n";
  }

}

//the read function works, but the printTotal function will not call.
int main() {
  readSpools();
  printTotal(spool, stock, shipping);
  return 0;
}
#包括
#包括
使用名称空间std;
双读假脱机(){
国际线轴,库存;
双倍装运=12.99;
字符选择;
cout>spool;

while(spool我已经完成了家庭作业。事实证明,全局变量实际上不允许用于作业。我必须学习原型函数,以及如何正确使用它们。我理解全局变量如何在更大的代码中使事情变得复杂。很高兴我很早就学会了好习惯

/*
This is a program to calculate sells of spools of copper wiring for $104 each,
and displays the status of an order based on user input
*/

#include <iostream>
#include <iomanip>
using namespace std;

//prototype functions to avoid using global variables
void readSpools(int &spool, int &stock, double &shipping);
void printTotal(int spool, int stock, double shipping);

//main function
int main() {

    // declaration of variables for the program
    int spool, stock;

    double shipping = 12.99;
    
    //calls the functions for the program
    readSpools(spool, stock, shipping);

    printTotal(spool, stock, shipping);

}

//function to get user input for the program
void readSpools(int &spool, int &stock, double &shipping) {

    //declares variable for yes or no answer
    char choice;

    //prompts user to enter spools being ordered
    cout << "Spools to be ordered\n";
    cin >> spool;
    //ensures user must input is valid before proceeding
    if (spool <= 0) {
        cout << "Spools order must be 1 or more\n";
        std::exit(0);
    }

    //prompts user to enter spools in stock
    cout << "Spools in stock\n";
    cin >> stock;
    //ensures valid entry for stock amount before proceeding
    if (stock < 0) {
        cout << "Spools in stock must be 0 or more\n";
        std::exit(0);
    }

    //prompts user to input special S and H if applicable
    cout << "Special shipping and handling (y or n)\n";
    cin >> choice;
    //prompts user to choose y or n for special shipping
    if (choice == 'y' || choice == 'Y') {
        //if yes, user will input special S and H
        cout << "Shipping and handling amount\n";
        cin >> shipping;
        //ensures valid entry for special shipping and handling
        if (shipping < 0) {
            cout << "The spool shipping and handling charge must be 0.0 or more\n";
            std::exit(0);
        }
    }
}

//function to display output
void printTotal(int spool, int stock, double shipping) {

    //calculates backorder and ensures a negative number is not displayed
    int backorder= 0 - (stock-spool);
    if (backorder < 0) {
        backorder = 0;
    }

    //ensures that the amount of spools in stock is not ordered
    if (stock > spool) {
        stock = spool;
    }

    //calculation for subtotal
    double subtotal = (spool + stock - spool) * 104;

    //displays all outputs with calculations complete
        cout << "Spools ready to ship: " << stock << endl;
        cout << "Spools on back-order: " << right << backorder << endl;
        cout << "Subtotal ready to ship: $" << setw(10) << right << fixed << setprecision(2) << subtotal << "\n";
        cout << "Shipping and handling:  $" << setw(10) << right <<fixed << setprecision(2) << stock * shipping << "\n";
        cout << "Total shipping charges: $" << setw(10) << right <<fixed << setprecision(2) << stock * shipping + subtotal<< "\n";
    }

/*
这是一个计算铜线绕线轴每根售价104美元的程序,
并根据用户输入显示订单的状态
*/
#包括
#包括
使用名称空间std;
//避免使用全局变量的原型函数
作废读线轴(内部和线轴、内部和库存、双重和装运);
无效打印总计(整数线轴、整数库存、双重装运);
//主要功能
int main(){
//程序变量的声明
国际线轴,库存;
双倍装运=12.99;
//调用程序的函数
读取线轴(线轴、库存、装运);
printTotal(线轴、库存、装运);
}
//函数获取程序的用户输入
作废读线轴(内部和线轴、内部和库存、双重和装运){
//为“是”或“否”答案声明变量
字符选择;
//提示用户输入正在订购的假脱机
cout>spool;
//确保用户必须在继续之前输入有效
如果(短管运输);
//确保特殊运输和处理的有效入口
如果(装运<0){
(四通){
库存=线轴;
}
//小计计算
双倍小计=(短管+存货-短管)*104;
//显示计算完成后的所有输出

如果您有错误消息,请(始终)包括它们。另外,请更正代码的格式。
return shipping,spool,stock;
不从函数返回三个值,它就是不这样工作。还有许多其他因素使您看起来只是在猜测,而不是从一开始就费心去学习。请看一些退后一步,从头开始(不要跳过任何内容)。您只能返回1个变量,不能超过1个。其次,根据您放置方法的位置,如果您放置在主函数上方,则不需要函数原型,否则您将在下方声明它。您需要声明变量globally@TechGeek49这是个糟糕的主意。有更好的方法“返回”多个值,包括(但不限于)
std::tuple
,结构,使用引用参数。