Before I start, I'm sure you've all seen this error, and explained how to solve it many times before, and for that I thank you for bothering to read this, and if you choose to answer, I wish to thank you in advance.

So, the issue I am having is one which I'm sure is simple to solve, and yet I cannot seem to got any viable solution to work. The program compiles fine, and runs perfectly in the compiler console, however, whenever I run the Release executable of my project (which, incidentally is now important, but simply something to help me learn) when I have entered the two integers that the program requires, it calculates the solution, and them immediately closes, giving me no time to see the result. This is the code I am currently using:

#include "stdafx.h"
#include <iostream>

int ReadNumber()								// Defining the fuction for lateruse.
{
	using namespace std;						// Allows us to use cin and cout.
	cout << "Please enter an number: " << endl;	// Tells the user what to do.
	int x;										// Defines x so the input can be stored.
	cin >> x;									// Allows the user to input a number to store in x.
	return x;
	std::cin.get();
}

void WriteAnswer(int x)							// Defining the function for later use.
{  
     using namespace std;						// Lets us use cout.
     cout << "The answer is " << x << endl;		// The text, and the value x are output.
}  

int main()
{
	using namespace std;
	cout << "This program adds two numbers together." << endl;	// Tells the user what the program does.
	int x = ReadNumber();										// Gets the value of x from the input.
	int y = ReadNumber();										// Gets the value of y fromt he input.
	WriteAnswer (x + y);										// Uses the funcion to write the result.
	return 0;
	std::cin.get();
}

I understand that my code is probably messy, and there is much that could be improved on, but your help will be greatly appreciated.

Thank you very much for your time.

It looks like you've nailed down the problem, judging by the extra std::cin.get() calls, but putting them after a return statement means they won't get executed. For example, try swapping this pattern:

return 0;
std::cin.get();

To get this one:

std::cin.get();
return 0;

Well this happens to me too. Most of the time. And therefore i use multiple cin.get() codes and also

if possible i add another cin statement into the code to view the answer.

So try adding the following

std::cin >> x;

Thank you both very much. I've solved the issue thanks to your help. Also, thanks for the prompt replies. I greatly appreciate it.

Thanks again.

hoping you come back to read this: i think you'll find it easier to declare "using namespace std;" right after your #include headings. that way, you won't have to write it in every function, 'cause it will be declared globally so everything can use it.

>i think you'll find it easier to declare "using namespace std;"
>right after your #include headings
Easier, yes. Better, not so much.

>'cause it will be declared globally so everything can use it.
And that's the problem. Have you memorized every single identifier in the std namespace? I haven't. I see people using standard names all the time simply because they haven't either. The "easier" method you're promoting completely defeats the purpose of namespaces in the first place.

>i think you'll find it easier to declare "using namespace std;"
>right after your #include headings
Easier, yes. Better, not so much.

>'cause it will be declared globally so everything can use it.
And that's the problem. Have you memorized every single identifier in the std namespace? I haven't. I see people using standard names all the time simply because they haven't either. The "easier" method you're promoting completely defeats the purpose of namespaces in the first place.

On a larger scale. For simple programs such as the one the OP posted, declaring the std namespace globally is a better idea. There's no downfall to it. I suppose I can see where your point would be valid if it were to refer to a larger program, but I see no reason not to declare it once and leave it be for simple programs.
Of course, I can't pretend I'm an expert (though that is my ultimate goal). I've only been programming since mid 2007, and I haven't come across a situation where it was inconvenient to declare namespace std globally.

I agree that declaring the std namespace at the start of the program would be simpler, but it isn't really an issue. One of the things I have drilled into my head is that if I'm using functions like cin, and cout then i need to declare the std namespace. It isn't really an issue to add the namespace to every function, and if i make a habit of declaring it for the whole program as a whole, it could become an issue at a later date.

>On a larger scale.
On any scale. I've helped fix problems in small programs that stemmed from someone accidentally reusing a standard name. There's no size limit for bad practices.

>I see no reason not to declare it once and leave it be for simple programs.
I've never been a fan of the "I don't see it, so it doesn't exist" way of life.

>I've only been programming since mid 2007, and I haven't come across a
>situation where it was inconvenient to declare namespace std globally.
I've been programming a lot longer than you and I haven't come across a situation where it was inconvenient either. However, I have come across several situations where the subtle bugs caused were disastrous. Calling it an inconvenience diminishes the seriousness of difficult to find errors.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.