stacks

Im not sure how to write this segment of code. My goal here is to ask user for a file name, then read in the file one character at a time into a stack class reference object "DoubleStack dblStack;". This is what i have so far...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main (void)
{
  string fname;
  cout << "Enter name of file to open: \n";
  cin >> fname;
  ifstream in;
  in.open(fname);
  assert (in.is_open());
  while (!in.eof())
  {
    for (int i = 0; i <= STACK_CAPACITY; i++)
      {
        myCharArray[i];
        dblStack = myCharArray[i];
       }
   }
}


Is this right?
Suggestions, tips, constructive criticism?

I don't understand what you are doing on line 13.

Line 14 might be something like
dblStack.push( myCharArray[i] ); //push the char on to the stack
It depends on your class DoubleStack, and its methods.
Where is myCharArray coming from anyway? If you want the chars in a stack you dont need to put them in an array first, try:
1
2
3
4
5
6
... //line 9
while(!in.eof()) {
  char c;
  in >> c; //read the char
  dblStack.push(c); //push c on the stack
} ...
I'm terribly sorry for not explaining this better. The two stacks are implemented with a dynamic array. The array is declared and initialized in two header files not mentioned here. Sorry for the confusion. Also, I realize now that i could have just used push to do what I needed and feel very stupid after having noticed lol
So, was that it?

PS: I've made far worse oversights, and every time I realize it I say "Aww, I'm an idiot!" It's okay to say it, just don't believe it.
In addition to what others have said you should not loop on in.eof(). It is better to loop on the stream state after the get() has been called. That way you know that your char is valid. Also assert() is not suited to checking user input.

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
#include <fstream>
#include <iostream>

using namespace std;

//int main(void) // void not needed
int main()
{
	string fname;
	cout << "Enter name of file to open: \n";
	cin >> fname;
	ifstream in;
	in.open(fname.c_str()); // can't pass std::string to open()

	// assert (in.is_open()); // Not a good use of assert()

	// Better to use if()
	if(!in.is_open())
	{
		std::cout << "Could not open file: " << fname << '\n';
		return 1; // return 1 to indicate error
	}

	// while (!in.eof()) // Almost never the right thing
	// check the *result* of the get() to determine if it worked
	char c;
	while(in.get(c)) dblStack.push(c);
}
Last edited on
This helped A LOT
So I've taken what i've learned from all that and revised this section in my driver. How does it look now?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main()
{
   string fname;
   cout << "Enter name of file to open: \n";
   cin >> fname;
   ifstream in;
   in.open(fname.c_str());
   if (!in.is_open())
   {
      std::cout<< "Could not open file: " << fname << '\n';
      return 1;
    }
    else
    {
       char c;
       if (in.get(c))
       {
          myCharArray[c];
          //to be continued... 


at this point in the code, i need to determine if the character is a capital letter or a lowercase letter and place them in either stack one (lowercase letters) or stack two (capital letters). I have no clue how to do this...
Topic archived. No new replies allowed.