嘿,我是一个初学者,对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;
}
答案 0 :(得分: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();