Get user input should be done in a function. The function should read the product number, price per unit, and quantity sold and return them to the main().
Display the value of product number, price per unit and quantyty sold in main().
int main()
{
getInfo(info); // what's info? Main doesn't know what that is
cout<<"DATA:"<<endl;
cout<<info.number<<info.price<<info.quantity;
return 0;
}
#include<iostream>
#include<string>
#include<iomanip>
usingnamespace std;
struct product
{
int number,quantity;
double price;
};
void getInfo(product info);//In function prototype, you don't need to specify
//variable identifiers.
int main()
{
getInfo(info);
cout<<"DATA:"<<endl;
cout<<info.number<<info.price<<info.quantity;
return 0;
}
//You can obtain the value of this modified object(info) in
//several ways.
//Pass argument by reference(assuming you know what a reference is)
//Return the instance of product.
void getInfo(product info)
{
cout<<"Please enter number:";
cin>>info.number;
cout<<"Please enter price:";
cin>>info.price;
cout<<"Please enter quantity:";
cin>>info.quantity;
}
//If you choose to return the instance of object,
//then simply object = getInfo(object).
//The reason why this works is because every structure
//comes with a predefined "copy constructor",
//which overloads the assignment operator('=').
//What it does is simply to copy every member's value of the
//right side of the expression, and copy them over to respective
//left side members.
//Personally though, i would have prefered to pass the object as reference in this case.
//If you don't know about references, look it up.