having trouble getting the file.txt to work

  • (5 Pages)
  • +
  • 1
  • 2
  • 3
  • 4
  • 5

70 Replies - 2778 Views - Last Post: 12 November 2014 - 10:57 PM Rate Topic: -----

#31 RFernandez   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 89
  • Joined: 25-August 14

Re: having trouble getting the file.txt to work

Posted 12 November 2014 - 03:02 PM

ill show exactly what the program is supposed to do:
Your program should read a character and determine
- if it is a new line, it is the end of the expression;
print the value at the top of the stack

- if it is a space, ignore it and continue with the next
iteration

- if it a digit, put it back on the input stream and read
an int number

- anything else should be an operator; your program should
them pop the top two elements on the stack, compute the
result, and push the result back on the stack
Was This Post Helpful? 0
  • +
  • -

#32 #define   User is offline

  • Cannot compute!
  • member icon

Reputation: 1868
  • View blog
  • Posts: 6,763
  • Joined: 19-February 09

Re: having trouble getting the file.txt to work

Posted 12 November 2014 - 03:40 PM

You can put print statements in the code temporarily to debug eg

cout << "in loop" << endl;




and also print variables etc.
Was This Post Helpful? 0
  • +
  • -

#33 RFernandez   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 89
  • Joined: 25-August 14

Re: having trouble getting the file.txt to work

Posted 12 November 2014 - 03:59 PM

i tried that and i dont see much wrong with it, that i understand anyway.
Was This Post Helpful? 0
  • +
  • -

#34 RFernandez   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 89
  • Joined: 25-August 14

Re: having trouble getting the file.txt to work

Posted 12 November 2014 - 04:29 PM

anyone got anything? im lost
Was This Post Helpful? -1
  • +
  • -

#35 #define   User is offline

  • Cannot compute!
  • member icon

Reputation: 1868
  • View blog
  • Posts: 6,763
  • Joined: 19-February 09

Re: having trouble getting the file.txt to work

Posted 12 November 2014 - 05:19 PM

If you print a message in the loop do you see that message.

In the switch you return values which means the program will end because you are in the main function. The variables a and b are not given a value.

In the third part you want to pop a value to a and pop a value to b. Instead of returning a value store the value and push it on to the stack.
Was This Post Helpful? 1
  • +
  • -

#36 RFernandez   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 89
  • Joined: 25-August 14

Re: having trouble getting the file.txt to work

Posted 12 November 2014 - 05:48 PM

the message does not appear. yeah i noticed that a and b didnt have a value. im trying to figure out what value to give them. i thought it would be stk.pop but i get an error with that. i believe it is stk.push for the value.

This post has been edited by RFernandez: 12 November 2014 - 05:51 PM

Was This Post Helpful? 0
  • +
  • -

#37 RFernandez   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 89
  • Joined: 25-August 14

Re: having trouble getting the file.txt to work

Posted 12 November 2014 - 06:18 PM

i gave a and b stk.top to see if that would work. and the program ran but still nothing happened. Its able to find my file but it seems its not able to execute anything
Was This Post Helpful? 0
  • +
  • -

#38 RFernandez   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 89
  • Joined: 25-August 14

Re: having trouble getting the file.txt to work

Posted 12 November 2014 - 06:27 PM

There seems to be something wrong with the first while loop. I put the cout message there and it doesnt get shown
Was This Post Helpful? 0
  • +
  • -

#39 RFernandez   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 89
  • Joined: 25-August 14

Re: having trouble getting the file.txt to work

Posted 12 November 2014 - 06:47 PM

here is my current code
#include    <iostream>
#include    <fstream>
#include    <cctype>
#include    <string>
#include    <stack>
using namespace std;

// function prototypes

ifstream& get_ifs(ifstream& ifs);


