Like Tree3Likes
  • 1 Post By whiteflags
  • 2 Post By laserlight

C++ Outputing Formats:

This is a discussion on C++ Outputing Formats: within the C++ Programming forums, part of the General Programming Boards category; I'm having a difficult time changing my codes from C to C++. For C: Code: printf("%2d/%-2d ", month, day); printf("%02d:%02d ...

  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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,738
    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.
    iMalc likes this.

  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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,738
    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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,738
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,782
    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
    21,914
    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.
    Elysia and King Mir like this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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

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