![]() |
|
Welcome to the Computer Webmaster Gaming Console Graphics Forum forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact contact us. |
| |||||||
| Software Programming Software programming talk, ask questions about computer software programming or help others |
![]() |
| | LinkBack | Thread Tools | Display Modes |
| | #1 | ||
| On Mon, 30 Jun 2003 23:54:07 GMT, "Josh Dawg" <slopeking7@nospam-earthlink.net> wrote: >I took a C++ turtorial and it said that the coding for displaying text is: > >cout<<"what ever you want to write"; > >but it won't work for me, is there something wrong in the coding or is my >compiler messed up? I can't believe no one hit this obvious problem: cout is within the namespace std - your problem is that you should have written: #include <iostream> int main() { std::cout << "whatever you want to write" << std::endl; //the std::endl may not be necessary (it skips a line and forces cout //to display (iirc)) //you can avoid writing std:: before every command if you type //using namespace std; right after the #include statement but //that's bad programming practice - a better way might be to //type using std::cout; and using std::endl; - now your only line of //code can be: // cout << "whatever you want to write" << endl; return 0; } Alex atheist #2007 | |||
| Advertisements |
| | #2 | ||
| whoever thought that stream style's format was an improvement over good ole printf() must of been either smoking something or trying to justify their job. Alex wrote: > On Mon, 30 Jun 2003 23:54:07 GMT, "Josh Dawg" > <slopeking7@nospam-earthlink.net> wrote: > > >I took a C++ turtorial and it said that the coding for displaying text is: > > > >cout<<"what ever you want to write"; > > > >but it won't work for me, is there something wrong in the coding or is my > >compiler messed up? > > I can't believe no one hit this obvious problem: > > cout is within the namespace std - your problem is that you should > have written: > > #include <iostream> > > int main() > { > std::cout << "whatever you want to write" << std::endl; > //the std::endl may not be necessary (it skips a line and forces cout > //to display (iirc)) > > //you can avoid writing std:: before every command if you type > //using namespace std; right after the #include statement but > //that's bad programming practice - a better way might be to > //type using std::cout; and using std::endl; - now your only line of > //code can be: > // cout << "whatever you want to write" << endl; > > return 0; > } > > Alex > atheist #2007 | |||
| | #3 | ||
| On Wed, 02 Jul 2003 07:34:41 GMT, Eternal Vigilance <wotan@oneeye.com> wrote: >whoever thought that stream style's format was an improvement over good ole >printf() >must of been either smoking something or trying to justify their job. Are you able to overload printf() so that you can do the following (real question since I never used "good ole printf()" since it was before my time)? myClass myObject; //assign values to myObject's members here cout << myObject; ============== SCREEN RESULTS my1stVar = 32 my2ndVar = The rain in Spain is wet. my4thVar = -2 Alex atheist #2007 | |||
| | #4 | ||
| look up the function its in <stdio.h> int printf( const char *format [, argument]... ); ^^^^^ format string can be a Var itself so you can change the format in program...... printf("text %d <- int %s<- string %x<- int as hex <cr><lf> ---> \n", var1,string,var2); fprintf similar but to a file...... sprintf similar but outputs to a char array (string) I use them mostly for building error strings for output or logfile entries (shorter and more readable codewise) (like another poster said it is a bit slower than explicit string building itoa() strcat() etc.... ) you could use printf in your overloaded << operator to output what you want for object data dump sprintf(&outstr[0]," Object[%d] X=%f Y=%f Z=%f HP=%d /n", objx->index, objx->xPos, objx->yPos, objx->zPos, objx->HP); outstr[] contains: Object[23] X=4.3434 Y=56.5555 Z=1.0000 HP=100 Alex wrote: > On Wed, 02 Jul 2003 20:34:59 GMT, "Tony Di Croce" > <iaretony@hotmail.com> wrote: > > >No, but to be fair, you had to OVERLOAD the operator... The implementation > >is somewhere... > > Yes, that's the core of my question: can you overload printf()? > > Alex > atheist #2007 | |||
| | #5 | ||
| www.dcs.shef.ac.uk/~u0ke "Alex" <support@microsoft.com> wrote in message news:7n68gvo0r88itqlk8k5j2isc61682hdj94@4ax.com... > On Thu, 03 Jul 2003 10:46:56 GMT, Eternal Vigilance <wotan@oneeye.com> > wrote: > > >you could use printf in your overloaded << operator to output what you want > >for object data dump > > But could you do printf(myObject); and print out the member variables > in a nice little format? > > > Alex > atheist #20 Are you trying to do something like in java wher eyou can overload an objects 'toString()' method? i.e myObject.printf()? -- Kemal Enver u0ke@dcs.shef.ac.uk | |||
| | #6 | ||
| On Thu, 3 Jul 2003 22:10:13 +0100, "Kemal Enver" <kemalenver@f2s.com> wrote: >Are you trying to do something like in java wher eyou can overload an >objects 'toString()' method? i.e myObject.printf()? I'm asking if it's possible. Not sure about the Java. In the end I don't really need to know, I'm just exploring the advantages of cpp's cout << as opposed to the printf() function. I'm not even sure if this is the best advantage - it probably isn't - since I know close to nothing about C. Alex atheist #2007 | |||
| Featured Websites | ||||
|
![]() |
| Tags: doing, right |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
| |
| Featured Websites | ||||
|