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

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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