Thread: C++ Outputing Formats:

  1. #1
    Registered User
    Join Date
    May 2013
    Location
    Dallas, Texas
    Posts
    8

    C++ Outputing Formats:

    I'm having a difficult time changing my codes from C to C++.

    For C:

    Code:
    printf("%2d/%-2d ", month, day);
    
    printf("%02d:%02d ", hour, minute);
    
    printf("%-15s %s\n", subject, location);
    How do you convert the C codes into C++ with the same outputting format?

    Thanks

  2. #2
    neptune whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    8,914
    Code:
    #include <iostream>
    #include <iomanip>
    
    int main()
    {
      std::cout << std::setw(2) << month << '/' << std::left << std::setw(2) << day;
      std::cout << std::setw(2) << std::setfill('0') << hour << ':' << std::setw(2) << minute;
      std::cout << std::setfill(' ');
      std::cout << std::left << std::setw(15) << subject << ' ' << location;
    }
    http://www.cplusplus.com/reference/iomanip/
    Last edited by whiteflags; 05-15-2013 at 06:48 PM.

  3. #3
    Registered User
    Join Date
    May 2013
    Location
    Dallas, Texas
    Posts
    8
    This is what I did:

    Code:
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    int main()
    {
    ..
        cout << setw(2) << day.month << "/" << left << setw(2) << day.day << endl;
        cout << setw(2) << time.hour << ":" << setw(2) << time.minute << endl;
        cout << setw(16) << left << appt->subject << appt->location << endl;
    ...
    }
    Still not the same format? Any ideas?

  4. #4
    neptune whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    8,914
    Should be it - explain what's missing.

  5. #5
    Registered User
    Join Date
    May 2013
    Location
    Dallas, Texas
    Posts
    8
    This is my output when I convert it to C++:

    Start End Subject Location

    9: 010:30Team CAFE CA
    11:0 12:30Team CAFE NY
    13:0 18:0 CAFE CAFE TX


    This is what it should look like:

    Start End Subject Location
    09:00 10:30 Team CAFE CA
    11:00 12:30 Team CAFE NY
    13:00 18:00 CAFE CAFE TX

  6. #6
    Registered User
    Join Date
    May 2013
    Location
    Dallas, Texas
    Posts
    8
    *** Ignore the top. Below is the correct format.

    This is my output when I convert it to C++:

    Start End Subject Location

    9: 010:30Team CAFE CA
    11:0 12:30Team CAFE NY
    13:0 18:0 CAFE CAFE TX


    This is what it should look like:

    Start End Subject Location
    09:00 10:30 Team CAFE CA
    11:00 12:30 Team CAFE NY
    13:00 18:00 CAFE CAFE TX
    Last edited by inkjoy00; 05-15-2013 at 07:21 PM.

  7. #7
    neptune whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    8,914
    Well I notice you aren't using setfill, and you have to. I showed you how in my example.

  8. #8
    Registered User
    Join Date
    May 2013
    Location
    Dallas, Texas
    Posts
    8
    Thanks. That does solve the problem.

    Another quick question:

    I have
    Code:
    cout << "Start   End             Subject      Location";
    as my first line, but how do I get it to be the same spacing as the day/month , hour/minute , subject and location?

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    24,361
    Perhaps if you used the same values for setw for the same columns?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    25,346
    You might want to consider the use of Boost.Format.

    Quote Originally Posted by inkjoy00
    how do I get it to be the same spacing as the day/month , hour/minute , subject and location?
    One way is to decide on the field widths and set them accordingly for the headers too.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Spreadsheet I/O formats
    By Else in forum C++ Programming
    Replies: 5
    Last Post: 08-21-2009, 05:29 PM
  2. Need help on outputing.....
    By tx1988 in forum C++ Programming
    Replies: 7
    Last Post: 04-17-2009, 12:03 PM
  3. C99 formats
    By KIBO in forum C Programming
    Replies: 4
    Last Post: 01-17-2008, 08:13 AM
  4. Sprites and different formats?
    By Tommaso in forum Game Programming
    Replies: 6
    Last Post: 10-31-2002, 11:33 PM
  5. File Formats
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 03-11-2002, 11:25 PM