having trouble getting the file.txt to work

  • (5 Pages)
  • +
  • « First
  • 3
  • 4
  • 5

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

#61 #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 - 09:50 PM

One problem that stands out is the return statements in the switch statement. Create a variable to hold calculated value, which will be pushed on to the stack later. You will need to pop a and b from the stack. You will need a break statement in each case section.

   case '+': value = a + b; 
             break;


Was This Post Helpful? 0
  • +
  • -

#62 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: having trouble getting the file.txt to work

Posted 12 November 2014 - 09:51 PM

Quote

I do not know what is wrong with that part thought. it looks right to me and thats what i gathered based on what i found online

What part are you questioning? The file handling part or the switch logic?

The switch logic is the biggest problem, probably all those returns. You do realize that when a return statement is encountered the function returns to the calling function, and since this is main() it returns to the operating system (the program ends).

Jim
Was This Post Helpful? 0
  • +
  • -

#63 #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 - 09:55 PM

The switch statement code looks like it was meant to be for a function.
Was This Post Helpful? 0
  • +
  • -

#64 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 - 10:04 PM

i cant think of another way to write it
Was This Post Helpful? 0
  • +
  • -

#65 #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 - 10:06 PM

I told you how in post #61.
Was This Post Helpful? 0
  • +
  • -

#66 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 - 10:17 PM

i try to pop a value and it says deque empty before pop when i try to run the program

This post has been edited by RFernandez: 12 November 2014 - 10:20 PM

Was This Post Helpful? 0
  • +
  • -

#67 #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 - 10:34 PM

It sounds as if numbers are not being pushed onto the stack. The numbers that are read from the file should be printed when they are pushed. Also you need to push the calculated value onto the stack.
Was This Post Helpful? 0
  • +
  • -

#68 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 - 10:34 PM

okay now i get "deque iterator not derefernecable"

here is new code
#include    <iostream>
#include    <fstream>
#include    <cctype>
#include    <string>
#include    <stack>
#include    <sstream>

using namespace std;

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



int main()
{

	ifstream ifs;
	int c;   // input character
	if (!get_ifs(ifs))
	{
		cout << "File failed to open" << endl;
		return (1);
	}
	



	while (ifs.good() )          // as long as there is input
	{
		
		
		stack< int > stk;                       // stack of ints
		stk.push(c);// handle one exp<b></b>ression
		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 value;
				int a = stk.top();
				stk.pop();
				int b = stk.top();
				stk.pop();
				switch (c)

				{
				case '+':value = a + b;
					stk.push(value);
					break;		
					
				case '-':value = a - b;
					stk.push(value);
					break;
										
				case '*':value = a * b; 
					stk.push(value);
					break;
							
				case '/':value = a/b;
					stk.push(value);
					break;

				default: value = 0;
					stk.push(value);
					break;


				



									
				
					

					

				}
				// handle operators +, -, *, and /
				// flag invalid operators
			}
		}

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

	return 0;
}


bool 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 false;


	}

	return true;                                 // return input file stream

}

Was This Post Helpful? 1
  • +
  • -

#69 #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 - 10:46 PM

You want a peek

c = ifs.peek();

before line 34 (the push on the stack).

You want to pop the stack value after line 103.
Was This Post Helpful? 0
  • +
  • -

#70 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 - 10:49 PM

Put that in. the program just crashes, cant get a peek
Was This Post Helpful? 1
  • +
  • -

#71 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: having trouble getting the file.txt to work

Posted 12 November 2014 - 10:57 PM

This thread has become five pages of you debugging by proxy. Everyone else is doing the work, you tweak, then parrot back a response without really trying to push through it. Plus, the constant bumping of your thread is unnecessary.

I think it's time to close this thread.

@#define and jimblumberg- I wanted to say that you guys have been doing yeoman's work here helping out!
Was This Post Helpful? 1
  • +
  • -

  • (5 Pages)
  • +
  • « First
  • 3
  • 4
  • 5