NEED HELP: Loop not working

I am working on a school assignment that's asking me to finish a program that will display a name a given number of times. Everything works fine except for the looping statement that is to repeat the name. Here is the code (note the comments indicating the section that is not functioning correctly:

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
#include <iostream>
#include <string>
using namespace std;

//main PROGRAM
int main()
{
	string name;
	int limit;

	cout << "Please enter the number of times to display your name: ";
	cin >> limit;
	
	while (limit < 0)
	{
		cout << "ERROR: The number you enter must be a number greater than or equal to 0\n";
		cout << "Please enter the number of times to display your name: ";
		cin >> limit;
	}

// Beginning of problem
	cout << "Please enter your name: ";
	getline(cin, name);
	cin.ignore();

	for (limit != 0; limit > 1; limit--)
	{
		cout << name << endl;
		
	}
// End of problem

system("pause");
return 0;
}


Any help is appreciated.
Last edited on
Try this..
1
2
3
4
for (int x = 0; x <= limit; x++)
{
        cout << name << endl;
}

instead of
1
2
3
4
5
for (limit != 0; limit > 1; limit--)
{
cout << name << endl;

}
Last edited on
Attempted the fix, but the output does not display the name. It's as if the variable name is not storing what I'm typing.
You're telling the compiler to ignore it.
It's because you're mixing getline and operator>>. I thought there was a sticky thread about this...
@ciphermagi

I put the cin.ignore() to have it ignore the enter key pressed when entering the value.
Without the cin.ignore(), it just skips to the end of the program.

@moorecm

Where in the code am I mixing them up? If there is a thread that will explain this, please point me to it.
Found the fix for this problem. As moorecm said, the problem is in mixing getline and >> operators. It appears the new line character during the limit input was stuck in the buffer, so it needed to be cleared. Here's the working code with the correction noted in comments for any future reference:
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
//PRE-PROCESSOR DIRECTIVES
#include <iostream>
#include <string>
using namespace std;

//main PROGRAM
int main()
{
	string name;
	int limit;

	cout << "Please enter the number of times to display your name: ";
	cin >> limit;
	cin.ignore();   // The new line character must be cleared from here

	while (limit < 0)
	{
		cout << "ERROR: The number you enter must be a number greater than or equal to 0\n";
		cout << "Please enter the number of times to display your name: ";
		cin >> limit;
		cin.ignore();   // The new line character must be cleared from here
	}

	cout << "Please enter your name: ";
	getline(cin, name);
	

	for (int i = 1; i <= limit; i++)
	{
		cout << name << endl;
		
	}

system("pause");
return 0;
}


Thanks to all who helped.
Topic archived. No new replies allowed.