How to print out line input includes whitespace

Oct 25, 2009 at 10:55am
I have facing a problem on get a line of input with whitespaces inside. How can i solve this problem. Here is my source code.
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
#include <iostream.h>
#include <iomanip.h>
#include <string>
using namespace std;

class CAT
     {
       public:
           CAT() {} 
           ~CAT() {}        
		  void Setname(char* name) { Name=name;}
		  char* Getname(){ return Name;}
          
       private:
         char* Name;
    };

    int main()
    {
       CAT Litter[3];
       int i;
	   char* name;
       for (i = 0; i < 3; i++)
	   {
		   name=new char;
          cin>>name;
		  cin.ignore (1000,'\n');
		  Litter[i].Setname(name);
	   }
       for (i = 0; i < 3; i++)
       {
          cout<<"Cat #"<<i+1<<": ";
          cout<<Litter[i].Getname()<<endl;
       }
     return 0;
 }


It stops printing after the 1st whitespace in each line.
Guidance and help is welcome.
Oct 25, 2009 at 11:05am
Why are you allocating a character per time? (and never delete it)
You should use getline and C++ strings
Oct 25, 2009 at 11:28am
Because i tried to it after the Litter[i].Setname(name); . There will be a debug error occured and the program not reponding.

Then if i use getline , i will only can print out the characters after the 1st white space.
here is my source code using getline.
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
#include <iostream.h>
#include <iomanip.h>
#include <string>

class CAT
     {
       public:
           CAT() {} 
           ~CAT() {}        
		  void Setname(char* name) { Name=name;}
		  char* Getname(){ return Name;}
          
       private:
         char* Name;
    };

    int main()
    {
       CAT Litter[3];
       int i;
	   char* name;
	   
       for (i = 0; i < 3; i++)
	   {
		  name=new char;
		  cin>>name;
		  cin.getline (name,'\n');
		  Litter[i].Setname(name);
	   }
       for (i = 0; i < 3; i++)
       {
          cout<<"Cat #"<<i+1<<": ";
          cout<<Litter[i].Getname()<<endl;
       }
	  return 0;
 }
Oct 25, 2009 at 11:54am
If you allocate a single char, you will never get that right.
Use C++ strings
Oct 25, 2009 at 12:30pm
HOw should i write the code ?
Please provide me some guides.
Oct 25, 2009 at 12:39pm
change char* to string in declaration and then call getline(cin,name), not >>
Oct 25, 2009 at 12:43pm
Do it like this;
1
2
3
4
//...
string s;
getline(cin, s);
//... 
Oct 25, 2009 at 12:48pm
Is it like this ?
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
#include <iostream.h>
#include <iomanip.h>
#include <string>
using namespace std;

class CAT
     {
       public:
           CAT() {} 
           ~CAT() {}        
		  void Setname(string name) { Name=name;}
		  string Getname(){ return Name;}
          
       private:
         string Name;
    };

    int main()
    {
       CAT Litter[3];
       int i;
	   string name;
	   
       for (i = 0; i < 3; i++)
	   {
		  getline(cin,name);
		  Litter[i].Setname(name);
	   }
       for (i = 0; i < 3; i++)
       {
          cout<<"Cat #"<<i+1<<": ";
          cout<<Litter[i].Getname()<<endl;
       }
	  return 0;
 }

