I’m c++ newbie. My first app looks like that.
#include "stdafx.h"
#include <iostream>
using namespace std;
int add (int x, int y);
int add (int x, int y){
return (x+y);
}
int _tmain(int argc, _TCHAR* argv[])
{
int x, y;
cin >>x>>y;
cout <<add (x,y);
cin.get();
return 0;
}
Got 2 questions:
- Why console window closing itself directly after function returns value even if I used
cin.get();? - I tested this application without
int add (int x, int y);line at the top of file. It worked well. Do I need to write prototype for every function or application works without it too?
Question 1: The
cin >>x>>yleaves a newline in the input buffer, which gets read bycin.getinstead, causing it to go on.Try
Question 2: The prototype is there so you can let the compiler know the function exists, then use the function (say in main), and then define the body of the function later (say after main).