I am having some problems and I would appreciate some help if someone could tell me where it is that I am going wrong. I am trying to create a simple class that can hold the name of a product, product number, and price but if I take a space in the naming of product then it is jumping right to the end of the program. Below is the code for the class:
1 2 3 4 5 6 7 8 9 10 11
class product
{
char product_name[30];
int product_code;
float unit_price;
public:
void getproducts();
void displayproducts();
};
under your includes type "using namespace std;" That way all the cin's you are using now will work fine. Otherwise you will need to type your cin's like "std::cin >> product_name;"
Hi there and thank you both for you replies, I have tried both solutions but am still experiencing difficulties with both, if I use cin.getline(product_name,30) it is allowing the entry of the first product name but is skipping straight to the product code without allowing the second product name to be entered.
If i change my code to that below then I am getting one error which is "namespace name expected.
Using both cin.getline() and cin>> in the same code often gives problems, in this case it causes that the user cant put in the name of the second product. The easiest way to solve this is using cin.getline() for the other values to, but that way you cant pass the input directly to an integer variable. You will have to use a construction like this one:
1 2 3 4 5 6 7 8 9 10 11 12
int getint(int min,int max)
{
int input;
char temp[100];
while (input<min||input>max||(input==0 && temp[0]!='0'))
{
std::cin.getline(temp, 100);
input=strtol(temp,0,10);
if (input<min||input>max||(input==0 && temp[0]!='0'))
std::cout<<"Not an option!\n";
}
}