But i cannot compile it .
i wan to pass it to a function. Den return with a function.
Here are the compilation errors.
D:\Unimas\Sem 1 0910\software enginrng\Project\testing\test.cpp(26) : error C2780: 'class std::basic_istream<_E,_Tr> &__cdecl std::getline(class std::basic_istream<_E,_Tr> &,class std::basic_string<_E,_Tr,_A> &,const _E)' : expects 3 arguments - 2 p
rovided
c:\program files\microsoft visual studio\vc98\include\string(149) : see declaration of 'getline'
D:\Unimas\Sem 1 0910\software enginrng\Project\testing\test.cpp(26) : error C2784: 'class std::basic_istream<_E,_Tr> &__cdecl std::getline(class std::basic_istream<_E,_Tr> &,class std::basic_string<_E,_Tr,_A> &)' : could not deduce template argument
for 'class std::basic_istream<_E,_Tr> &' from 'class istream_withassign'
D:\Unimas\Sem 1 0910\software enginrng\Project\testing\test.cpp(26) : error C2784: 'class std::basic_istream<_E,_Tr> &__cdecl std::getline(class std::basic_istream<_E,_Tr> &,class std::basic_string<_E,_Tr,_A> &)' : could not deduce template argument
for 'class std::basic_istream<_E,_Tr> &' from 'class istream_withassign'
D:\Unimas\Sem 1 0910\software enginrng\Project\testing\test.cpp(26) : error C2784: 'class std::basic_istream<_E,_Tr> &__cdecl std::getline(class std::basic_istream<_E,_Tr> &,class std::basic_string<_E,_Tr,_A> &)' : could not deduce template argument
for 'class std::basic_istream<_E,_Tr> &' from 'class istream_withassign'
D:\Unimas\Sem 1 0910\software enginrng\Project\testing\test.cpp(32) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (
or there is no acceptable conversion)
Oct 25, 2009 at 12:50pm
Remove the '.h' from included headers and try again.
Oct 25, 2009 at 1:19pm
Ok . It is working now. But when i put this part into my main program. i got some problem occured.
This is my program code.
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
void main()
{
	Plane Seating[240],plane[12];
	Plane S1;
    int i,a,k=300,row,choice2;
	char column,choiceone;
	string name;
	string nation;
	string address;
	char* depart;
	char* arrive;
	long int icno;
	time_t start;	//declaring start as a time function
	time (&start);

	for(a=1;a<=12;a++)
	{
		plane[a].seatplan();			//Set the seat plane for each plane
	}

	for (i=1;i<=100000;i++)
	{
		
		S1.menu(); //Display the main manu
		cout<<"Which process you want to proceed ?"<<endl;
		cin>>choice2;			// choosing which process would like to be proceed
		switch (choice2)
		{
		case 1:		depart=new char;
					arrive=new char;
					Seating[i].Destination_list();
					a=Seating[i].decision();
					cout<<"Please wait a while......"<<endl;
					S1.loading(3);			//hold the program for 3 seconds
					system ("cls");			// clear the screen
					cout<<"Please enter your details."<<endl;
					cout<<"Name                       : ";		//customers enter their names
					getline(cin,name);
					cout<<"Nationality                : ";		//nationality
					getline(cin,nation);
					cout<<"Identity Card/Passport NO. : ";
					cin>>icno;									//IC no./Passport no.
					cout<<"Address                    : ";
					getline(cin,address);								//address
					Seating[i].Setinfo(name,nation,icno,address);	//pass the info entered into the function by reference using pointer
					Seating[i].select_seat();					//customer select their desired seats
					row=Seating[i].Getrow();					//Get the row
					column=Seating[i].Getcolumn();				//and column chosen
					plane[a].show_seat(row,column);				//pass the row and column into function by reference using pointer
					cout<<"Please check the details below are correct or not."<<endl;
					cout<<"Name                       : "<<Seating[i].Getname()<<endl;		//prints
					cout<<"Nationality                : "<<Seating[i].Getnation()<<endl;	//out
					cout<<"Identidy Card/Passport NO. : "<<Seating[i].Geticno()<<endl;		//the info
					cout<<"Address                    : "<<Seating[i].Getaddress()<<endl;	//entered
					Seating[i].decision();
					cout<<"Depart At                  : "<<Seating[i].GetDeparture()<<endl;
					cout<<"Arrive At                  : "<<Seating[i].GetDestination()<<endl;
					cout<<"Seat No.                   : "<<Seating[i].Getrow()<<Seating[i].Getcolumn()<<endl;
					cout<<"If correct, press <Y>. If wrong, press <N>. "<<endl; //confirm the info entered is correct or not
					cin>>choiceone;
					choiceone = static_cast<char>(toupper(choiceone));	//column is converted to uppercase and a temporary copy of type char is made
					if (choiceone == 'Y')
					{
						cout<<"Seat reserved at "<<ctime (&start);	//Set the reservation time
						Seating[i].Settime(ctime (&start));			//for identical customer
						cout<<"Your Reservation Reference No. : "<<i<<endl;
						cout<<"Please settle your payment within 1 month. "<<endl;
						cout<<"If not, your reservation will be cancelled automatically."<<endl;
					}
					else
					{
						i--;
					}break;
		case 2:		cout<<"Enter your reference no."<<endl;				//customers check their reservation 
					cin>>i;												//using the provided reference after the
					cout<<"Name                       : "<<Seating[i].Getname()<<endl;	//reservatiion made
					cout<<"Nationality                : "<<Seating[i].Getnation()<<endl;
					cout<<"Identidy Card/Passport NO. : "<<Seating[i].Geticno()<<endl;
					cout<<"Address                    : "<<Seating[i].Getaddress()<<endl;
					Seating[i].decision();
					cout<<"Depart At                  : "<<Seating[i].GetDeparture()<<endl;
					cout<<"Arrive At                  : "<<Seating[i].GetDestination()<<endl;
					cout<<"Seat No.                   : "<<Seating[i].Getrow()<<Seating[i].Getcolumn()<<endl;
					cout<<"Seat reserved time         : "<<Seating[i].Gettime()<<endl;
					break;
		case 3:		S1.Destination_list();			//Allow the customers to check the seats availability
					a=S1.decision();				//for their desired flight
					plane[a].show_seat(k++,k++);
					break;
		case 4:		i+=100050;			// exits the system
					break;
		default:	i--;
					break;
		}
		S1.loading(2);			//hold the system for 2 seconds before proceed to next line
		system ("cls");			//clear the system screen
	}
}


I dunno y after i key in the nationality den the program keep on display the seating selection out nonstop. Is there any wrong in my code ?
Oct 25, 2009 at 1:22pm
Don't mix getline with >>, see http://www.cplusplus.com/forum/articles/6046/
Oct 25, 2009 at 1:27pm
Means i have to choose either one to be used in my program ?
Oct 25, 2009 at 1:30pm
With cin, getline is the best choice. You can mix them if you know how to do it but using only one is easier.
Oct 25, 2009 at 1:37pm
wat is the difference between get() and getline() ?
Oct 25, 2009 at 1:46pm
Why i change all my cin>> to getline() .
Compilation errors as below occurred .

D:\Unimas\Sem 1 0910\software enginrng\Project\Project 1\Airline Reservation System.cpp(218) : error C2784: 'class std::basic_istream<_E,_Tr> &__cdecl std::getline(class std::basic_istream<_E,_Tr> &,class std::basic_string<_E,_Tr,_A> &)' : could not
deduce template argument for 'class std::basic_string<_E,_Tr,_A> &' from 'int'
D:\Unimas\Sem 1 0910\software enginrng\Project\Project 1\Airline Reservation System.cpp(158) : error C2784: 'class std::basic_istream<_E,_Tr> &__cdecl std::getline(class std::basic_istream<_E,_Tr> &,class std::basic_string<_E,_Tr,_A> &)' : could not
deduce template argument for 'class std::basic_string<_E,_Tr,_A> &' from 'char'
D:\Unimas\Sem 1 0910\software enginrng\Project\Project 1\Airline Reservation System.cpp(403) : error C2784: 'class std::basic_istream<_E,_Tr> &__cdecl std::getline(class std::basic_istream<_E,_Tr> &,class std::basic_string<_E,_Tr,_A> &)' : could not
deduce template argument for 'class std::basic_string<_E,_Tr,_A> &' from 'long'
D:\Unimas\Sem 1 0910\software enginrng\Project\Project 1\Airline Reservation System.cpp(421) : error C2780: 'class std::basic_istream<_E,_Tr> &__cdecl std::getline(class std::basic_istream<_E,_Tr> &,class std::basic_string<_E,_Tr,_A> &,const _E)' :
expects 3 arguments - 2 provided

