开发者

C++ Function with one input and one output not getting input

its function subtotalspool. It says i cant use it as a function somewere inside int main and it says in function subtotalspool that the varriable spoolnumber wasnt declared. I put "int spoolnumber" in the function declaration for subtotalspool. is there something im missing? EDIT for code change

/**************************************************/
/* Author:     Samuel LaManna                     */
/* Course:     CSC 135 Lisa Frye                  */
/* Assignment: Program 2 Functions                */
/* Due Date:   10/11/2011                         */
/* Filename:   program2.cpp                       */
/* Purpose:    This progam will accept input and  */
/*             give user shipping and total cost  */
/*             for a shippment of spools of wire  */
/**************************************************/


#include <iostream>

using namespace std;

void instruct();     //Function Declaration for instruction function
int spoolnum();      //Function for number of spools input
int stotalspool(int spoolnumber); //Function to calculate the sub total 
float shippingcost();//Function to calculate shipping cost
void results();      //Function to display all of the costs and a final total


int main()
{
  int spoolnumber;     //Var for number of spools to be ordered
  int subtotalspool;   //Var for spool sub total

  instruct();          // Function call to print instructions to user

  spoolnumber = spoolnum(int spoolnumber );
  //Test output to make sure/show that input given in function is retained in int main
  cout << endl << "Value stored in variable spollnumber inside int main is: " <<  spoolnumber << endl;

  subtotalspool = stotalspool();

  cout<< endl << "Value stored in variable subtotalspool inside int main is: " << subtotalspool <&开发者_开发技巧lt; endl;

  return 0;
}



/********************************************/
// Name: instruct                            /
// Description: Print instructions to user   /
// Parameters: N/A                           /
// Reture Value: N/A                         /
/********************************************/

void instruct()
{
  cout << endl << "This program will calculate the shipping information " << endl
       << "for a batch of wire spools. " << endl << endl;

  return;
}

/********************************************/
// Name: spoolnum                            /
// Description: Ask for and get number of    /
// spools                                    /
// Parameters: N/A                           /
// Reture Value: spoolnum                    /
/********************************************/
int spoolnum()
{
  int spoolnum = 0;
  char type = 'n';

  do {
      cout << "Number of spools to be shipped: ";
      cin >> spoolnum;
      cout << endl;
      cout << spoolnum << " spool(s) of wire will be shipped" << endl;
      cout << "Is this correct? [y/n] ";
      cin >> type;
  } while (type != 'y');

  return spoolnum;
} 

/********************************************/
// Name: stotalspool                       /
// Description: Calculate the subtotal for   /
//the shipped spools                         /
// Parameters: N/A                           /
// Reture Value: stotalspool               /
/********************************************/
int stotalspool()
{
  int stotalspool;
  stotalspool = spoolnumber * 100;

  return stotalspool;
}


Your variable spoolnumber is only defined on the scope of the function main. You either need to call spoolnum() in your subtotalspool() function, or pass that variable into the function to operate on it appropriately.

EDIT To pass a variable to a function, you need to change its signature. For instance, int subtotalspool(int x) will give you a copy of the value of whatever you pass in. You would then call this function as follows sts = subtotalspool(spoolnumber); Then in your actual function code (if in your signature you used the variable name x) you should change all of your code in this function which uses spoolnumber variable and replace that with x


you declared:

int subtotalspool(int spoolnumber); //Function to calculate the sub total 

and you use it in:

 subtotalspool = subtotalspool(); 

but you use it without an int parameter: it makes NO sense.

moreover, you should always initialize your variables. int spoolnumber = 0; //Var for number of spools to be ordered int subtotalspool = 0;

and please avoid using the same name for diferrent things like in:

subtotalspool = subtotalspool(); // here you have an int that is also a fonction.

;-) have fun


You define subtotalspool() as taking an int but you don't pass it one. Did you mean:

subtotalspool_ = subtotalspool(spoolnumber);

And yes, a variable and a function with the same name is going to cause problems.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