alternatives for fgets?

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

1
2
3
  char pofx[SIZE], ch;
fgets(pofx, 100, stdin);
closed account (E0p9LyTq)
Did you try std:string's non-member overload of std::getline?

http://www.cplusplus.com/reference/string/basic_string/getline/

tried it but doesnt work idk wat im doing wrong
What is the program you are trying to solve?
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

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69

#include<iostream>
#include<string>

using namespace std;
const int  SIZE = 50; 

int s[SIZE];
int top = -1; // Global declarations /
int flag = 0;


int pop()
{                      // Function for POP operation /
	return(s[top--]);
}

int push(int elem)
{ // Function for PUSH operation /
	if (flag == 1) 
	{
		int num;
		num = pop();
		s[++top] = elem + 10 * num;
	}
	else if (flag == 0) {
		s[++top] = elem;
		flag = 1;
	}
	return elem;
}


int main()
{        
	// Main Program /
	
	char pofx[SIZE], ch;
	int i = 0, op1, op2;
	cout << "\nEnter postfix expression to be evaluated : ";
	fgets(pofx, 100, stdin); //prompt user to enter the postfix expression with a fixed size.
	//cout << "Enter the Postfix Expression:";
	//fgets(pofx, 100, stdin);
	while ((ch = pofx[i++]) != '\n')
	{
		if (isdigit(ch)) 
			push(ch  -'0'); // Push the operand /
		else if (ch == ' ')
			flag = 0;
		else
		{        // Operator,pop two  operands //
			flag = 0;
			op2 = pop();
			op1 = pop();
			switch (ch)
			{
			case '+':push(op1 + op2); break;
			case '-':push(op1 - op2); break;
			case '*':push(op1*op2); break;
			case '/':push(op1 / op2); break;
			default:
				cout << "Input invalid ... give proper input\n";
				return 0;
			}
		}
	}
	cout << "Result: " <<  s[top];
	system("pause");
}
closed account (E0p9LyTq)
Trying to cram 100 characters into a char array with a size of 50 is going to fail.
i know but somehow the code is running and compling with no errors
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>

using namespace 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
    const char 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.

Topic archived. No new replies allowed.