Why like this one ?
Oct 25, 2009 at 2:12pm
If you are passing an int or other non string types to getline, read the link I posted earlier again.
Oct 25, 2009 at 2:19pm
Means how ? i dun understand it . Can you explain it by giving an example ?
Oct 25, 2009 at 2:31pm
That link shows an example
Oct 25, 2009 at 2:50pm
Okie i got it. But one of my input still cant pass into the function which is the 1st input use getline().
Here is my code. Help me check where it goes wrong .

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
void main()
{
	Plane Seating[240],plane[12];
	Plane S1;
    int i,a,k=300,row,choice2;
	char column,choiceone;
	string name;
	string nation;
	string address;
	string depart;
	string arrive;
	long int icno;
	time_t start;	//declaring start as a time function
	time (&start);

	for(a=1;a<=12;a++)
	{
		plane[a].seatplan();			//Set the seat plane for each plane
	}

	for (i=1;i<=100000;i++)
	{
		
		S1.menu(); //Display the main manu
		cout<<"Which process you want to proceed ?"<<endl;
		cin>>choice2;			// choosing which process would like to be proceed
		switch (choice2)
		{
		case 1:		Seating[i].Destination_list();
					a=Seating[i].decision();
					cout<<"Please wait a while......"<<endl;
					S1.loading(3);			//hold the program for 3 seconds
					system ("cls");			// clear the screen
					cout<<"Please enter your details."<<endl;
					cout<<"Name                       : ";		//customers enter their names
					getline(cin,name);
					cin.ignore(1000,'\n');
					cin.clear();
					Seating[i].Setname(name);
					cout<<"Nationality                : ";		//nationality
					getline(cin,nation);
					cin.ignore(1000,'\n');
					cin.clear();
					Seating[i].Setnation(nation);
					cout<<"Identity Card/Passport NO. : ";
					cin>>icno;
					cin.ignore(1000,'\n');
					cin.clear();
					Seating[i].Seticno(icno);					//IC no./Passport no.
					cout<<"Address                    : ";
					getline(cin,address);							//address
					cin.ignore(1000,'\n');
					cin.clear();
					Seating[i].Setaddress(address);				//pass the info entered into the function by reference using pointer
					Seating[i].select_seat();					//customer select their desired seats
					row=Seating[i].Getrow();					//Get the row
					column=Seating[i].Getcolumn();				//and column chosen
					plane[a].show_seat(row,column);				//pass the row and column into function by reference using pointer
					cout<<"Please check the details below are correct or not."<<endl;
					cout<<"Name                       : "<<Seating[i].Getname()<<endl;		//prints
					cout<<"Nationality                : "<<Seating[i].Getnation()<<endl;	//out
					cout<<"Identidy Card/Passport NO. : "<<Seating[i].Geticno()<<endl;		//the info
					cout<<"Address                    : "<<Seating[i].Getaddress()<<endl;	//entered
					Seating[i].decision();
					cout<<"Depart At                  : "<<Seating[i].GetDeparture()<<endl;
					cout<<"Arrive At                  : "<<Seating[i].GetDestination()<<endl;
					cout<<"Seat No.                   : "<<Seating[i].Getrow()<<Seating[i].Getcolumn()<<endl;
					cout<<"If correct, press <Y>. If wrong, press <N>. "<<endl; //confirm the info entered is correct or not
					cin>>choiceone;
					choiceone = static_cast<char>(toupper(choiceone));	//column is converted to uppercase and a temporary copy of type char is made
					if (choiceone == 'Y')
					{
						cout<<"Seat reserved at "<<ctime (&start);	//Set the reservation time
						Seating[i].Settime(ctime (&start));			//for identical customer
						cout<<"Your Reservation Reference No. : "<<i<<endl;
						cout<<"Please settle your payment within 1 month. "<<endl;
						cout<<"If not, your reservation will be cancelled automatically."<<endl;
					}
					else
					{
						i--;
					}break;
		case 2:		cout<<"Enter your reference no."<<endl;				//customers check their reservation 
					cin>>i;												//using the provided reference after the
					cout<<"Name                       : "<<Seating[i].Getname()<<endl;	//reservatiion made
					cout<<"Nationality                : "<<Seating[i].Getnation()<<endl;
					cout<<"Identidy Card/Passport NO. : "<<Seating[i].Geticno()<<endl;
					cout<<"Address                    : "<<Seating[i].Getaddress()<<endl;
					Seating[i].decision();
					cout<<"Depart At                  : "<<Seating[i].GetDeparture()<<endl;
					cout<<"Arrive At                  : "<<Seating[i].GetDestination()<<endl;
					cout<<"Seat No.                   : "<<Seating[i].Getrow()<<Seating[i].Getcolumn()<<endl;
					cout<<"Seat reserved time         : "<<Seating[i].Gettime()<<endl;
					break;
		case 3:		S1.Destination_list();			//Allow the customers to check the seats availability
					a=S1.decision();				//for their desired flight
					plane[a].show_seat(k++,k++);
					break;
		case 4:		i+=100050;			// exits the system
					break;
		default:	i--;
					break;
		}
		S1.loading(2);			//hold the system for 2 seconds before proceed to next line
		system ("cls");			//clear the system screen
	}
}


