Product class - whitespace in first input causing problems

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();
};


This is the main test:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

#include <stdio.h>
#include <iostream.h>
#include <conio.h>
#include <math.h>
#include "product.h"

void product::getproducts(void)
{
  	cout << "Enter Product Name : ";
  	cin >> product_name;
  	cout << "Enter Product Code : ";
  	cin >> product_code;
	cout << "Enter Unit Price : ";
  	cin >> unit_price;
}

void product::displayproducts(void)
{
  	cout << "Product Name : " << product_name;
  	cout << "\nProduct Price : " << product_code;
  	cout << "\nUnit Price : " << unit_price;
}

main()
{
    product P[3];  //object of type person

    for(int i=0;i<3;i++)
    {
    	cout << "\nEnter Product Information" << (i+1);
      cout << "\n--------------------------\n";
    	P[i].getproducts();
    }

   cout<<"\n";
   for(int i=0;i<3;i++)
   {
    	cout << "\nProduct P" << (i+1);
      cout << "\n---------\n";
    	P[i].displayproducts();
   }

   cout<<"\n";
   getche();
}


Any ideas of what I could do so that a whitespace would be tolerated in the character array?
Use cin.getline (product_name, 30) instead of cin>>product_name, or use type string
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107

#include <stdio.h>
#include <iostream.h>
#include <conio.h>
#include <math.h>
#include <time.h>
#include "product.h"
#include "order.h"
using namespace std;


void product::getproducts(void)
{
  	cout << "Enter Product Name : ";
  	cin >> product_name;
  	cout << "Enter Product Code : ";
  	cin >> product_code;
	cout << "Enter Unit Price : ";
  	cin >> unit_price;
}

float product::getprice(void)
{
	return unit_price;
}

void product::displayproducts(void)
{
  	cout << "Product Name : " << product_name;
  	cout << "\nProduct Code : " << product_code;
  	cout << "\nUnit Price : " << unit_price;
}

void order::getorder(void)
{
 	cout << "Enter Order No : ";
   cin >> order_no;
   cout << "Enter Product Code : ";
   cin >> product_code;
   cout << "Enter Quantity : ";
   cin >> quantity;
}

float order::totalprice(float a, int b)
{
        return a*b;
}

void order::invoice()
{
    char dateStr [9];
    cout << "\n";
    line();
    cout << "\n|" << " ABC Company Invoice       Date: " << _strdate(dateStr) << " |\n";
    line();
    cout << "\n|" << " Order No                  |      " << order_no << "       |" ;
    cout << "\n|" << " Product code              |   " << product_code << "       |" ;
    cout << "\n|" << " Quantity " << quantity << "  @ Unit Price  |         ***  |\n";
    line();
    cout << "\n|" << " Total price               |         ***  |\n";
    line();
}

void order::line(void)
{
 	for(int x=0;x<44;x++)
   	cout << "-";
}

main()
{
	product P[3];  //object of type product
   order O[3]; // object of type order

	for(int i=0;i<3;i++)
    {
    	cout << "\nEnter Product " << (i+1) << " Information";
      cout << "\n--------------------------\n";
    	P[i].getproducts();
    }

   cout<<"\n";
	for(int i=0;i<3;i++)
   {
    	cout << "\n\nProduct P" << (i+1);
      cout << "\n---------\n";
    	P[i].displayproducts();
   }

   cout<<"\n";
   for(int i=0;i<3;i++)
    {
    	cout << "\nEnter Order Information " << (i+1);
      cout << "\n------------------------\n";
    	O[i].getorder();
    }

   cout<<"\n";
   for(int i=0;i<3;i++)
   {
    	cout << "\n\nOrder No: " << (i+1);
      cout << "\n---------\n";
    	O[i].invoice();
   }
 	getche();
}
#include <iostream>

not

#include <iostream.h>

Then your namespace compilation error will go away.

Also

#include <cstdio>
#include <ctime>
#include <cmath>
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";
  }
}


If you dont follow this, read those pages:
http://www.augustcouncil.com/~tgibson/tutorial/iotips.html
http://www.cplusplus.com/reference/clibrary/cstdlib/
>>http://www.cplusplus.com/reference/clibrary/cstdlib/strtol.html
Topic archived. No new replies allowed.