Archived

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

RogueZero

Ok a problem with my source code

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
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
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
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
quote:
Original post by Greatwolf
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!}

You don''t need the std::cin.get() call after the loop. The last iteration of the loop should remove the newline character from the input queue.     :]

[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]

Share this post


Link to post
Share on other sites
yes I know but we still want to execute a pause and that''s what the last cin.get() is for We only want to execute it when the stream is empty so that it''ll prompt the user for input. Otherwise no pause will happen after loop''s done





--{You fight like a dairy farmer!}

Share this post


Link to post
Share on other sites
quote:
Original post by Greatwolf
yes I know but we still want to execute a pause and that's what the last cin.get() is for We only want to execute it when the stream is empty so that it'll prompt the user for input. Otherwise no pause will happen after loop's done


OK, I see. I didn't really read through the original post, I just saw std::cin and took a guess that it was something to do with white spaces and istream input queues (e.g. std::getline()/std::istream::getline()).

[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]

[edited by - Lektrix on July 26, 2003 6:24:32 AM]

Share this post


Link to post
Share on other sites
quote:

i tried the #include <stdlib>


The compiler is right, there is no such file ''stdlib'', it should either be ''#include <stdlib.h>'' or ''#include <cstdlib>''. Then you can use system("pause").



-If you see this image I may or may not be online
My website

Share this post


Link to post
Share on other sites
quote:
Original post by brassfish89
The compiler is right, there is no such file ''stdlib'', it should either be ''#include <stdlib.h>'' or ''#include <cstdlib>''. Then you can use system("pause").


No, it should be either cstdlib or cstdlib.

[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]

Share this post


Link to post
Share on other sites