print char array

hi

i have a problem when print char array:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <cstring>

using namespace std;

int main()
{

    char x[10];
    char name[10][10] ;
    for(int i = 0 ; i < 10 ; i++)
    {
        cout << "Name " << i+1 << " : " ;
        cin.get(x,10,'\n');
        for(int j = 0 ; j < 10  ; j++)
        {
            name[i][j]=x[j];
        }
        cin.ignore(10,'\n');
    }

    //print

    for(int i=0 ; i<10 ; i++)
    {
        cout << "Name " << i+1 << " :" ;
        for(int j = 0 ; j < 10 ; j++)
        {
            cout << name[i][j];
        }
        cout << endl ;
    }


    return 0;
}


ex :
http://postimg.org/image/4cd4u960n
I can't view the screenshot for some reason. What problem are you having? The code appears to work.
There is a character at the end of each word !

ex : google♀

http://axgig.com/images/58041226632272381678.png
closed account (18hRX9L8)
That's because a string (char*) always has the characters and then the ending character (null). So for example:

You think this string contains:
hello

but it really contains:
hello[NULL]

so when printing a char* just use:

std::cout<<mychar;

that does the same thing as what you are trying to do but it formats it properly.
Last edited on
closed account (3qX21hU5)
Or just start using strings ;p

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main()
{
    vector<string> names;
    string name;

    for (int i = 0; i != 10; ++i)
    {
        cout << "Name " << i + 1 << ": ";
        cin >> name;
        names.push_back(name);
    }

    for (auto i = 0; i != names.size(); ++i)
        cout << "Name " << i  + 1<< ": " << names[i] << endl;
        

    return 0;
}
closed account (18hRX9L8)
Yes that is a good idea.
Whats the program supposed to be doing anyway?
It is a simple program.
Search words by first character...
closed account (3qX21hU5)
So you enter words then you enter a letter to search for and it prints all the words that start with that letter?

Let us know if you run into anymore problems you need help on. We would be more then willing to help out.
Topic archived. No new replies allowed.