简体   繁体   中英

I am having difficulty calling functions in c++

I cannot get my printTotal function to call. It says I am missing identifiers. The read function works fine, but the variables aren't transferring from one function to the other. The assignment says I cannot call one function inside the other. I can only call functions from within the main function. I am not finished programming the printTotal function, but I only need to get the function to call atm. I cannot get them to operate without declaring variables. I must be missing something fundamental to calling functions.

The error message is saying "Identifier "spool", "stock", "shipping" is undefined.

#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;
}

I have completed the homework assignment. It turned out that global variables are actually not allowed for the assignment. I had to learn about prototype functions, and how to use them properly. I understand how a global variable could make things complicated in a larger code. Glad I am learning good habits early on.

/*
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";
    }

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