getline function problem

I have this problem with the getline function where the output is clumped together:

This program calculates your weight on nine planets:
1 Mercury
2 Venus
3 Earth
4 Mars
5 Jupiter
6 Saturn
7 Uranus
8 Neptune
9 Pluto
Enter your name: TEST
Enter weight: 100
Enter planet number (1-9): 9
Your weight on Pluto is: 50
P
Do you want to enter another planet? (Y/N): N
Do you want to switch user? (Y/N): Y
Enter your name: Enter weight:





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
/* planet

*/

//planet.cpp – displays a grade based on the total number of points
//entered by the user

#include <iostream>
#include <string>

using std::cout;
using std::cin;
using std::endl;
using std::string;

int main()
{
	//declare variables and arrays
	double otherWeight = 0.0;
	double weight = 0.0;
	string name = " ";
	string planet[9] = {"Mercury", "Venus", "Earth",  "Mars", "Jupiter", "Saturn",  "Uranus", "Neptune", "Pluto"};
	double relGrav[9] = {0.4155, 0.8975, 1.0, 0.3507, 2.537, 1.0677, 0.8947, 1.1794, 0.5};
	string statement[9] = {"M", "V", "E", "Ma", "J", "S", "U", "N", "P"};
	char anotherName = ' ';
	int x = 0;
	char anotherPlanet = ' ';
 
	//get input
	cout << "This program calculates your weight on nine planets:" << endl;
	cout <<	"1 Mercury" << endl;
	cout << "2 Venus" << endl;
	cout << "3 Earth" << endl;
	cout << "4 Mars" << endl;
	cout << "5 Jupiter" << endl;
	cout << "6 Saturn" << endl;
	cout << "7 Uranus" << endl;
	cout << "8 Neptune" << endl;
	cout << "9 Pluto" << endl;
	do 
	{
	cout << "Enter your name: ";
	getline(cin, name);
	cout << "Enter weight: ";
	cin >> weight;
	do 
	{
		
		cout << "Enter planet number (1-9): ";
		cin >> x;
		
		otherWeight = relGrav[x - 1] * weight;
		cout << "Your weight on " << planet[x - 1] << " is: " << otherWeight << endl;
		cout << statement[x - 1] << endl;
		

	cout << "Do you want to enter another planet? (Y/N): ";
	cin >> anotherPlanet;
	anotherName = toupper(anotherPlanet);

	}	while (anotherPlanet != 'N');

		cout << "Do you want to switch user? (Y/N): ";
	cin >> anotherName;
	anotherName = toupper(anotherName);

	} while (anotherName != 'N');
	return 0;
}  //end of main function


Does anyone know how i can fix it?
Code Is Fixed Below.
Last edited on
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
/* planet

*/

//planet.cpp – displays a grade based on the total number of points
//entered by the user

#include <iostream>
#include <string>

using std::cout;
using std::cin;
using std::endl;
using std::string;

int main()
{
	//declare variables and arrays
	double otherWeight = 0.0;
	double weight = 0.0;
	string name = " ";
	string planet[9] = {"Mercury", "Venus", "Earth",  "Mars", "Jupiter", "Saturn",  "Uranus", "Neptune", "Pluto"};
	double relGrav[9] = {0.4155, 0.8975, 1.0, 0.3507, 2.537, 1.0677, 0.8947, 1.1794, 0.5};
	string statement[9] = {"M", "V", "E", "Ma", "J", "S", "U", "N", "P"};
	char anotherName = ' ';
	int x = 0;
	char anotherPlanet = ' ';
 
	//get input
	cout << "This program calculates your weight on nine planets:" << endl;
	cout <<	"1 Mercury" << endl;
	cout << "2 Venus" << endl;
	cout << "3 Earth" << endl;
	cout << "4 Mars" << endl;
	cout << "5 Jupiter" << endl;
	cout << "6 Saturn" << endl;
	cout << "7 Uranus" << endl;
	cout << "8 Neptune" << endl;
	cout << "9 Pluto" << endl;
	do 
	{
	cout << "Enter your name: ";
	//added
	cin.clear();
	cin.sync();
	getline(cin, name);
	cout << "Enter weight: ";
	cin >> weight;
	do 
	{
		
		cout << "Enter planet number (1-9): ";
		cin >> x;
		
		otherWeight = relGrav[x - 1] * weight;
		cout <<name <<" Your weight on " << planet[x - 1] << " is: " << otherWeight<<endl ;
		cout << statement[x - 1] << endl;
		

	cout << "Do you want to enter another planet? (Y/N): ";
	cin >> anotherPlanet;
	anotherName = toupper(anotherPlanet);

	}	while (anotherPlanet != 'N');

		cout << "Do you want to switch user? (Y/N): ";
	cin >> anotherName;
	anotherName = toupper(anotherName);

	} while (anotherName != 'N');
	return 0;
}  
 
Last edited on
Is there any way to use the ignore function to do this?
Yep. Here.
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
/* planet

*/

//planet.cpp – displays a grade based on the total number of points
//entered by the user

#include <iostream>
#include <string>

using std::cout;
using std::cin;
using std::endl;
using std::string;

int main()
{
	//declare variables and arrays
	double otherWeight = 0.0;
	double weight = 0.0;
	string name = " ";
	string planet[9] = {"Mercury", "Venus", "Earth",  "Mars", "Jupiter", "Saturn",  "Uranus", "Neptune", "Pluto"};
	double relGrav[9] = {0.4155, 0.8975, 1.0, 0.3507, 2.537, 1.0677, 0.8947, 1.1794, 0.5};
	string statement[9] = {"M", "V", "E", "Ma", "J", "S", "U", "N", "P"};
	char anotherName = ' ';
	int x = 0;
	char anotherPlanet = ' ';
 
	//get input
	cout << "This program calculates your weight on nine planets:" << endl;
	cout <<	"1 Mercury" << endl;
	cout << "2 Venus" << endl;
	cout << "3 Earth" << endl;
	cout << "4 Mars" << endl;
	cout << "5 Jupiter" << endl;
	cout << "6 Saturn" << endl;
	cout << "7 Uranus" << endl;
	cout << "8 Neptune" << endl;
	cout << "9 Pluto" << endl;
	do 
	{
	cout << "Enter your name: ";

	getline(cin, name);
	cout << "Enter weight: ";
	cin >> weight;
	do 
	{
		
		cout << "Enter planet number (1-9): ";
		cin >> x;
		
		otherWeight = relGrav[x - 1] * weight;
		cout <<name <<" Your weight on " << planet[x - 1] << " is: " << otherWeight<<endl ;
		cout << statement[x - 1] << endl;
		

	cout << "Do you want to enter another planet? (Y/N): ";
	cin >> anotherPlanet;
	anotherName = toupper(anotherPlanet);

	}	while (anotherPlanet != 'N');

		cout << "Do you want to switch user? (Y/N): ";
	cin >> anotherName;
	anotherName = toupper(anotherName);
        //added
        cin.ignore();
	} while (anotherName != 'N');
	return 0;
} 
Last edited on
What Subzero030201 has shown will not necessary work because cin.sync(); is not guaranteed to do anything.

The problem is that when you use operator>> to read from cin it will just read the number or whatever it is you are reading and then stop, leaving the end of line character still in the stream. When you later call getline it will read data until it finds a new line character. If the first thing it finds is a newline character it will stop reading right away and you get an empty string.


To fix that you can call cin.ignore(numeric_limits<streamsize>::max(), '\n'); after the use of operator>> but before the call to getline.

EDIT: Ah yes you can use just cin.ignore();. That will remove a single character whatever it is, so it will work as long as there is no extra characters on the line, which could happen if the user press space before he hits enter.
Last edited on
it should work if you clear the stream first.
Last edited on
Visual C++ and GCC handles sync() differently. GCC's sync does not discard any input while I think Visual C++ does.
Last edited on
Topic archived. No new replies allowed.