C ++函数,一个输入,一个输出没有输入

时间:2011-10-11 15:37:28

标签: c++

其功能小计。它说我不能使用它作为一个函数,有些内部在int main,它在函数subtotalspool中说,varriable spoolnumber没有声明。我把“int spoolnumber”放在了subtotalspool的函数声明中。有什么东西我不见了? 编辑代码更改

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

3 个答案:

答案 0 :(得分:0)

你宣布:

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

你可以在:

中使用它
 subtotalspool = subtotalspool(); 

但你在没有int参数的情况下使用它:它没有任何意义。

此外,您应始终初始化您的变量。   int spoolnumber = 0; // Var用于订购的线轴数量   int subtotalspool = 0;

请避免使用相同的名称来表示不同的内容,例如:

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

;-)玩得开心

答案 1 :(得分:0)

您的变量spoolnumber仅在函数main的范围内定义。您需要在spoolnum()函数中调用subtotalspool(),或将该变量传递给函数以对其进行适当的操作。

修改 要将变量传递给函数,需要更改其签名。例如,int subtotalspool(int x)将为您提供传入的任何值的副本。然后您将调用此函数,如下sts = subtotalspool(spoolnumber);然后在您的实际功能代码中(如果在您的签名中使用了变量)名称x)您应该更改此函数中使用spoolnumber变量的所有代码,并将其替换为x

答案 2 :(得分:0)

您将subtotalspool()定义为接受int但不传递一个。你的意思是:

subtotalspool_ = subtotalspool(spoolnumber);

是的,变量和具有相同名称的函数会导致问题。