case '+': value = a + b;
break;
70 Replies - 2777 Views - Last Post: 12 November 2014 - 10:57 PM
#61
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.
#62
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
#63
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.
#64
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
#65
Re: having trouble getting the file.txt to work
Posted 12 November 2014 - 10:06 PM
I told you how in post #61.
#66
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
#67
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.
#68
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
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
}
#69
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.
c = ifs.peek();
before line 34 (the push on the stack).
You want to pop the stack value after line 103.
#70
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
#71
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!
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!

New Topic/Question
This topic is locked




MultiQuote





|