printf to cout

This is a discussion on printf to cout within the C++ Programming forums, part of the General Programming Boards category; the code is Code: #include <stdio.h> void main() { int i, j; char a[3][4] = {"you", "my", "luv"}; for(i = ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    1

    Thumbs up printf to cout

    the code is
    Code:
    #include <stdio.h>
    
     
    
    void main()
    
    {
    
          int i, j;
    
          char a[3][4] = {"you", "my", "luv"};
    
      for(i = 0; i <= 2; i = i + 1)
    
    		{
    
                for(j = 0; j <= 3; j = j + 1)
    
                      printf("a[%d][%d] = %c\t", i, j, a[j][i]);
    
                printf("\n");
    
    		}
    
    }
    result http://img402.imageshack.us/img402/4271/40674082yj2.jpg

    My question is how can i print this using cout rather than printf but i don't want it to print a[0][0], a[0][0] etc
    when i run the code i just want to see this
    yml
    uyo
    u v
    thanks

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Well, a[j][i] is the character to be printed in the loop iteration. If you know how to print things with cout, it should be easy from there.

    However, the code as it is is bad. for(j = 0; j <= 3; j = j + 1) will lead to to a[1][3] being accessed, and that's invalid.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Your code managed to flip flop your counters there. i and j got switched for some reason.

    cout:
    Code:
    #include <iostream>
    
     
    
    void main()
    
    {
    
          int i, j;
    
          char a[3][4] = {"you", "my", "luv"};
    
      for(i = 0; i <= 2; i = i + 1)
    
    		{
    
                for(j = 0; j <= 3; j = j + 1)
                      std::cout << a[i][j];
    
                         std::cout << std::endl;
    		}
    
    }
    Last edited by master5001; 04-26-2008 at 12:15 PM. Reason: I just noticed you did NOT want the array indices printed

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    It's still an out-of-bounds access.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    True, but not in some catastrophic way. But listen to the lady, mostly because programming in such a half hazard way is slippery slope.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >It's still an out-of-bounds access.
    I don't see how it would be.

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    why is [1][3] out of bounds? if the array is [3][4].
    [0][0]
    [0][1]
    [0][2]
    [0][3]
    etc, etc

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Sorry, my fault. It's indeed a char[3][4], and not a char*[3], as I had thought. I confused it with another thread.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Although in one respect you're right, as some j's would be outside the string, and as a result the cout would print the string terminator. The OP may want to look up strlen(), or better yet use std::string.

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Note that void main() is a bad idea: cpwiki.sf.net/void_main
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  2. making it portable.....?
    By ShadeS_07 in forum C Programming
    Replies: 11
    Last Post: 12-24-2008, 08:38 AM
  3. Need Help with a Bowling Score Program
    By oobootsy1 in forum C++ Programming
    Replies: 6
    Last Post: 11-01-2005, 09:04 AM
  4. Whats Wrong Whith This!?
    By SmokingMonkey in forum C++ Programming
    Replies: 8
    Last Post: 06-01-2003, 09:42 PM
  5. (printf VS cout) and (including libraries)
    By \cFallen in forum C++ Programming
    Replies: 3
    Last Post: 01-20-2003, 08:47 PM

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