Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


[C++] printf Vs. cout

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
Nuclear898
Grandmaster Cheater Supreme
Reputation: 0

Joined: 04 Jun 2006
Posts: 1597
Location: The Netherlands

PostPosted: Thu Jul 10, 2008 5:21 am    Post subject: [C++] printf Vs. cout Reply with quote

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
View user's profile Send private message
kitterz
Grandmaster Cheater Supreme
Reputation: 0

Joined: 24 Dec 2007
Posts: 1268

PostPosted: Thu Jul 10, 2008 6:40 am    Post subject: Reply with quote

They are both the same, but I think printf lets you specify the type....personally I perfer cout better.
Back to top
View user's profile Send private message Send e-mail
Symbol
I'm a spammer
Reputation: 0

Joined: 18 Apr 2007
Posts: 5094
Location: Israel.

PostPosted: Thu Jul 10, 2008 7:13 am    Post subject: Reply with quote

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. Razz
Back to top
View user's profile Send private message
Nuclear898
Grandmaster Cheater Supreme
Reputation: 0

Joined: 04 Jun 2006
Posts: 1597
Location: The Netherlands

PostPosted: Thu Jul 10, 2008 8:47 am    Post subject: Reply with quote

Thanks symbol, I already started using printf anyway Very Happy

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
View user's profile Send private message
Heartless
I post too much
Reputation: 0

Joined: 03 Dec 2006
Posts: 2436

PostPosted: Thu Jul 10, 2008 12:31 pm    Post subject: Reply with quote

Symbol wrote:
...
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. Razz


I ran both and to the human eye I didn't notice a difference. Then I got Superman and the Flash and they confirmed that printf was faster.

I like to use printf because they can make ASCII symbols.

_________________
What dosen't kill you, usually does the second time.
Back to top
View user's profile Send private message
Noz3001
I'm a spammer
Reputation: 26

Joined: 29 May 2006
Posts: 6220
Location: /dev/null

PostPosted: Thu Jul 10, 2008 12:33 pm    Post subject: Reply with quote

printf is C cout is C++
Back to top
View user's profile Send private message MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites