Getline & cin.ignore trouble

Hey guys just needed some help with a code for school, it is pretty basic but i'm stuck on an error that occurs while debugging.

here is the exact code I've typed in...

cout << "Enter the name of the recorder: "; //Entering name of the recorder
getline(cin, name2);
cin.ignore();

cout << "\nThank you, " << name2 <<", for entering the data for " << name << endl;
return 0;

So what happens whenever I input name2 is it doesn't produce an output when I enter a line in.

given output (user input = [bold])

Enter the name of the recorder: [Boomer kuwanger]
Thank you, ,for entering the data for (the variable "name" works, and is the same coding ex: getline but earlier in the program)

please help me solve this problem and why it has no output!
You don't need to use cin.ignore() with getline().
Whenever i remove cin.ignore() the program doesn't allow me to even enter a value for the getline(cin, name2)

here is the code prior to the trouble...

1
2
3
4
5
6
7
8
9
10
11
cout << setw(8) << "yards" << setw(8) << "feet" << setw(10)			// table displaying information calculated through functions
					<< "inches" << setw(8) << "cm" << endl;
	cout << setprecision(4) << setw(8) << yard(d1) << setw(8) << foot(d1) << setw(10)
					<< inches(d1) << setw(8) << cm(d1) << endl;
	cout << setprecision(4) << setw(8) << yard(d2) << setw(8) << foot(d2) << setw(10)
					<< inches(d2) << setw(8) << cm(d2) << endl;
	cout << setprecision(4) << setw(8) << yard(d3) << setw(8) << foot(d3) << setw(10)
					<< inches(d3) << setw(8) << cm(d3) << endl;

	cout << "Enter the name of the recorder: ";						//Entering name of the recorder
	getline(cin, name2);



As you can see the previous text calls functions, im not sure if this is the problem or not, but it still wont allow me to enter a name
Last edited on
name2 should be an std::string. Also, make sure you aren't using std::cin>> anywhere else.
I guess I should have posted this all before, this is the entire code.
I'm still not sure why the error is happening.


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
#include <iostream>         //Include statements
#include <cmath>
#include <iomanip>
#include <string>

using namespace std;		//using std statement

double yard(double);		//All my function prototypes
double foot(double);	    //each function will take in 
double cm(double);			//the given throw distance 3 times
double inches(double);		//and calculate feet,inches,cm and yards

int main()					//main function
{
	string name;			//Variables and strings
	string name2;
	double d1, d2, d3;

	cout << "Enter the name of the competitor: ";  // First line, entering competetor's name
	getline(cin, name);
	
	cout << "\n\n";
	cout << "\t\t\t" << "Olympic Javelin Throws" << endl;					
	cout << '\t' << "Enter the distances, in meters, for " << name << endl;

	cout << "\nPlease enter the distance for throw 1: ";		//allows the user to enter number for first javelin throw
	cin >> d1;
	while (d1 < 0)												//each cin statement has a loop to prevent inaccurate inputs
	{
		cout << "Please enter an integer greater than 0: ";
		cin >> d1;
	}
	cout << "\nPlease enter the distance for throw 2: ";
	cin >> d2;
	while (d2 < 0)
	{
		cout << "Please enter an integer greater than 0: ";
		cin >> d2;
	}
	cout << "\nPlease enter the distance for throw 3: ";
	cin >> d3;
	while (d3 < 0)
	{
		cout << "Please enter an integer greater than 0: ";
		cin >> d3;
	}
	
	cout << setw(8) << "yards" << setw(8) << "feet" << setw(10)			// table displaying information calculated through functions
					<< "inches" << setw(8) << "cm" << endl;
	cout << setprecision(4) << setw(8) << yard(d1) << setw(8) << foot(d1) << setw(10)
					<< inches(d1) << setw(8) << cm(d1) << endl;
	cout << setprecision(4) << setw(8) << yard(d2) << setw(8) << foot(d2) << setw(10)
					<< inches(d2) << setw(8) << cm(d2) << endl;
	cout << setprecision(4) << setw(8) << yard(d3) << setw(8) << foot(d3) << setw(10)
					<< inches(d3) << setw(8) << cm(d3) << endl;

	cout << "Enter the name of the recorder: ";						//Entering name of the recorder
	getline(cin, name2);
	
	cout << "\nThank you, " << name2 <<", for entering the data for " << name << endl;
	return 0;
}

double yard(double x)
{
	double yards = x * 1.093;
	return yards;
}
double foot(double x)
{
	double feet = (x * 1.093) * 3;
	return feet;
}
double inches(double x)
{	
	double inches =((x * 1.093) * 3) *12;
	return inches;
}
double cm(double x)
{
	double cm = x * 100;
	return cm;
}
Soultion:

cin.ignore();
placed before
1
2
cout << "Enter the name of the recorder: ";
getline(cin, name2);
You are using std::cin>> on lines 27, 34, and 41, don't mix it with std::getline().
Topic archived. No new replies allowed.