简体   繁体   English

具有一输入一输出未获得输入的C ++函数

[英]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. 它说我不能将它用作int main内部的某个函数,并且在函数subtotalspool中说未声明可变阀芯编号。 I put "int spoolnumber" in the function declaration for subtotalspool. 我将“ int spoolnumber”放在小计的函数声明中。 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 << 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;
}

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. 但您不带int参数使用它:没有任何意义。

moreover, you should always initialize your variables. 此外,您应该始终初始化变量。 int spoolnumber = 0; int spoolnumber = 0; //Var for number of spools to be ordered int subtotalspool = 0; //将要订购的线轴数量转换为整数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 ;-) 玩得开心

Your variable spoolnumber is only defined on the scope of the function main. 变量spoolnumber仅在函数main的范围内定义。 You either need to call spoolnum() in your subtotalspool() function, or pass that variable into the function to operate on it appropriately. 您要么需要在subtotalspool()函数中调用spoolnum() ,要么将该变量传递给函数以对其进行适当的操作。

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); 例如, int subtotalspool(int x)将为您提供传入值的副本。然后,您将按以下方式调用此函数: 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 然后在实际的函数代码中(如果在签名中使用了变量名x ),则应更改此函数中使用spoolnumber变量的所有代码,并将其替换为x

You define subtotalspool() as taking an int but you don't pass it one. 您将subtotalspool()定义为采用一个int,但未将其传递给它。 Did you mean: 你的意思是:

subtotalspool_ = subtotalspool(spoolnumber);

And yes, a variable and a function with the same name is going to cause problems. 是的,具有相同名称的变量和函数将引起问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM