-1

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;
}
beavercat
  • 1
  • 1
  • 2
    If you have error messages, then please (always) include them. Also, please correct the formatting of your code. – jarmod Oct 26 '20 at 23:14
  • 1
    `return shipping, spool, stock;` doesn't return three values from the function, it just doesn't work that way. There's also many other things that makes it seems like you're just guessing about things, not actually having bothered to learn from the start. Please take a few steps back, get [some good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282), and start over from the very beginning (without skipping anything). – Some programmer dude Oct 26 '20 at 23:22
  • You can only return 1 variable and not more than that. Secondly, depending on where you place the method, if you place above the main function, you don't need a function prototype lest u declared it below. – TechGeek49 Oct 26 '20 at 23:28
  • You need to declare variables globally – TechGeek49 Oct 26 '20 at 23:30
  • I did guess on the return for the read function. TechGeek49 may have turned me onto my problem. last several assignments disallowed global variables. I think I am allowed to use them on this one. Thank you for all the input. – beavercat Oct 26 '20 at 23:35
  • 1
    @TechGeek49 Which is a terrible idea. There's better ways of "returning" multiple values, including (but not limited to) `std::tuple`, structures, using reference arguments. – Some programmer dude Oct 26 '20 at 23:35
  • The big reason why global variables are frowned upon: Any function can change any global variable at any time, so you no longer have a simple and easy audit trail of who is updating what data and when. With global variables in play you always have to keep an eye out for unadvertised side effects, and this can greatly complicate debugging. – user4581301 Oct 26 '20 at 23:51
  • @Someprogrammerdude I think the `tuple` function is more or less a Python identifier and I don't really use that often. I stick with the fundamentals of it and yeah. Regarding global variables, I'm not trying to say he must use that. I don't recommend it much unless if he wants to – TechGeek49 Oct 27 '20 at 00:12

1 Answers1

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

beavercat
  • 1
  • 1