 |
Cheat Engine The Official Site of Cheat Engine
|
| View previous topic :: View next topic |
| Author |
Message |
Travis13 Expert Cheater
Reputation: 0
Joined: 17 Feb 2007 Posts: 199
|
Posted: Sun Aug 17, 2008 6:54 pm Post subject: C++ Functions Problem??? Help Please! |
|
|
Hey people of cef, sorry for posting this in the wrong section, I really am, but everyone knows that everyone visits here the most. So enough of that...here's my problem.
The following code is from an example off the www.cplusplus.com c++ tutorial.
| Code: | #include <iostream>
using namespace std;
int addition (int a, int b)
{
int r;
r=a+b;
return (r);
}
int main ()
{
int z;
z = addition (5,3);
cout << "The result is " << z;
return 0;
}
|
so after I saw this I wanted to do two things...that I thought would be easy.
1. make everything a double so the program can evaluate decimals too.
2. make so users can input the numbers
so heres the updates code i wrote (the cin.get(); 's are there because I'm using dev-c++)
| Code: |
#include <iostream>
using namespace std;
double number1;
double number2;
double addition (double a, double b)
{
double r;
r=a+b;
return (r);
cin.get();
}
int main ()
{
double z;
cout << "Please enter number1" << endl;
cin >> number1;
cout << "Please enter number2" << endl;
cin >> number2;
z = addition (number1 , number2);
cout << "The result is " << z;
cin.get();
return 0;
} |
But after running this I was asked to type in the first and second number.....after I press enter on my second number it closes??!?!! even after i put cin.get(); after the function statement and the int main statement......i have no clue why so I would appreciate some help PLEASE AND TY.
Travis13
edit: i know it works since i typed in 10 and 9 and right when it finished my computer lagged and said "The result is 19" so i just need to know why it closes _________________
Learning C++, trying, failing, never gonna give up tho  |
|
| Back to top |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Sun Aug 17, 2008 6:58 pm Post subject: |
|
|
Try this:
| Code: |
#include <iostream>
double add(double num1, double num2) {
return num1+num2;
}
int main() {
double num1;
double num2;
std::cout<<"Please enter number 1: ";
std::cin>>num1;
std::cout<<"\nPlease enter number 2: ";
std::cin>>num2;
std::cout<<"\nThe answer is: "<<add(num1, num2)<<"\n";
cin.sync();
cin.ignore();
return 0;
}
|
_________________
| 8D wrote: |
cigs dont make people high, which weed does, which causes them to do bad stuff. like killing |
|
|
| Back to top |
|
 |
Travis13 Expert Cheater
Reputation: 0
Joined: 17 Feb 2007 Posts: 199
|
Posted: Sun Aug 17, 2008 7:20 pm Post subject: |
|
|
| oib111 wrote: | Try this:
| Code: |
#include <iostream>
double add(double num1, double num2) {
return num1+num2;
}
int main() {
double num1;
double num2;
std::cout<<"Please enter number 1: ";
std::cin>>num1;
std::cout<<"\nPlease enter number 2: ";
std::cin>>num2;
std::cout<<"\nThe answer is: "<<add(num1, num2)<<"\n";
cin.sync();
cin.ignore();
return 0;
}
|
|
sorry but i want to do it the proper function way that im learning from the online tutorial _________________
Learning C++, trying, failing, never gonna give up tho  |
|
| Back to top |
|
 |
Renkokuken GO Moderator
Reputation: 4
Joined: 22 Oct 2006 Posts: 3249
|
Posted: Sun Aug 17, 2008 7:51 pm Post subject: |
|
|
| Code: | cin.sync();
cin.ignore(); |
Much better than just cin.get(), which doesn't clear the input buffers. Learning by a tutorial, and the safer, more preferred way, should make a difference.
Anyhow, my way would be:
| Code: | #define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
__inline double AddDouble( __in double dOne, __in double dTwo ) {
return ( dOne + dTwo );
}
int __cdecl _tmain( __in int argc, __in const LPTSTR argv[] ) {
UNREFERENCED_PARAMETER( argc );
UNREFERENCED_PARAMETER( argv );
INPUT_RECORD inRec = {0};
DWORD dwRead = 0L;
double dOne = 0.0f,
dTwo = 0.0f;
_tprintf_s( _T( "Please enter a number: " ) );
_tscanf_s( _T( "%lf" ), &dOne );
fflush(stdin);
_tprintf_s( _T( "Please enter a second number: " ) );
_tscanf_s( _T( "%lf" ), &dTwo );
_tprintf_s( _T( "Result is: %lf" ), AddDouble( dOne, dTwo ) );
do {
ReadConsoleInput( GetStdHandle( STD_INPUT_HANDLE ), &inRec, 1, &dwRead );
} while ( inRec.EventType != KEY_EVENT );
return ERROR_SUCCESS;
} |
|
|
| Back to top |
|
 |