int main()
{
		
	ifstream ifs;	
	char c;   // input character
	if (!get_ifs(ifs))
	{ //File failed to open.
		return(1);
	}
	



	while ((c = ifs.peek()) != EOF)          // as long as there is input
	{
			

		stack< int > stk;                       // stack of ints
		stk.push(c);// handle one expression
		while ((c = ifs.get()) != '\n')
		{
			if (isspace(c))
			{
				

				putchar(c);
				c++;
				// handle whitespace character
			}
			else if (isdigit(c))             // is it a digit ?
			{
				

				ifs.putback(c);               // put it back in the input
				int number;                     // an incoming number
				ifs >> number;                  // read the number
				stk.push(number);
				cout << number << ' ';
			}
			else                               // it must be an operator
			{
				
				int a = stk.top();
				int b = stk.top();				
				switch (c) 
				{
				case '+':return a + b;
				case '-':return a - b;
				case '*':return a * b;
				case '/':return a / b;
					default: return 0; 
						cerr << "you have entered an incorrect operator" << '\n';
					
				}
				// handle operators +, -, *, and /
				// flag invalid operators
			}
		}

		cout << "result is " << stk.top() << '\n';
	}

	return 0;
}


ifstream& get_ifs(ifstream &ifs)                               // get input file stream
{
	string filename;                            // input file name
	

	cout << "name of file to read from? ";
	cin >> filename;
	ifs.open(filename.c_str());
	


	
	if (!ifs)                                 // cannot open file infilen
	{
		cerr << "cannot open input file '" << filename << "'\n";
	}

	return ifs;                                 // return input file stream

}


Was This Post Helpful? 0
  • +
  • -

#40 #define   User is offline

  • Cannot compute!
  • member icon

Reputation: 1868
  • View blog
  • Posts: 6,763
  • Joined: 19-February 09

Re: having trouble getting the file.txt to work

Posted 12 November 2014 - 06:58 PM

You can try get_ifs returning a bool.

using namespace std;

// function prototypes
bool get_ifs(ifstream& ifs);


int main()
{
  ifstream ifs;	
  char c;   // input character

  if (!get_ifs(ifs))
  { //File failed to open.
    cout << "File failed to open" << endl;
    return(1);
  }

  cout << "after get_ifs" << endl;




The function has changed as well, to return true and false.

bool get_ifs(ifstream &ifs)  
{
  string filename;  

  cout << "name of file to read from? ";
  cin  >> filename;
  
  ifs.open(filename.c_str());

  if (!ifs)  
  {
    cerr << "cannot open input file '" << filename << "'\n";
    return false;
  }

  // fstream is ok
  return true;  
}


Was This Post Helpful? 0
  • +
  • -

#41 RFernandez   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 89
  • Joined: 25-August 14

Re: having trouble getting the file.txt to work

Posted 12 November 2014 - 07:08 PM

i do that and i get the message "after get ifs" and then the "press any key to continue"
Was This Post Helpful? 0
  • +
  • -

#42 #define   User is offline

  • Cannot compute!
  • member icon

Reputation: 1868
  • View blog
  • Posts: 6,763
  • Joined: 19-February 09

Re: having trouble getting the file.txt to work

Posted 12 November 2014 - 07:29 PM

Did you try changing the variable c to a type int?

int main()
{
  ifstream ifs;	
  //char c;   // input character
  int c;




You can add a message in the outer loop.

while((c = ifs.peek()) != EOF)
{
  cout << "in peek loop" << endl;


Was This Post Helpful? 0
  • +
  • -

#43 RFernandez   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 89
  • Joined: 25-August 14

Re: having trouble getting the file.txt to work

Posted 12 November 2014 - 07:31 PM

changed the variable to int. When running the program "in peek loop" does not come up. so that seems to be where the problem is
Was This Post Helpful? 0
  • +
  • -

#44 #define   User is offline

  • Cannot compute!
  • member icon

Reputation: 1868
  • View blog
  • Posts: 6,763
  • Joined: 19-February 09

Re: having trouble getting the file.txt to work

Posted 12 November 2014 - 07:53 PM

It probably best to check the stream in main, to see if that is ok.

cout << "after get_ifs" << endl;

if (ifs.good())
{
  cout << "stream is ok" << endl;
}
else
{
  cout << "stream is NOT ok" << endl;
}


Was This Post Helpful? 1
  • +
  • -

#45 RFernandez   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 89
  • Joined: 25-August 14

Re: having trouble getting the file.txt to work

Posted 12 November 2014 - 07:55 PM

stream is okay
Was This Post Helpful? 0
  • +
  • -

  • (5 Pages)
  • +
  • 1
  • 2
  • 3
  • 4
  • 5