Hey fellas. i want to know if there is an alternative for fgets This is what i have. For some reaseon i tried cin getline no luck i was wondering if there is another way to that gives me the same output result without using fgets
a postfix evaluation. this is the code i found online but i was trying to fix it up a bit and i came across the fgets which i nvr seen before. was wondering if there is an alternative. i tried many other way but no luck
Just because a program compiles with no errors doesn't mean it is correct. You have a buffer over flow waiting to happen (it'll probably happen at the best moment possible, like when your instructor tries to run your program).
The best thing to do is stop using C-strings and start using std::strings, then you won't have this problem.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include <string>
#include <iostream>
usingnamespace std;
int main()
{
// Main Program /
std::string postfix;
cout << "\nEnter postfix expression to be evaluated : ";
getline(cin, postfix);
...
If for some horrible reason you must use the C-string you can still use getline() instead of fgets().
1 2 3 4
constchar postfix_size = 50;
char postfix[postfix_size];
cout << "\nEnter postfix expression to be evaluated : ";
cin.getline(postfix, postfix_size);
Notice the difference in the getline(), there are two different versions of this function one that works with a std::string and another that works with a C-string.