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>
usingnamespace 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.
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).
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)
@ 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>
usingnamespace 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>
usingnamespace 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;
}
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.
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.
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.
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 :)