How to use in.get() with text file

I'm writing a program that reads in words from a text file into a stack and queue to check if they are palindromes.
Here is the text file:


madam
Madam I'm Adam.
A man, a plan, a canal, Panama!
Never odd or even
Amor, Roma
race car
step on NO pets
Able was I, ere I saw Elba
$$$$$


This code reads up to the first line "madam" and does not go onto the next line. What am I doing wrong?
Also, how can I output it so that I can print each word that is a palindrome with "is a Palindrome etc" next to it?
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
  int main(){
	ifstream in;
	in.open("input.txt");
	int i=0;
	int num=0;
	char word;	
	
	Stack *s;
	s=initStack();
	Queue *q;
	q=initQueue();

	in.get(word);
	while(word!='\n'){
		word=toLower(word);
    		enqueue(q, word);
    		push(s, word);
		num=num+1;		
    	in.get(word);
	}
			while(!isEmpty(q) && !isEmpty(s) && (q->front->data==s->top->data)){
	    		pop(s);
				dequeue(q);							
					}
				if(isEmpty(s))
					cout<<word<<" is a Palindrome";
				else
					cout<<word<<" is not a Palindrome";
	 	
		return 0;
}
Hello ElleJay,

The choice of "word" for a single character is a bad choice. It is misleading. it makes me think that "word" will contain a whole word not a single character. Most often people will use "ch" to describe a single character or even "letter" is a better choice.

The first while loop is working as it should. It reads all the characters in a line until a new line is reached and then ends.

The second while loop determines if it is a palindrome and clears the stack and queue or partially. After the if statements you should that the stack ans queue are completely cleared for the next line.

The if statements work for now, but still need some work before you read the next line. It is more to the point that when a "$" is read the if/else would need to be bypassed so the outer loop will end and the program can end. I have not finished working on this part yet.

What you have works. All yo need now is a way to get back to the top and process the second line. I still have that to work on.

I include this to let you see what I have done so far:

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
int main()
{
	std::ifstream in;
	in.open("Input2.txt");
	int i = 0;
	int num = 0;
	std::string word;
	char ch;

	Stack *s;
	s = initStack();
	Queue *q;
	q = initQueue();

	// <--- Starting here need a way to loop until "$" is reached.
	in.get(ch);

	while (ch != '\n' && ch != '$')  // <--- Changed "word" to "ch". Added the && part in case you reach the "$".
	{
		ch = toLower(ch);
		enqueue(q, ch);
		push(s, ch);
		num = num + 1;
		word += ch;  // <--- Added this line.
		in.get(ch);
	}

	while (!isEmpty(q) && !isEmpty(s) && (q->front->data == s->top->data) && ch != '$')
	{
		pop(s);
		dequeue(q);
	}

	if (isEmpty(s))  // <--- This section needs more work.
	{
		std::cout << "\"" << word << "\" is a Palindrome";
		word.clear();  // <--- Clears the contents of the string and leaves it empty.
	}
	else
	{
		std::cout << word << " is not a Palindrome";
		word.clear();
	}

	std::cout << "\n\n\n\n Press Enter to continue";
	std::cin.get();

	return 0;
}


What I did not include is that I added the header file "<string>" and put a comment on "<cstring>" and "<cstdlib>". You do not need to include "cstdlib" because "stdlib.h" is included through the "iostream" header file. The "cstring" file: there is nothing from this header file that you are using, I speak of the functions defined in this header file.

I do not know what happened, but watch your indenting. It makes the code easier to read.

Notice the difference between my code and your code especially the {}s. When they line up in the same column they are easier to find and the indented code lets you know what it goes with. In your code I had a hard time seeing the closing } of the second while loop until I put it into my IDE to straighten out the code to eventually fine it on line 24. A helpful nudge to make your life easier in the future.

Hope that helps,

Andy
You do not need to include "cstdlib" because "stdlib.h" is included through the "iostream" header file.

It's better not to rely on this, it might be not the case with some compilers.
It's common practice to include all the headers you need in your file and not to rely that they are included somewhere else.
@Thomas1965,

Good point. Noted. Thank you.

Andy
Is it required to use cin.get ? Much easier to read the whole line with getline and loop through all the chars in the string and add them to the Stack and Queue.
1
2
3
4
5
6
7
8
9
10
  string line;
  while (getline(in, line))
  {
    line = toLower(line);
    for(char ch: line)
    {
      enqueue(q, word);
      push(s, word);
    }
  }


I'm only to use characters, not allowed to use string type at all
Topic archived. No new replies allowed.