Char Help!!! PLEASE

Hi. I am pretty new to the C++ language and I need some help. Int makes number variables and char makes letter variables. I have been looking at codes online and figured out that char[number] allows words to be entered. Without the number, only letters will work. So, if someone can tell me what the number does, I thank you in advance.

Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>

using namespace std;

int main()
{
    char word[10];

    cout << "Enter a word:" << endl;
    cin >> word;
    cout << word << endl;
}


BTW, sorry if I posted in the wrong section. Tell me where to post it so I can do it next time.
Last edited on
What you're declaring is an array of characters. So, in your example, you've created an array of characters that can be up to 10 letters in length.
well if i use the number 2, and enter apple which is 5 letters long, apple still prints, not ap.
closed account (3TkL1hU5)
The numbers are initializing the number of elements in a character array.

See here for information about arrays. http://www.cplusplus.com/doc/tutorial/arrays/

In order to input a 'word', you would want to make a object of class type string.

Information about the string class can be found here: http://www.cplusplus.com/reference/string/string/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

int main()
{

string string;

cout << "Input a word: ";
cin >> string;
cout << "\n Your word is: " << string << endl;

return 0;
}

For clarification:

 
char word[10];


means that word is NOT a char, but instead 10 chars stored in sequence. Oh, and char is a number type as well - usually 8 bits long (meaning, it can take on 256 distinct values).
thank you so much!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Internally, an array is contiguous block memory memory, pointed to by a pointer. In this case, that block is 10 characters in length, which means only 9 of those characters are usable (the 10th is the null-character). With no null-character, who knows where the block ends.

When you ask for word, std::cin will extract 1 character at a time, and place the extracted character into word, until the newline character is found.

Cole99 wrote:
well if i use the number 2, and enter apple which is 5 letters long, apple still prints, not ap.(sic)

Can you show me an example, please?

Wazzak
@ OsiumFramework: Good question. I've never heard about such thing. I'm working with arrays for like one year now. The problem is that my teacher doesn't know how to explain anything, and so, I had to learn from books, but still no idea how could the program give him the whole word as a result.
At a closer look to his first code, I can see that he didn't even use a for loop to get through the array. That shouldn't be working, but the declared variable char word[10]; did actually store the word "apple". Maybe because he used cout << word; and not cout << word[i]; which should have been placed in a for loop as I already said.

@ Cole99: Waiting to see that code.
Is this the code you used ?

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

using namespace std;

int main()
{
char word[2];
                           // a for loop should be here
    cout << "Enter a word:" << endl;
    cin >> word;          // this should be: cin >> word[i];
                          // another for loop here
    cout << word << endl; // this should be cout << word[i];
}


so, actually the code should look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

using namespace std;

int main()
{
    string word[10];  // if you want a word, that means "string"
     for (int i=1;i<=10;i++)  /* you don't have to use the "{" "}", unless your code includes more than one statement in the for loop */
    {
        cin >> word[i];  // read 10 words
    }
    for (int i=1;i<=10;i++)
    {
        cout << word[i];  // write the 10 words you read already
    }
    return 0;
}
Last edited on
No jumper, actually std::ostreams are perfectly capable of printing C-strings, amongst other things. That the word was printed is no mystery either - string literals are embedded in the resulting binary upon compilation, and since the length of a C string is not determined by the size argument of the array but rather by the position of the next '\0' character, this can work. However, it's not actually guaranteed to work, the behavior is in this case of course undefined. Though I imagine that most compilers that are less than 10 years old would reject that piece of code.
Last edited on
well if i use the number 2, and enter apple which is 5 letters long, apple still prints, not ap.


This indicates that you are trashing memory, writing chars over it, and you're not lucky enough to have caused a segFault. This is very, very bad. You are trashing memory and you don't even know it.

C (and C++) carries several assumptions. One of these is that if you want to write over some memory, you are trusted to know what you are doing. The compiler will not stop you. The operating system may stop you by crashing, if you are lucky. In this case, it did not.
Last edited on
Talking about arrays, I don't want to start another topic, could someone tell me why and when do I have to include the #include<vector.h> ?

Only before 1998, which is when this became officially completely incorrect. Get a new compiler.


Nowadays, we do this:

#include <vector>
whenever we want to use a vector.
Last edited on
1
2
3
4
5
#include <vector>
int main()
{
    vector<int>v; // create an empty vector of integers
} 


What does that mean ?

Does it mean that I can create a vector which is actually non-defined in length ?

EDIT : Sorry this may be a missunderstanding. In my national language a vector is an one dimensional array. Isn't it same in C++ ?

vector != array ?
Last edited on
Does it mean that I can create a vector which is actually non-defined in length ?


That's what vectors are for. They change their own length as needed. The length is always defined, but it's not fixed.
Last edited on
Vectors are variable length - meaning, if you add an element and there's not enough space for it, the vector will just allocate more space and put the element in anyways.

"vector" is actually a rather misleading name, they are called ArrayLists pretty much everywhere else.
No, an array is fixed length, a vector (in C++) is variable length.

Oh, and you might be thinking about vectors in math - those are entirely different things.
Last edited on
Thanks both. I've got that now. So actually I was using array declarations, when I could make my life easier using vector(I always looked for such a thing with variable length ). That's how I'm gonna do it starting from now. Thanks again. Good night. (My GMT) :)

EDIT: I know the difference between math and c++, but thanks.
I'm in the 10th grade :)
Last edited on
Thanks but I already got it. look at my 3rd post
Topic archived. No new replies allowed.