Thread: while does this 2d char array (almost) work in C , but not even close in C++?

  1. #1
    Banned
    Join Date
    Aug 2017
    Posts
    861

    while does this 2d char array (almost) work in C , but not even close in C++?

    printing out a 2d char array, In C++ it just wacks the hell out of it, and comes up with its own abbreviations for what is in the array, and in C it chops off part of the last word in the first element being printed. NOT using element 0, btw.

    What would be a better way to print out various sizes in an array like this?

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
        char snacks[7] [30] = { " ", "Cracker Jacks","Gummy Bears","Lemon Heads",
                                "Skittles","Snickers","Milky Way"};
    
     
    size_t count = 1, col = 0;
    
    for ( ; count < 7 ; count++)
    {
        for (; col != '\n'; col++)
        {
            cout <<snacks[count][col]; 
            col++;
        }
        cout<<endl;
        col = 0;
    }
        //same code compiled in C as well 
        //gets same output
        printf("\ntime two\n"
        "C code\n\n");
        
        count = 1, col = 0;
        
        for ( ; count < 7 ; count++)
        {
            for (; col != '\n'; col++)
            {
                printf("%c", snacks[count][col]);  
            }
            printf("\n");
            col = 0; //reset
        }
    
    return 0;
    }
    Results
    Code:
    userx@slackwhere:~/bin
    $ ./temp
    CakrJ
    GmyBa
    LmnHa
    Site
    Sikr
    MlyWy
    
    time two
    C code
    
    Cracker Ja
    Gummy Bear
    Lemon Head
    Skittles
    Snickers
    Milky Way
    Last edited by userxbw; 12-16-2017 at 01:08 PM.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,482
    Well I'd say the C++ version should be more like:
    Code:
    ...
        std::vector<std::string> snacks= { "Cracker Jacks", "Gummy Bears", "Lemon Heads",
                                "Skittles", "Snickers", "Milky Way"
                              };
    
        for(auto& itr : snacks)
        {
            for(auto& it : itr)
                std::cout << it;
            std::cout << '\n';
        }
    ...
    And the C version should be more like:

    Code:
    ...
    #define NUM_ELEMENTS 6
    
        char snack[NUM_ELEMENTS][30] = { "Cracker Jacks", "Gummy Bears", "Lemon Heads",
                                "Skittles", "Snickers", "Milky Way"
                              };
    
        for(size_t counter = 0; counter < NUM_ELEMENTS; ++counter)
        {
            for(size_t len = 0; len < strlen(snacks[counter]); ++len)
               printf("%c",snacks[counter][len]);
            printf( "\n");
        }
    By the way for someone who considers himself an expert your code contains quite a few beginner mistakes.
    Last edited by jimblumberg; 12-16-2017 at 02:22 PM.

  3. #3
    Banned
    Join Date
    Aug 2017
    Posts
    861
    ...........
    Last edited by userxbw; 12-16-2017 at 03:00 PM.

  4. #4
    Banned
    Join Date
    Aug 2017
    Posts
    861
    yeah < > I had to switch gears,
    Code:
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    int main()
    {
        char snacks[7] [30] = { " ", "Cracker Jacks","Gummy Bears","Lemon Heads",
                                 "Skittles","Snickers","Milky Way"};
        
        vector < string > snackos =
         { " ", "Cracker Jacks","Gummy Bears","Lemon Heads",
            "Skittles","Snickers","Milky Way"};
    
     
    size_t count = 1, col = 0;
    
    for ( ; count < 7 ; count++)
        cout <<snackos[count]<<endl;  
             
     
        //same code compiled in C as well 
        //gets same output
        printf("\ntime two\n"
        "C code\n\n");
        
        count = 1, col = 0;
        
        for ( ; count < 7 ; count++)
        {
            for (; col != '\n'; col++)
            {
                printf("%c", snacks[count][col]);  
            }
            printf("\n");
            col = 0; //reset
        }
    
    return 0;
    }


    Quote Originally Posted by jimblumberg View Post

    By the way for someone who considers himself an expert your code contains quite a few beginner mistakes.
    that is an assumption on your part, period.

    go look at every post in here Not one time have I ever made that claim, that I am an expert nor to I consider myself an expert in programming.
    I have actually set the record straight in one post for the record. and No I am not going to look for it.


    Code:
    $ ./temp
    Cracker Jacks
    Gummy Bears
    Lemon Heads
    Skittles
    Snickers
    Milky Way
    
    time two
    C code
    
    Cracker Ja
    Gummy Bear
    Lemon Head
    Skittles
    Snickers
    Milky Way
    Last edited by userxbw; 12-16-2017 at 03:05 PM.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,482
    Do you realize that your C++ version is no longer doing the same thing as the C version, even if you fix the logic errors in the C version?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,675
    I was going to add something but it appears bx knows everything.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by Salem View Post
    I was going to add something but it appears bx knows everything.
    appearances can be deceiving.

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Code:
    for ( ; count < 7 ; count++)
    {
        for (; col != '\n'; col++)
        {
    Perhaps you meant to make the middle term snacks[count][col] != '\0' considering how your data looks. col != '\n'; just means col != 10 in this context.

    printing out a 2d char array, In C++ it just wacks the hell out of it, and comes up with its own abbreviations for what is in the array, and in C it chops off part of the last word in the first element being printed. NOT using element 0, btw.
    Before you blame the programming language, remember PEBCAC

  9. #9
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by jimblumberg View Post
    Do you realize that your C++ version is no longer doing the same thing as the C version, even if you fix the logic errors in the C version?
    I have no idea how it works under the hood. I only know it is now acting like a 1d array because one no longer has to reference both whatever them [ ] <<--- are called.

    Code:
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    int main()
    {
        vector < string > snackos =
            { " ", "Cracker Jacks","Gummy Bears","Lemon Heads",
            "Skittles","Snickers","Milky Way"};
    int count = 1;
    
    for ( ; count < 7 ; count++)
        cout <<snackos[count]<<endl;   
        cout<<"\n"<<snackos[4]<<endl;
    
        //last line prints out the 5th element starting from  zero,
        // after the loop has ened. 
        //Is it properly placed within the code?
        // Some would say no. But the compiler won't complain about it as
        //much as some humans might.
    return 0;
    }
    that much I know.
    Last edited by userxbw; 12-16-2017 at 03:31 PM.

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    4,482
    I have no idea how it works under the hood.
    Well you finally admit that you don't know something, amazing.

    The C++ version is now printing strings, the C version is trying to print characters. I really don't understand why you can't see the difference.

    //Is it properly placed within the code?
    As long as you realize that the indentation is misleading, yes, otherwise no.

    // Some would say no. But the compiler won't complain about it as
    //much as some humans might.
    Actually most modern compilers can warn you about the misleading indentation, if you tell them to.

    that much I know.
    Not much then.

  11. #11
    Informer -Adrian's Avatar
    Join Date
    Jan 2013
    Posts
    830
    As long as you realize that the indentation is misleading, yes, otherwise no.
    Yes, it is a ........ing eyesore (every time). But I found that people who don't care about structure/order never improve in this regard. It's deeply ingrained like one's preference of vanilla or chocolate and logical arguments have little effect.

  12. #12
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by jimblumberg View Post
    Well you finally admit that you don't know something, amazing.
    whatever.... never said I knew everything, that was your ill guided mind thinking

    Quote Originally Posted by jimblumberg View Post

    The C++ version is now printing strings, the C version is trying to print characters. I really don't understand why you can't see the difference.
    technicalities, cheep shot.
    ..
    Quote Originally Posted by jimblumberg View Post
    As long as you realize that the indentation is misleading, yes, otherwise no.
    yep

    Quote Originally Posted by jimblumberg View Post
    Actually most modern compilers can warn you about the misleading indentation, if you tell them to.
    yep

    Quote Originally Posted by jimblumberg View Post

    Not much then.
    nope, I know a lot about some stuff and some about other stuff and nothing about what I do not know about, but I do not know everything. My mind is not big enough to hold all of that information.

    just like you.

    I never claimed to be a know it all, maybe that was just projecting your self image on to me. it is a subconscious action you know.

    So no, you have not bruised my ego any, none whatsoever by that last remark.

    though I do know enough about programming to write what I need to write to get done what I need to get done in my program I maintain.

    Bjarne Stroustrup that said on being an expert in C++. that'd take more than a lifetime. it is better to just take it one piece at a time and learn how to be good at that. , paraphrased

    Maybe you see yourself as an expert but I do not.
    .
    Last edited by userxbw; 12-16-2017 at 05:17 PM.

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,675
    Bored of your constant ........y attitude whenever anyone tries to help your sorry ass.
    ........ off.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,675
    There, I've put you out of our misery.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assigning a char value to char pointer doesn't work.
    By inckka in forum C Programming
    Replies: 5
    Last Post: 03-15-2017, 04:34 AM
  2. Replies: 2
    Last Post: 09-25-2014, 06:12 AM
  3. Replies: 2
    Last Post: 09-25-2014, 04:03 AM
  4. Help altering code to work with char array
    By mikeman in forum C++ Programming
    Replies: 3
    Last Post: 02-16-2010, 07:59 PM
  5. Replies: 3
    Last Post: 11-17-2008, 12:36 PM

Tags for this Thread