Programm stops working..

Hi everyone! I have a problem that I can't solve by myself.. My program is compiled but after I write the number of variable n after its start, the program stop working..
Does anyone have any idea how to solve this?

My input should looks like these:
9
+ 4
+ 3
-
+ 3
-
-
-
+ 2
-
And output should looks like these:
OK
OK
3
OK
3
4
BLAD
OK
2
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
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<stdio.h>
#include<cstdlib>
#include<string>
#include<stack>
#include<sstream>
using namespace std;


void stackFunction(){
	int n=0;//number of commands
	cin >> n;
	stack<int> abc;
	
	for (int i = 0; i < n; i++){
		char s='0';//variable to save sign
		
		scanf("%c",s);
		if (s == '+')
		{
			string e;//expression	
			scanf("%s", e);
			cout << "OK" << endl;
			int l;//auxiliary variable to save number from string expression	
			istringstream is1(e);
			is1 >> l;
			abc.push(l);

		}
		if (s == '-')
		{

			if (abc.empty())
			{
				cout << "BLAD" << endl;
				break;
			}
			else
			{
				cout << abc.top() << endl;
				abc.pop();
			}
		}
	}
}



int main(){
	
	stackFunction();
	return 0;
}
Last edited on
Don't use scanf. it's unsafe and nasty. Even the safe version (scanf_s) I'd steer clear of. Use c++ IO constructs like 'cin'.

Consider using std::string (you are using them in some places so i don't know why you're using chars in other places ).
something like this:

1
2
3
4
5
6
7
8
9
10
....

for (int i = 0; i < n; i++){
		std::string s("");//variable to save sign

		cin >> s;
		if (s.find("+") != string::npos) // <- is there a + in my string
		{

....
Last edited on
I know that better idea is to use cin than scanf but this exercise what I do know exercise from Spoj, when i tried to use cin Spoj write me an error message "
Timeout error" and when i use scanf Spoj doesnt show this error..
You aren't using scanf() correctly. This one of the reasons to avoid it. Line 18 should be scanf("%c", &s) and line 23 won't work at all because scanf() doesn't work with strings.

Converting to cin as mutexe suggested. This also reads the numbers directly instead of reading to a string and parsing it. See other comments throughout.
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
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
#include<stack>

using namespace std;

void 
stackFunction()
{
    stack<int> abc;

    int n = 0;			//number of commands
    cin >> n;

    for (int i = 0; i < n; i++) {
	char operation = '0';		// use meaningful names
	cin >> operation;

	// use a switch instead of an if/then chain. It lets the
	// reader know immediately that you're doing different things
	// depending on the value.
	switch(operation) {
	case '+':
	    int value;
	    cin >> value;
	    abc.push(value);
	    cout << "OK\n";
	    break;
	case '-':
	    if (abc.empty()) {
		cout << "BLAD" << endl;
	    } else {
		cout << abc.top() << endl;
		abc.pop();
	    }
	    break;
	}
    }
}

int 
main()
{

    stackFunction();
    return 0;
}
Thanks u solve my problem ;)
Topic archived. No new replies allowed.