Why does this not wait for input:

Hola, I have a problem; this code will exit immediately, and can someone tell me why:

//add.cpp
#include <iostream>
#include <string>
#include "add.h"
int main(){
string exit;
return (a(1));
// Wait for Enter:
cout << "Press \"Enter\" to exit...";
getline(cin, exit);
return 0;
}

and in the ".h" file:

//add.h
#include <iostream>
#include <string>
using namespace std;
int a(int a) {
return a;
}

P.S. I am using Windows XP, and compiling it with Microsoft Visual C++ 2010 Command Line by "cl 'filepath\add.cpp' /EHsc"
Because your programs ends when you return something from main
You mean, like take away the "return 0;"?
Confused already:\I should know what "return" does though
You are returning a(1) before any output/input is displayed
So what should I do? printf a1?
What do you want to do with a(1) ?
Simply display it; but the point of the program is to test out header files
"return" makes the program exit out of the function it is currently running. Main is a function so it can exit out of that.

I found a couple errors in your code so try this instead. I hope this helps for I haven't actually made a header myself before so this is all I can help you in.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//main stuff
#include <iostream>
#include <string>
#include "add.h" //if this doesn't work try <add.h> or something...
using namespace std;

int main()
{
a(1);
// Wait for Enter:
cout << "Press \"Enter\" to exit...";
getline(cin, exit);
return 0;
}

//in the header file
#include <iostream>
#include <string>
using namespace std;
int a(int a)
{
cout << a;
return 0;
}

Well what the heck! Never did ANY of my online tutorials for any language tell me that's what "return;" does.
P.S. I think putting the #include <iostream> and <string> in both violates the rule of only one #include per program or something
Thanx, I'll try that...
Yes, Thank You Alpha!! Works great!
You're welcome. Glad to be of service. :)
Topic archived. No new replies allowed.