Char Array Initialization and Display

This is a discussion on Char Array Initialization and Display within the C++ Programming forums, part of the General Programming Boards category; First off, thank you for your earlier tips, I learned alot and one of those things is to approach my ...

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    6

    Char Array Initialization and Display

    First off, thank you for your earlier tips, I learned alot and one of those things is to approach my code a step at a time.The following is something I am not clear about. Multi dimensional char arrays and the displaying of them.
    Code:
    #include <iostream>
    
    
    using namespace std;
    
    
    main()
    {
    
    
      //char test[5][5] = {'***\0','***\0','***\0','***\0','***\0'};
        char test[5][5] = {"***\0","***\0","***\0","***\0","***\0"};
    	cout << test << endl;
    	cout << char[2][2] << endl;
    
    
    	cin.get();
    	return 0;
    
    
    }
    The commented out expression didn't run at all but the double quotation mark one did, unfortunately, it gives me a hexadecimal display. How can I get it to display like this:

    *****
    *****
    *****
    *****
    *****

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    In C++, we use the string class for handling strings. Try again
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    The first failed because you're trying to initialize a 2d array of chars by stuffing more than a single character into an index position.

    Code:
    char test[5][5] = 
    {
        { '*', '*', '*', '*', '\0' },
        { ... },
        { ... },
        { ... },
        { ... }
    };
    The second is being treated as 5 strings capable of holding up to 4 characters + 0 to terminate them... sort of.

    cout test[2][2], wants to print a single character; the 3rd character of the 3rd string. to print the lines, use a loop

    Code:
    for(int i=0; i < 5; ++i)
        cout << test[i] << "\n";
    To print the lines as you'd like, your declaration should be

    Code:
    char test[5][6] = {  "*****", "*****", "*****", "*****", "*****" };
    
    loop
    Using the string class is probably a good idea.

  4. #4
    Registered User
    Join Date
    May 2013
    Posts
    6
    Awesome! I ran through the suggestions and modified my code and it works like a charm but when continuing on and adding to my code, I added a loop to write to a .txt file and it won't work. Here is my code. No erors, just doesnt write. TXT file was pre created prior to compiling.
    Code:
    #include <iostream>
    #include <fstream>
    
    
    using namespace std;
    
    
    main()
    {
    	ofstream outputFile;
    	outputFile.open("grid.txt");
    	
        char test[5][5];
    	
    	for(int i = 0; i < 5; i++) 
    	{
    		for(int j = 0; j < 5; j++) 
    		{
            test[i][j] = '*';
    		}
    	}
    	
    	for(int b = 0; b < 5; ++b && cout << endl)
    	{
    		for(int a=0; a < 5; ++a)
    		outputFile << test[b][a];
    	}
    	
    	for(int b = 0; b < 5; ++b && cout << endl)
    	{
    		for(int a=0; a < 5; ++a)
    		cout << test[b][a];
    	}
    	
    	
    	
    	outputFile.close();
    
    
    	cin.get();
    	return 0;
    
    
    }

  5. #5
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    you should check that your file is open before you try and read/write to it.

    Code:
    if(outputFile.is_open())
    { 
         ...
    }
    I don't use cpp often, but I got an output file using

    Code:
    int i, j;
    ...
    
    if(outputFile.is_open())
    {
        for(i=0; i < 5; ++i)
        {
             for(j=0; j < 5; ++j)
             {
                  test[i][j] = '*';
                  cout << test[i][j];
                  outputFile << test[i][j];
             }
             cout.flush() << "\n";
             outputFile.flush() << "\n";
        }
    
        outputFile.close();
    }
    ...

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    You are still no using std::string and furthermore, you must declare the return type of main to be int. All functions must have a return type--omitting it is a compile error.
    And, unlike other flawed languages, in C++ you don't have to call .close(). It does it implicitly for you when it goes out of scope! Learn to rely on this mechanism, because it is key in C++.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. (char) array initialization, size etc.
    By cnewbie1 in forum C Programming
    Replies: 6
    Last Post: 06-21-2011, 07:08 AM
  2. Need Help, cannot get char to display A+,F-, etc.
    By slickwilly440 in forum C++ Programming
    Replies: 7
    Last Post: 03-23-2011, 03:06 AM
  3. initialization of char array (c style string)
    By manzoor in forum C++ Programming
    Replies: 1
    Last Post: 09-24-2008, 07:29 AM
  4. Char array display error...
    By Karpaty in forum C Programming
    Replies: 23
    Last Post: 10-31-2007, 08:17 AM
  5. Initialization of Char Pointers?
    By Kleid-0 in forum C Programming
    Replies: 14
    Last Post: 01-07-2005, 10:41 PM

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