Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

RogueZero

Ok a problem with my source code

This topic is 5535 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok im learning about simple functions in the e book Teach yourself C++ in 21 days. This is the source code im trying to run
#include <iostream>
int Add (int x, int y)
{
cout << "In Add(), received " << x << " and " << y << "\n";
return (x+y);
}
int main()
{
cout << "im in main()!\n";
int a, b, c;
cout << "Enter two numbers: ";
cin >> a;
cin >> b;
cout << "\nCalling Add()\n";
c=Add(a,b);
cout << "\nBack in main().\n";
cout << "C was set to " << c;
cout << "\nExiting...";
cin.get();
return 0;
}
The problem is I run the program and i enter the two numbers then it adds them and closes before i can see what it does. I tried cin.get() to keep it on the screen but that doesn''t seem to work with this program what am i doing wrong?

Share this post


Link to post
Share on other sites
Advertisement
try this.

#include <stdlib.h>
#include <iostream>
using namespace std;
int Add (int x, int y)
{
cout << "In Add(), received " << x << " and " << y << "\n";
return (x+y);
}
int main()
{
cout << "im in main()!\n";
int a, b, c;
cout << "Enter two numbers: ";
cin >> a;
cin >> b;
cout << "\nCalling Add()\n";
c=Add(a,b);
cout << "\nBack in main().\n";
cout << "C was set to " << c;
cout << "\nExiting...";
return 0;
system("pause");
}

just add stdlib.h and after return 0 add system("pause")

Share this post


Link to post
Share on other sites
Before return 0. Before return 0.
Either:
#include <stdio.h> and use getch()
#include <stdlib.h> and use system("pause")
#include <iostream> and use std::cin.get()

Share this post


Link to post
Share on other sites
I would just do
cin>>c;
before the return statement. That way it''s expecting input and won''t close until you give it input.

And anyone know why this happens? I used to have this problem when I used C based languages, and there wasn''t any consitency. Sometimes it happened, sometimes it didn''t.

And I believe you''d need to put the pause before the return statement, because the return statement is what tells it to exit.

Share this post


Link to post
Share on other sites
Sorry guys im a newb and some of the stuff your telling me i dont understand but NashuNutta i tried the #include <stdlib> and an error comes up saying stdlib no such file or directory. Cowsarenotevil i tried the flushing thing and it didn''t work.Nervo i also tried your idea and it still didn''t work. And i dont know what getch() is.

Share this post


Link to post
Share on other sites
ok nickwinters i did your cin>>c; and it worked now how do i exit the program without having to close it down?

Share this post


Link to post
Share on other sites
Ok i got nick cin >> c just needs input from me like a letter or a number and enter but whats the difference in cin>>c; and cin.get() and why does one work and not the other?

Share this post


Link to post
Share on other sites
either use system("pause"); for pausing, like mentioned before, or use this:


while(cin.get()!=''\n'')
//clears out all the crap still in the stream
continue;
cin.get();






--{You fight like a dairy farmer!}

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!