stoping loop if input is '\n'

i am trying to stop the first for loop when the user ONLY inputs "enter" and then display the current array. I am trying to use "while (Names[i] != '\n')", but it its not working.

1
2
3
4
5
6
7
8
9
10
  	for (i = 0; i < MaxNumNames; i++)
	{
		cout << "Enter a name: ";
		Names[i] = ReadString();
		
	}

	while (Names[i] != '\n')
	for (i = 0; i < MaxNumNames; i++)
		cout << Names[i] << endl;
You need to show ReadString() and how Names[] was declared.
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

using namespace std;

#include "ReadString.h"
#include <string.h>
#include <stdlib.h>
#include <iostream>

void main()
{

	const	int		MaxNumNames(20);
	int		i=0;
	char *	Names[MaxNumNames];
	char a;

	for (i = 0; i < MaxNumNames; i++)
	{
		cout << "Enter a name: ";
		Names[i] = ReadString();
		cin >> a;
	}

	while (Names[i] != '\n')
	for (i = 0; i < MaxNumNames; i++)
		cout << Names[i] << endl;
	
}


and ReadString is declared in header as
 
char *	ReadString();
Last edited on
Line 14 declares an array of pointers.

Since you haven't shown the code for ReadString, it's unclear what the pointer it returns points to. Since it is called multiple times, it should be allocating a new char array on each call (unlikely).

Line 24: You're comparing a pointer to '\n'. You're not comparing a character of the name.
Line 24: i changed to while (*Names[i] != '\n')

however it is not stopping the for loop from line 17-22 and continuing to line 25 when i input "enter"
Topic archived. No new replies allowed.