Writing C#'s Console::WriteLine in C++

This is a discussion on Writing C#'s Console::WriteLine in C++ within the C++ Programming forums, part of the General Programming Boards category; There is something that I would really enjoy in C++ if it is possible. Code: class Console { public: template ...

  1. #1
    Registered User philvaira's Avatar
    Join Date
    Apr 2004
    Posts
    72

    Writing C#'s Console::WriteLine in C++

    There is something that I would really enjoy in C++ if it is possible.

    Code:
    	class Console
    	{
    	public:
    		template <class T> static void WriteLine(T text)
    		{
    			std::cout << text << std::endl;
    		}
    
    		template <class T> static void Write(T text)
    		{
    			std::cout << text;
    		}
    	};
    This works for a single data type. However, what would I do if I had something like this?

    float price = 5.25f;
    Console::WriteLine("The price is " + price + "."); // outputs The price is 5.25.

    I would assume I need to overload the operator '+' somehow within this and concatenate everything together into a final string. I know C# isn't C++, and visa-versa, but for some programming tasks this would save some work. I learned not to ever use std::cout directly as much as Java's System.out.println(). Instead they should be in wrappers so we can change output in one method instead of changing it throughout the entire project. This also allows us to output to a GUI, file, or whatever later on if we ever wished. So something above would be ideal and better abstracted, although I'm not sure how to go about something like it.

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    If you really need a way to easily swap io functionality mid-program, try the following:

    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
      ostream *output;
    
      // Let's do some output to the console:
      output = &cout;
      *output << "Hey!" << endl;
    
      // Now let's output to a file instead:
      ofstream file("tmp.txt");
      output = &file;
    
      *output << "Hey!" << endl;
    
      file.close();
    
      return 0;
    }

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    You could try declaring a global reference to an ostream object - then only this reference would need to change when you wanted to switch the code from using cout to something else (Since cout is already a global variable, it won't matter having a global reference to it instead)

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    you can overload <<
    or you can use stringstream to construct your line before calling WriteLine
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    And of course, the sick macro solution:

    Code:
    #define WriteLine(x) std::cout << x
    You'll have to use "<<" instead of "+" to join elements.

    Code:
    WriteLine("This is " << "Ill advised.");

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-20-2008, 08:57 AM
  2. Very slow file writing of 'fwrite' function in C
    By scho in forum C Programming
    Replies: 6
    Last Post: 08-03-2006, 02:16 PM
  3. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 08:44 AM
  4. help! fifo read problem
    By judoman in forum C Programming
    Replies: 1
    Last Post: 08-16-2004, 09:19 AM
  5. The Art of Writing Comments :: Software Engineering
    By kuphryn in forum C++ Programming
    Replies: 15
    Last Post: 11-23-2002, 04:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21