samuri25404 Grandmaster Cheater
Reputation: 7
Joined: 04 May 2007 Posts: 955 Location: Why do you care?
|
Posted: Sun Aug 17, 2008 8:08 pm Post subject: |
|
|
| Renkokuken wrote: | | Code: | cin.sync();
cin.ignore(); |
Much better than just cin.get(), which doesn't clear the input buffers. Learning by a tutorial, and the safer, more preferred way, should make a difference.
Anyhow, my way would be:
| Code: | #define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
__inline double AddDouble( __in double dOne, __in double dTwo ) {
return ( dOne + dTwo );
}
int __cdecl _tmain( __in int argc, __in const LPTSTR argv[] ) {
UNREFERENCED_PARAMETER( argc );
UNREFERENCED_PARAMETER( argv );
INPUT_RECORD inRec = {0};
DWORD dwRead = 0L;
double dOne = 0.0f,
dTwo = 0.0f;
_tprintf_s( _T( "Please enter a number: " ) );
_tscanf_s( _T( "%lf" ), &dOne );
fflush(stdin);
_tprintf_s( _T( "Please enter a second number: " ) );
_tscanf_s( _T( "%lf" ), &dTwo );
_tprintf_s( _T( "Result is: %lf" ), AddDouble( dOne, dTwo ) );
do {
ReadConsoleInput( GetStdHandle( STD_INPUT_HANDLE ), &inRec, 1, &dwRead );
} while ( inRec.EventType != KEY_EVENT );
return ERROR_SUCCESS;
} |
|
Haha, leave it to Renko to make a big deal out of something stupid. =P
I doubt the poor guy understands half the crap in there.
| Code: |
#include <iostream>
double Add( double d1, double d2 ) {
return (d1 + d2);
}
int main( int argc, char *argv[] ) {
double d1 = 0.0, d2 = 0.0;
std::cout << "Enter the first number" << std::endl;
std::cin >> d1;
std::cout << "Enter the second number" << std::endl;
std::cin >> d2;
std::cout << "Result!: " << Add( d1, d2 ) << std::endl;
std::cin.sync();
std::cin.ignore();
return 0;
} |
_________________
|
|
| Back to top |
|
 |
WafflesFTW Expert Cheater
Reputation: 0
Joined: 21 Mar 2008 Posts: 131
|
Posted: Sun Aug 17, 2008 8:10 pm Post subject: |
|
|
| Uh, why would you use windows.h for a console application which does not even interact with windows? |
|
| Back to top |
|
 |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
Posted: Sun Aug 17, 2008 8:30 pm Post subject: |
|
|
| WafflesFTW wrote: | | Uh, why would you use windows.h for a console application which does not even interact with windows? |
Oh my god...
~flush~
That would be the minute amount of faith I had in you for being a half decent coder going down the toilet. |
|
| Back to top |
|
 |
HalfPrime Grandmaster Cheater
Reputation: 0
Joined: 12 Mar 2008 Posts: 532 Location: Right there...On your monitor
|
Posted: Sun Aug 17, 2008 9:12 pm Post subject: |
|
|
This is very odd and I have no idea why it's happening, but, after the cins, the next cin.get() is completely ignored. If you put 2 cin.get()s, it'll pause...very odd.
btw, you don't need to make number1 and number2 global since you're passing them to the function directly. _________________
|
|
| Back to top |
|
 |
manc Grandmaster Cheater
Reputation: 1
Joined: 16 Jun 2006 Posts: 551
|
Posted: Sun Aug 17, 2008 10:00 pm Post subject: |
|
|
I tried to keep it as close to your original code as possible, and it works. Here you go:
| Code: |
#include <iostream>
using namespace std;
double number1;
double number2;
double addition (double a, double b)
{
double r;
r=a+b;
return r;
}
int main ()
{
double z;
cout << "Please enter number 1: " << endl;
cin >> number1;
cout << "Please enter number 2: " << endl;
cin >> number2;
z = addition (number1 , number2);
cout << "The result is " << z;
cin.get();
cin.sync();
cin.ignore();
return 0;
} |
_________________
|
|
| Back to top |
|
 |
xBubba How do I cheat?
Reputation: 0
Joined: 03 Oct 2006 Posts: 4
|
Posted: Sun Aug 17, 2008 10:34 pm Post subject: |
|
|
I use Dev-C++ too, so here's the solution...
Without dramatically changing yours, this is what it should look like:
| Code: | #include <iostream>
using namespace std;
double number1;
double number2;
double addition (double a, double b)
{
double r;
r=a+b;
return (r);
cin.get();
}
int main ()
{
double z;
cout << "Please enter number1" << endl;
cin >> number1;
cin.get();
cout << "Please enter number2" << endl;
cin >> number2;
cin.get();
z = addition (number1 , number2);
cout << "The result is " << z;
cin.get();
return 0;
} |
You have to add the "cin.get();"s after each time you need to input something. I don't know why since I'm new to C++ and still learning.
Hope this helps.
EDIT: Sorry, I didn't see the post before mine. I saw this thread in the MapleStory section, so I came straight here to answer. I guess I should've read the other posts, my mistake. |
|
| Back to top |
|
 |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
Posted: Sun Aug 17, 2008 10:43 pm Post subject: |
|
|
| HalfPrime wrote: | This is very odd and I have no idea why it's happening, but, after the cins, the next cin.get() is completely ignored. If you put 2 cin.get()s, it'll pause...very odd.
btw, you don't need to make number1 and number2 global since you're passing them to the function directly. |
The '\n' character is still in the input buffer, as cin doesn't clear it, and therefore cin.get() just removes the newline character from the stream and returns. |
|
| Back to top |
|
 |
HalfPrime Grandmaster Cheater
Reputation: 0
Joined: 12 Mar 2008 Posts: 532 Location: Right there...On your monitor
|
Posted: Mon Aug 18, 2008 9:30 am Post subject: |
|
|
Oh, thanks. That makes sense. _________________
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
|
|