gpt4 book ai didi

c++ - 如何从另一个函数读取函数中的输入值?

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:40:32 25 4
gpt4 key购买 nike

嘿,我是初学者,对 C++ 知之甚少。我试图在函数的帮助下开发一个库存系统,但我似乎无法在另一个函数中使用我在一个函数中输入的输入。

可以做到吗?如果可以,怎么做?代码如下。还没有完成,我只是想展示我创建的所有功能。

这里我想在void displayData()中显示void input()的输入

    #include <iostream>
#include <string>
using namespace std;

void introDisplay (char&n)
{

cout << " ______MOON'S
INVENTORY SYSTEM_____" << endl;
cout << "*To insert data press 'i'" << endl;
cout << "*To delete data press 'd'" << endl;
cout << "*To edit data press 'e'" << endl;
cout << "*To update data press 'u'" << endl;
cout << "*To display data of all fields press 'v'" << endl;
cout << "*To display all the menu press 'm'" << endl;

cin >> n;

}

void menu ()

{

cout << "Data Fields are: " << endl;
cout << "1. Item id" << endl;
cout << "2. Item name" << endl;
cout << "3. Item description " << endl;
cout << "4. Manufacterer" << endl;
cout << "5. Selling Price" << endl;
cout << "6. Cost Price" << endl;
cout << "7. Units in store" << endl;
cout << "8. Units sold" << endl;
cout << "9. Year of date first introduced" << endl;
cout << "10. Month of date first introduced" << endl;
cout << "11. Day of date first introduced" << endl;

}
void input ()
{
double itemid;
cout << "Enter the item ID:" << endl;
cin >> itemid;
while (itemid<0)
{
cout << "Invalid Item ID. Please type again." << endl;
cin >> itemid;
}
cout << "Item ID: " << itemid << endl;

string itemname;
cout << "Type the name of the Item here :" << endl;
cin. ignore();
getline (cin,itemname);
cout << "Item Name: " << itemname << endl;

string itemdes;
cout << "What's the description of the item? " << endl;
cin. clear();
getline (cin,itemdes);
cout << "Item Description: " << itemdes << endl;

string cat;
cout << "What's the category of the product?" << endl;
cin. clear();
getline (cin,cat);
cout << "Category: " << cat << endl;

string manufacturer;
cout << "Who is the manufacturer?" << endl;
cin. clear();
getline (cin, manufacturer);
cout << "Manufacturer: "<< manufacturer << endl;

double sp;
cout << "Input the selling price of the item: " << endl;
cin >> sp;
while (sp<0)
{
cout << "Invalid Number. Please type again." << endl;
cin >> sp;
}
cout << "Selling Price: " << sp << endl;

double cp;
cout << "Input the cost price of the item: " << endl;
cin >> cp;
while (cp<0)
{
cout << "Invalid Number. Please type again." << endl;
cin >> cp;
}
cout << "Cost Price: " << cp << endl;

double units;
cout << "Input the number of units left in store: " << endl;
cin >> units;
while (units<0)
{
cout << "Invalid Number. Please type again." << endl;
cin >> units;
}
cout << "Units in store: " << units << endl;

double unitssold;
cout << "Input the number of units sold: " << endl;
cin >> unitssold;
while (units<0)
{
cout << "Invalid Number. Please type again." << endl;
cin >> unitssold;
}
cout << "Units Sold: " << unitssold << endl;

int year;
cout << "Input the year of date first introduced: " << endl;
cin >> year;
while (year<0)
{
cout << "Invalid Number. Please type again." << endl;
cin >> year;
}
cout << "Year of date first introduced: " << year << endl;

int month;
cout << "Input the month of date first introduced: " << endl;
cin >> month;
while (month<0 || month>12)
{
cout << "Invalid Number. PLease type again" << endl;
cin >> month;
}
cout << "Month of date first introduced: " << month << endl;

int day;
cout << "Input the day of the date first introduced: " << endl;
cin >> day;
while (day<0 || day>31)
{
cout << "Invalid Number. Please type again." << endl;
cin >> day;
}
cout << "Day of date first introduced: " << day << endl;


}
void displayData()
{
cout << "Item ID: " << itemid << endl;
cout << "Item Name: " << itemname << endl;
cout << "Item Description: " << itemdes << endl;
cout << "Item Category: " << cat << endl;
cout << "Item Manufacturer: " << manufacturer << endl;
cout << "Selling Price: " << sp << endl;
cout << "Cost Price: " << cp << endl;
cout << "Units in Store: " << units << endl;
cout << "Units Sold: " << unitssold << endl;
cout << "Year of date first introduced: " << endl;
cout << "Month of date first introduced: " << endl;
cout << "Day of date first introduced: " << endl;

}

int main()
{
char n;
introDisplay(n);

switch (n)
{
case 'i':
input();
break;

case 'm':
menu();
break;

case 'v':
displayData();

default :
cout << "Please enter a valid character to continue. Note that the
characters are case sensitive." << endl;
}
system("pause");
introDisplay(n);
return 0;
}

最佳答案

Function 中声明变量的范围仅限于该函数。因此,要么全局声明它们,要么使用 class包含函数和所有变量,类中的函数可以访问变量,但其他函数不能。

class class_name{

int year,month,day;
double itemid,sp,cp,units,unitssold;
string itemname,itemdes,cat,manufacturer;

public:

void input();
void displayData();

} object_name;

void class_name::input (){ //input method here }
void class_name::displayData(){//displayData method here}

在 main() 中你必须这样调用函数

     object_names.input();
object_names.displayData();

关于c++ - 如何从另一个函数读取函数中的输入值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51911414/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com