getline just skipping

im trying to solve a problem in my programming book which is an incredibly simple challenge in itself, but the point is to practice passing data between functions as opposed to just writing out 3 easy lines of code to print stuff out.

i have to use one function to get the data from the user, one function to output the data and i must keep all of the data in the main function. my problem is that where the user is supposed to input their name the getline function is being skipped and its just going to the next statement, and i am ever so confused :o
please help :)

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
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

bool input(int& number, char name[]);
void output(int x, char y[]);

int main()
{
	int number = 0;
	char name[15];
	
	//run it once to see whether or not to keep going
	bool stop = input(number, name);
	output(number, name);
	
	//keep the loop going until they enter 0
	while (stop)
	{
		cout << endl << endl;
		stop = input(number, name);
		output(number, name);
	}

	cout << endl;
	return 0;
}

bool input(int& number, char name[])
{
	//get the data
	bool stop = false;
	cout << "Number (0 to exit): " << endl;
	cin >> number;
	cout << "Name: " << endl;
	cin.getline(name, 15, '\n');
	
	//if they enter 0 stop the program
	if (number == 0)
		return false;
	else
		return true;
}

void output(int x, char y[])
{
	//output the data
	cout << "the number you entered is " << x;
	cout << endl << "the name you entered is " << y;
	return;
}
closed account (D80DSL3A)
The '\n' remaining in the stream after line 35 is messing things up. Try adding
cin.ignore(256,'\n'); after line 36.
thank you! what exactly do the parameters do in in this function??
Here's a page on it

http://cplusplus.com/reference/iostream/istream/ignore/

Basically, its ignoring the fact that there is a char array is delimited by the newline.
Last edited on
Topic archived. No new replies allowed.