int Add (int x, int y)
{
std::cout << "In Add(), received " << x << " and " << y << "/n";
return (x+y);
}
int main()
{
std::cout << "I'm in maim()!\n";
std::cout << "\nCalling Add()\n";
std::cout << "The value returned is: " << Add(3,4);
std::cout << "\nBack in main().\n"
std::cout << "\nExiting...\n\n";
return 0;
}
the only problem it had was this part std::cout << "\nExiting...\n\n";
My question is why that is the problem and what would fix it?
Please answer this
Thanks, Mark
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
"the only problem it had" - what problem was that?
The code didn't compile? It didn't run? It didn't do what you expected? Your computer tried to start global war?
You probably think I am being mean to you, but look at your question for a minute. Did you lay out
exactly what was happening? And how that is different than what you wanted? A well asked question
contains within it the logic that gets you answering your own questions, and makes you much better.
That is why I make a point of trying to ask a good question.
Wayne
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
The problem with a missing semi-colon (oops, I gave it away) is that the compiler
doesn't know where the end of the line is, so it keeps on trucking, looking through
the following lines. So you can get an error message that points to a line that
has nothing to do with the real source of the error.
(I think you also missed a \n in the line prior, you will want to check your formatting
when you fix the error)
Wayne
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
You might be surprised to learn (even though you have probably observed it many times in every program you have ever executed under Windows), that when a program in Windows terminates, its window is closed by the OS. Think about it. This may be why you think your program does not work.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Belcher3 - Consider the constrast in usefulness between
"Nothing gets displayed when I run it"
versus
"It still doesn't work"
The first one tells us what happened, and gives us vital clues as to why it happened. The latter doesn't even tell us if the code compiled.
If this is indeed your problem now, (and Clifford is usually right), then your question is also answered in the thread titled "Please Read Before Posting a Question" on how to keep the display window open. It is also answered in the FAQ's that the thread points to.
Wayne
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
STOP! THINK! How does that tell us anything useful? It doesn't. You still
haven't told us ANYTHING about what is going wrong!!!! Did you bother to read
what I wrote?
Post your Basic 3! Before you ask "What's the Basic 3?", they are covered
in the thread titled "Please Read Before Posting a Question"
Wayne
p.s. The code compiles and runs fine here.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hey Wayne sorry that my question wasn't clear enough I was in a rush. Now I have time to tell you that I looked at the "Please Read Before Reading a Question" thread about pausing the screen and it said that I needed a #include <stdlib.h> tag on it and I already had it on their now why does it still not work I have the 4.9.9.2 versoin installed. Yes you were right my screen just flashed for a second then cut off. I will link you the Program again just to make sure were talking about the same thing.
include <iostream>
include <stdlib.h>
int Add (int x, int y)
{
std::cout << "In Add(), received " << x << " and " << y << "/n";
return (x+y);
}
int main()
{
std::cout << "I'm in maim()!\n";
std::cout << "\nCalling Add()\n";
std::cout << "The value returned is: " << Add(3,4);
std::cout << "\nBack in main().\n";
std::cout << "\nExiting...\n\n";
return 0;
}
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
There is also an example of "Hello World" in the "Please Read" thread that incorporates this.
Note that a header almost never actually does anything. For the most part (there are exceptions),
it is just a menu of functions, with no code, so adding a header will seldom fix a run time issue
like this.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Thanks once again Wayne for solving my error. Now here is one last question for this program. Do I have to type
system ("pause") for all my programs or no?
Thanks, Mark
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
It depends what you want it to do, the code only behaves in the manner you determine by your code. If you want it to hang around waiting for user confirmation before terminating, then yes (or some other solution).
In anything non-trivial, it is possible that your code will naturally be user interactive. So for example the user might have to select from a menu or answer some prompt before terminating. In this case of course you would not also need the system( "pause" ) call as well. Note that system( "pause" ) does nothing other than run the command shell 'pause' command. If you run cmd.exe, you can try it directly - typing 'pause' at the command line.
In other cases you might have built a utility that is intended to run without intervention as part of a batch file script for example, and in this case you would indeed want it to terminate without user intervention.
Typically the pause is for your convenience while developing, and you might remove it when done. There are other solutions:
1) Run the code in the debugger with a break point at the end of main().
2) Run the code from within cmd.exe (because the window belongs to cmd, it is not closed when your program ends).
3) Add an item to the tools menu to run your code as a child process of cmd.exe (essentially integrating (2) into the IDE ).
Note that the reason this causes so much confusion is that most IDEs essentially do (2) when the program is launched and so pause without modification of the code, and the unwary accept that 'magic' without question and get confused when faced with Dev-C++ which runs teh code directly. If however you run the code from Windows Explorer for example, the same problem will occur, in this sense running code from Explorer is just like running it from Dev-C++.
Note that because 'pause' is executed by cmd.exe it is unaffected by characters buffered in your program's input buffer. If however you use say getchar() or cin.get(), you may be surprised by the results if you have unused buffered characters in the input. For example, the following code will not hang around for you to press enter before terminating:
int main()
{
char c ;
printf( "Enter a character: " ) ;
c = getchar() ;
printf( "Press Enter for terminate" ) ;
getchar() ;
return 0 ;
}
This happens because standard input is line oriented. Although getchar() gets a single character you have to enter a whole line before it returns, so the buffer contains at least two characters, the one you typed, and the '\n' resulting from pressing <enter>. So the second getchar() returns immediately by reading the buffered '\n'.
Clifford
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I typed this Simple Function
include <iostream>
include <stdlib.h>
int Add (int x, int y)
{
std::cout << "In Add(), received " << x << " and " << y << "/n";
return (x+y);
}
int main()
{
std::cout << "I'm in maim()!\n";
std::cout << "\nCalling Add()\n";
std::cout << "The value returned is: " << Add(3,4);
std::cout << "\nBack in main().\n"
std::cout << "\nExiting...\n\n";
return 0;
}
the only problem it had was this part std::cout << "\nExiting...\n\n";
My question is why that is the problem and what would fix it?
Please answer this
Thanks, Mark
"the only problem it had" - what problem was that?
The code didn't compile? It didn't run? It didn't do what you expected? Your computer tried to start global war?
You probably think I am being mean to you, but look at your question for a minute. Did you lay out
exactly what was happening? And how that is different than what you wanted? A well asked question
contains within it the logic that gets you answering your own questions, and makes you much better.
That is why I make a point of trying to ask a good question.
Wayne
Do you see anything missing on this line of code?
std::cout << "\nBack in main().\n"
look at the end.
The problem with a missing semi-colon (oops, I gave it away) is that the compiler
doesn't know where the end of the line is, so it keeps on trucking, looking through
the following lines. So you can get an error message that points to a line that
has nothing to do with the real source of the error.
(I think you also missed a \n in the line prior, you will want to check your formatting
when you fix the error)
Wayne
Yeah I forgot the semi-colon.....I put it in and it still doesn't work....please help
You might be surprised to learn (even though you have probably observed it many times in every program you have ever executed under Windows), that when a program in Windows terminates, its window is closed by the OS. Think about it. This may be why you think your program does not work.
You are probably right Clifford.
Belcher3 - Consider the constrast in usefulness between
"Nothing gets displayed when I run it"
versus
"It still doesn't work"
The first one tells us what happened, and gives us vital clues as to why it happened. The latter doesn't even tell us if the code compiled.
If this is indeed your problem now, (and Clifford is usually right), then your question is also answered in the thread titled "Please Read Before Posting a Question" on how to keep the display window open. It is also answered in the FAQ's that the thread points to.
Wayne
I refer you back to Wayne's first response.
... and you have read the "PLEASE READ BEFORE POSTING A QUESTION" thread right?
"and it still doesn't work"
STOP! THINK! How does that tell us anything useful? It doesn't. You still
haven't told us ANYTHING about what is going wrong!!!! Did you bother to read
what I wrote?
Post your Basic 3! Before you ask "What's the Basic 3?", they are covered
in the thread titled "Please Read Before Posting a Question"
Wayne
p.s. The code compiles and runs fine here.
Hey Wayne sorry that my question wasn't clear enough I was in a rush. Now I have time to tell you that I looked at the "Please Read Before Reading a Question" thread about pausing the screen and it said that I needed a #include <stdlib.h> tag on it and I already had it on their now why does it still not work I have the 4.9.9.2 versoin installed. Yes you were right my screen just flashed for a second then cut off. I will link you the Program again just to make sure were talking about the same thing.
include <iostream>
include <stdlib.h>
int Add (int x, int y)
{
std::cout << "In Add(), received " << x << " and " << y << "/n";
return (x+y);
}
int main()
{
std::cout << "I'm in maim()!\n";
std::cout << "\nCalling Add()\n";
std::cout << "The value returned is: " << Add(3,4);
std::cout << "\nBack in main().\n";
std::cout << "\nExiting...\n\n";
return 0;
}
That is not all that the FAQ says. Note that is indicates that you have to add some code as well.
Copying from the official FAQ:
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
There are two ways. You can do it this way:
include <stdio.h>
int main()
{
printf ("Press ENTER to continue.\n");
getchar (); // wait for input
return 0;
}
Or this way:
include <stdlib.h>
int main()
{
system ("pause"); // execute M$-DOS' pause command
return 0;
}
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Note that it adds
system("pause");
in the latter case.
There is also an example of "Hello World" in the "Please Read" thread that incorporates this.
Note that a header almost never actually does anything. For the most part (there are exceptions),
it is just a menu of functions, with no code, so adding a header will seldom fix a run time issue
like this.
Here is a nice, simple "Hello World" example that shows one way of keeping the window open:
include<iostream>
using namespace std;
int main(void)
{
cout << "This is what that putz Wayne said to do!\n";
system("pause");
return 0;
}
I took out the comments to make it cleaner to read.
Wayne
Thanks once again Wayne for solving my error. Now here is one last question for this program. Do I have to type
system ("pause") for all my programs or no?
Thanks, Mark
It depends what you want it to do, the code only behaves in the manner you determine by your code. If you want it to hang around waiting for user confirmation before terminating, then yes (or some other solution).
In anything non-trivial, it is possible that your code will naturally be user interactive. So for example the user might have to select from a menu or answer some prompt before terminating. In this case of course you would not also need the system( "pause" ) call as well. Note that system( "pause" ) does nothing other than run the command shell 'pause' command. If you run cmd.exe, you can try it directly - typing 'pause' at the command line.
In other cases you might have built a utility that is intended to run without intervention as part of a batch file script for example, and in this case you would indeed want it to terminate without user intervention.
Typically the pause is for your convenience while developing, and you might remove it when done. There are other solutions:
1) Run the code in the debugger with a break point at the end of main().
2) Run the code from within cmd.exe (because the window belongs to cmd, it is not closed when your program ends).
3) Add an item to the tools menu to run your code as a child process of cmd.exe (essentially integrating (2) into the IDE ).
Note that the reason this causes so much confusion is that most IDEs essentially do (2) when the program is launched and so pause without modification of the code, and the unwary accept that 'magic' without question and get confused when faced with Dev-C++ which runs teh code directly. If however you run the code from Windows Explorer for example, the same problem will occur, in this sense running code from Explorer is just like running it from Dev-C++.
Note that because 'pause' is executed by cmd.exe it is unaffected by characters buffered in your program's input buffer. If however you use say getchar() or cin.get(), you may be surprised by the results if you have unused buffered characters in the input. For example, the following code will not hang around for you to press enter before terminating:
int main()
{
char c ;
printf( "Enter a character: " ) ;
c = getchar() ;
printf( "Press Enter for terminate" ) ;
getchar() ;
return 0 ;
}
This happens because standard input is line oriented. Although getchar() gets a single character you have to enter a whole line before it returns, so the buffer contains at least two characters, the one you typed, and the '\n' resulting from pressing <enter>. So the second getchar() returns immediately by reading the buffered '\n'.
Clifford
Thanks again,