your help will be appreciated.
Oct 25, 2009 at 4:35pm
I have a problem about the getline(). Why my 1st input will goes to the second output ad one while the 1st output is empty? Anyone can help check my code where it goes wrong ?
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
void main()
{
	Plane Seating[240],plane[12];
	Plane S1;
    int i,a,k=300,row,choice2;
	char column,choiceone;
	string name;
	string nation;
	string address;
	char* depart;
	char* arrive;
	string icno;
	time_t start;	//declaring start as a time function
	time (&start);

	for(a=1;a<=12;a++)
	{
		plane[a].seatplan();			//Set the seat plane for each plane
	}

	for (i=1;i<=100000;i++)
	{
		
		S1.menu(); //Display the main manu
		cout<<"Which process you want to proceed ?"<<endl;
		cin>>choice2;			// choosing which process would like to be proceed
		switch (choice2)
		{
		case 1:		depart=new char;
					arrive=new char;
					Seating[i].Destination_list();
					a=Seating[i].decision();
					cout<<"Please wait a while......"<<endl;
					S1.loading(3);			//hold the program for 3 seconds
					system ("cls");			// clear the screen
					cout<<"Please enter your details."<<endl;
					cin.clear();
					cout<<"Name                       : ";		//customers enter their names
					getline(cin,name);
					cin.clear();
					Seating[i].Setname(name);
					cout<<"Nationality                : ";		//nationality
					getline(cin,nation);
					cin.clear();
					Seating[i].Setnation(nation);
					cout<<"Identity Card/Passport NO. : ";
					getline(cin,icno);									//IC no./Passport no.
					cin.clear();
					Seating[i].Seticno(icno);
					cout<<"Address                    : ";
					getline(cin,address);						//address
					cin.clear();
					Seating[i].Setaddress(address);	//pass the info entered into the function by reference using pointer
					Seating[i].select_seat();					//customer select their desired seats
					row=Seating[i].Getrow();					//Get the row
					column=Seating[i].Getcolumn();				//and column chosen
					plane[a].show_seat(row,column);				//pass the row and column into function by reference using pointer
					cout<<"Please check the details below are correct or not."<<endl;
					cout<<"Name                       : "<<Seating[i].Getname()<<endl;		//prints
					cout<<"Nationality                : "<<Seating[i].Getnation()<<endl;	//out
					cout<<"Identidy Card/Passport NO. : "<<Seating[i].Geticno()<<endl;		//the info
					cout<<"Address                    : "<<Seating[i].Getaddress()<<endl;	//entered
					Seating[i].decision();
					cout<<"Depart At                  : "<<Seating[i].GetDeparture()<<endl;
					cout<<"Arrive At                  : "<<Seating[i].GetDestination()<<endl;
					cout<<"Seat No.                   : "<<Seating[i].Getrow()<<Seating[i].Getcolumn()<<endl;
					cout<<"If correct, press <Y>. If wrong, press <N>. "<<endl; //confirm the info entered is correct or not
					cin>>choiceone;
					choiceone = static_cast<char>(toupper(choiceone));	//column is converted to uppercase and a temporary copy of type char is made
					if (choiceone == 'Y')
					{
						cout<<"Seat reserved at "<<ctime (&start);	//Set the reservation time
						Seating[i].Settime(ctime (&start));			//for identical customer
						cout<<"Your Reservation Reference No. : "<<i<<endl;
						cout<<"Please settle your payment within 1 month. "<<endl;
						cout<<"If not, your reservation will be cancelled automatically."<<endl;
					}
					else
					{
						i--;
					}break;
		case 2:		cout<<"Enter your reference no."<<endl;				//customers check their reservation 
					cin>>i;												//using the provided reference after the
					cout<<"Name                       : "<<Seating[i].Getname()<<endl;	//reservatiion made
					cout<<"Nationality                : "<<Seating[i].Getnation()<<endl;
					cout<<"Identidy Card/Passport NO. : "<<Seating[i].Geticno()<<endl;
					cout<<"Address                    : "<<Seating[i].Getaddress()<<endl;
					Seating[i].decision();
					cout<<"Depart At                  : "<<Seating[i].GetDeparture()<<endl;
					cout<<"Arrive At                  : "<<Seating[i].GetDestination()<<endl;
					cout<<"Seat No.                   : "<<Seating[i].Getrow()<<Seating[i].Getcolumn()<<endl;
					cout<<"Seat reserved time         : "<<Seating[i].Gettime()<<endl;
					break;
		case 3:		S1.Destination_list();			//Allow the customers to check the seats availability
					a=S1.decision();				//for their desired flight
					plane[a].show_seat(k++,k++);
					break;
		case 4:		i+=100050;			// exits the system
					break;
		default:	i--;
					break;
		}
		S1.loading(2);			//hold the system for 2 seconds before proceed to next line
		system ("cls");			//clear the system screen
	}
}

Name                       :
Nationality                : ng shi wei
Identidy Card/Passport NO. : malaysian
Address                    : 1234567890
Topic archived. No new replies allowed.