 |
Cheat Engine The Official Site of Cheat Engine
|
| View previous topic :: View next topic |
| Author |
Message |
Nuclear898 Grandmaster Cheater Supreme
Reputation: 0
Joined: 04 Jun 2006 Posts: 1597 Location: The Netherlands
|
Posted: Thu Jul 10, 2008 5:21 am Post subject: [C++] printf Vs. cout |
|
|
| Code: | int roflcopter;
roflcopter = 10101010101;
printf("%i",roflcopter);
cout << roflcopter; |
it both does the same, but which ostream is better and why?
I can use both, just don't know which one I should use. |
|
| Back to top |
|
 |
kitterz Grandmaster Cheater Supreme
Reputation: 0
Joined: 24 Dec 2007 Posts: 1268
|
Posted: Thu Jul 10, 2008 6:40 am Post subject: |
|
|
| They are both the same, but I think printf lets you specify the type....personally I perfer cout better. |
|
| Back to top |
|
 |
Symbol I'm a spammer
Reputation: 0
Joined: 18 Apr 2007 Posts: 5094 Location: Israel.
|
Posted: Thu Jul 10, 2008 7:13 am Post subject: |
|
|
printf is good for printing long strings with information, its easier to do:
| Code: | | printf("A value is: %d\nA address is: 0x%X\nB value is: %d\nB address is: 0x%X\nSum of A + B: %d", a, &a, b, &b, a + b); |
Than:
| Code: | | cout << "A value is: " << a << "\nA address is: " << &a << "\nB value is: " << b << "\nB address is: " << &b << "\nSum of A + B: " << a + b; |
cout is also very slow:
| Code: | int time = GetTickCount();
for (int i = 1; i <= 1000; ++i)
cout << i << endl;
time = GetTickCount() - time;
cout << time << "Milliseconds.\n" |
While printf is way faster:
| Code: | int time = GetTickCount();
for (int i = 1; i <= 1000; ++i)
printf("%d\n", i);
time = GetTickCount() - time;
printf("%d Milliseconds\n", time); |
Run both programs and see that printf is faster.
Note that without "endl", cout is much faster. (also without '\n', cout/printf is a bit faster, but thats because its 1 character less, 1000 in the whole loop)
Use printf.  |
|
| Back to top |
|
 |
Nuclear898 Grandmaster Cheater Supreme
Reputation: 0
Joined: 04 Jun 2006 Posts: 1597 Location: The Netherlands
|
Posted: Thu Jul 10, 2008 8:47 am Post subject: |
|
|
Thanks symbol, I already started using printf anyway
Edit;
Hmm I'm having a little trouble now.
I try to use printf for a string but it shows NULL when executed
when I use cout it works just fine.
| Quote: | #include <iostream>
#include <string>
using namespace std;
double a,b,result,product;
int menuinput, endinput;
string menustring;
double addition(double a,double b)
{
product = a + b;
return (product);
}
double multiplication(double a,double b)
{
product = a * b;
return (product);
}
double division(double a,double b)
{
product = a / b;
return (product);
}
double subtraction(double a,double b)
{
product = a - b;
return (product);
}
int main()
{
system("CLS");
printf("Choose Option: \n1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\nOption:");
cin >> menuinput;
if (menuinput < 1 || menuinput > 4)
{
printf("\nERROR :: Wrong Input!\n\n");
system("pause");
main();
}
printf("\nChoose numbers to");
switch (menuinput)
{
case 1: menustring =" add"; break;
case 2: menustring =" subtract"; break;
case 3: menustring =" multiply:"; break;
case 4: menustring =" divide:"; break;
}
cout << menustring << "\nNumber 1: "; //This is where printf shows NULL
//printf("%s\nNumber1: ",menustring) // <-- This printf
cin >> a;
printf("Number 2: ");
cin >> b;
switch (menuinput)
{
case 1: result = addition(a,b); break;
case 2: result = subtraction(a,b); break;
case 3: result = multiplication(a,b); break;
case 4: result = division(a,b); break;
default:
printf("\nERROR :: Wrong input!\n\n");
system("pause");
main();
break;
}
printf("\n%f\n\nInsert 1 to do another calculation\nor insert 0 to exit\n",result);
cin >> endinput;
if (endinput == 1) main();
else if (endinput != 1) return 0;
return 0;
} |
|
|
| Back to top |
|
 |
Heartless I post too much
Reputation: 0
Joined: 03 Dec 2006 Posts: 2436
|
|
| Back to top |
|
 |
Noz3001 I'm a spammer
Reputation: 26
Joined: 29 May 2006 Posts: 6220 Location: /dev/null
|
Posted: Thu Jul 10, 2008 12:33 pm Post subject: |
|
|
| printf is C cout is C++ |
|
| 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
|
|