c++ - 如何从另一个函数中读取函数中的输入值?
问题描述
嘿,我是初学者,对 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_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();
推荐阅读
- angular - 在没有父子关系的情况下将数据从一个组件传输到另一个组件
- c# - 显示包含 SQL 数据库外部项目的组合框
- javascript - 创建与 Sequelize 和 Migrations 的关联时,别名是绝对名称还是更改为 AliasId?
- php - WordPress:使用 get_pages() 排除父页面
- python - 从 keras 升级到 tf.keras 时 DCGAN 性能不佳
- visual-studio-code - vscode 安装程序下载需要很长时间
- commonjs - 汇总 commonJs 和 nodeResolve MagicString 错误
- dialogflow-es - Dialogflow:移动到特定意图
- java - 在 SQLite 数据库中搜索特定字符串 - Android SDK
- pdf - PAdES 的最低 PDF 版本是多少?