Hi there,
I am experiencing a problem during the program execution.
Here is the program:

#include <iostream>

using std::cout;
using std::cin;

int additions (int a, int b, int c)

{
	int k;
	k = a+b-c;
	return k;
}

int main()

{
	int s;
	int x;
	int y;
	int z;
	cout << "The first number is: \n";
	cin >> x;
	cout << "The second number is: \n";
	cin >> y;
	cout << "The third number is: \n";
	cin >> z;

	s = additions (x,y,z);
	
	cout << "The result is: " << s << "\n";

	if (s == 4)
	cout << " \a\a\a";
	return 0;
}

Basically, when I click on the exe file in the debug folder, the program doesn't execute properly: it asks for the first number, second and third and then when I click enter the terminal disappears (I am using windows XP and the compiler is VC++ 6.0 trial copy).

If I instead compile the program and run it from VC++ (with ctrl F5) everything goes well...what am I doing wrong?
Thanks

Put in a cin.get(); between lines 33 and 34. This will effectively wait for a key to be pressed.

thanks. It seems to work only if the result is 4. If it is another number the program doesn't execute...I think this happened also before adding the line. Is there anything wrong in the code?

Hi there, I tried to the cin.ignore; line (I had to add the semicolon because the compiler didn't like it without) but nothing changes except for a warning during compilation which says:
--------------------Configuration: second - Win32 Debug--------------------
Compiling...
second.cpp
Z:\example\sec\second.cpp(28) : warning C4551: function call missing argument list
Linking...

second.exe - 0 error(s), 1 warning(s)

So the program now looks like the below
Again it works only when the result is 4, any other result the program doesn't finish its execution in the terminal.
I read the tutorial, but in some places it is a bit too advanced for me to be honest. SO basically in my program there is some character kicking around and i need to flush the stream...Might be a bit naive but how about about std::endl;? doesn't it help? I also added that but to no avail...

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int additions (int a, int b, int c)

{
int k;
k = a+b-c;
return k;
}

int main()

{
int s;
int x;
int y;
int z;
cout << "The first number is: \n";
cin >> x;
cout << "The second number is: \n";
cin >> y;
cout << "The third number is: \n";
cin >> z;
cin.ignore;

s = additions (x,y,z);

cout << "The result is: " << s << endl; "\n";

if (s == 4)
cout << " \a\a\a";
cin.get();
return 0;
}

Oh, I see sorry!
Done, but still same problem. Does it have anything to do with maybe the position of the lines in main()? Sorry just doing silly guesses.
Is it that frequent to have a program that compiles ok but doesn't execute properly? sorry, but it is really annoying when it does that:confused:

Oh, I see sorry!
Done, but still same problem. Does it have anything to do with maybe the position of the lines in main()? Sorry just doing silly guesses.
Is it that frequent to have a program that compiles ok but doesn't execute properly? sorry, but it is really annoying when it does that:confused:

so are you saying that if the result is 4 the terminal window remains open till you press enter, however if the result is anything else the window closes without waiting for enter?

Is it that frequent to have a program that compiles ok but doesn't execute properly?

All the time. A loose analogy is that you can write something that has no spelling errors that follows the rules of grammar but makes no sense as a sentence once spoken or written.

Are you using the same code you posted before?

Sort of: if the result of the operation is 4the terminal window display the result "the result is: 4". any other resut the terminal doesn't display the result but, when I put the last integer and click enter to display the result the terminal shuts without returning anything. It would be easier if you try the program yourself I guess, but I don't want to trouble you that much!

I have tried it and I am not able to reproduce the error.

What happens if you run it from a terminal, not clicking on it to open it but if you open the terminal then change to its directory and type in the name of the file? (it shouldn't matter but try it)

So as of now you're clicking on the exe in the debug directory? and like I was asking before, the only change you made was to put in () after cin.ignore?

(btw, there's a stray "\n" on line 32 that's not doing anything. If you want it in that cout bring it inside the quotes).

Edited 6 Years Ago by jonsca: n/a

OK.. I guess it is the difference of newline characters in windows and unix. I tried on Unix and it worked, forgetting that it may not work in the same way for windows. Probably you can try this

cin.ignore ( 80, '\n' ); //reads and discards 80 character or until newline

OK.. I guess it is the difference of newline characters in windows and unix. I tried on Unix and it worked, forgetting that it may not work in the same way for windows. Probably you can try this

cin.ignore ( 80, '\n' ); //reads and discards 80 character or until newline

... That will probably help, if you put it in the correct spot.
Add the ignore line just above the cin.get() at the end of the program.

And if stuff doesn't work after you've changed it, since we don't know what you changed, we can't reliably help.

Edited 6 Years Ago by WaltP: n/a

Ok sorry, maybe with all the changes it got a bit confusing, my fault.
Let me recap.
the original (I will put just the main() because the rest is the same as before) is this

int main()

{

int s;
int x;
int y;
int z;

cout << "The first number is: \n";
cin >> x;
cout << "The second number is: \n";
cin >> y;
cout << "The third number is: \n";
cin >> z;

s = additions (x,y,z);

cout << "The result is: " << s << "\n";

if (s == 4)

cout << " \a\a\a";

return 0;

}

Then I followed Agni's suggestion and I added cin.ignore() after the last cin and cin.get() and also endl; (my own initiative) and corrected the stray /nso that the code looked like:

int main()

{
	int s;
	int x;
	int y;
	int z;
	cout << "The first number is: \n";
	cin >> x;
	cout << "The second number is: \n";
	cin >> y;
	cout << "The third number is: \n";
	cin >> z;
	cin.ignore();

	s = additions (x,y,z);
	
	cout << "The result is: \n" << s << endl;

	if (s == 4)
	cout << " \a\a\a";
	cin.get();
	return 0;
}

Basically, no difference the problem persists but, I tried - as suggested - to run the program from the terminal, so open the terminal navigate to the directory where the program is and run it from there rather than click on the exe file in the debug folder, and it works, the program is executed till the end, no matter the result. Whichever combination of numbers I use the result is displayed perfectly. Not sure if this helps or not.
I haven't tried yet the

cin.ignore ( 80, '\n' );

, I am curious to see whether that fixes the issue if I run the program from the debug folder, I will try tomorrow morning and post the result here!
Thanks for all your help guys!

right, the cin.ignore ( 80, '\n' ); line worked a treat!!
So basically by using that line I effectively flushed the input stream, and that was the problem, wasn't it?
Thanks

That ignores up to 80 characters in the input stream until it finds a '\n' (so if it finds the newline, the '\n' is ignored and it stops).

This article has been dead for over six months. Start a new discussion instead.