why doesnt char name[10] work?

closed account (LAfSLyTq)
char name[10];

int main(){
cin<<name<<endl;
cout>>name>>endl;
}

if i put my name a wernkjofnwjotrnwenkormwoekmrkowemkormom it displays all the letters, and doesnt shorten it to [10]
Why would it? This is C++. A programming language where you're trusted to know what you're doing.

I assume you actually meant this:
cin>>name;
with the >> going the right way.

It means (when name is a char*): read in from console until you hit white space, and store all that at the memory location name and following memory locations, and stick a zero on the end. It does what you tell it. If you tell it to trash the memory and stomp all over data, it will do that, because this is C++, and you're supposed to know what you're doing.

cout<<name<<endl;
This means (when name is a char*): Output whatever you find at the memory location name, and all following locations, until you hit a zero value. Why would it stop after 10 characters? Did you tell it to? No. This is C++, and it does what you tell it to.

char name[10];
This mean: set aside enough space for 10 char objects, and let's label this name. That's all it means. It does not mean: in the future, stop me writing to anywhere in memory. If you ask to write into memory, as you do, it's up to YOU to make sure that writing to that memory is sensible. The OS will stop you if you do anything really, REALLY stupid, but otherwise you're trusted to know what you're doing. This is C++.
Last edited on
Interesting. Without testing, I can only guess that the operator>>() overload being used simply doesn't check for buffer overflows. I guess it simply doesn't have the required information to do so.

I guess an overload can be added to provide the information.

You may classify this as a bug of your compiler or as your own bug. Up to you and whatever the C++ standard says.

To avoid buffer overruns like this, use safer functions, like std::cin.getline().
Last edited on
closed account (LAfSLyTq)
i do know what im doing jackass, maybe you should actually help people on this forum instead of being an arrogant ass. the reason why i have the << going to wrong way, probably because i havent slept in 2 days. char[10] according to my brothers teaching is supposed to make the char contain only ten letters? if that teaching isnt correct then how would i do this?
i do know what im doing jackass

You clearly don't, otherwise you wouldn't have asked. I'm explaining it to you. I know I'm explaining in simple terms. That's not an insult. It's so that you definitely understand.

char[10] according to my brothers teaching is supposed to make the char contain only ten letters?
How can a char contain ten letters? A char is an object, of type char. It can hold one char. I explained above what char name[10]; means. Perhaps you answered before I finished editing it, in which case I apologise for that. Please see above.

if that teaching isnt correct then how would i do this?

This being C++, use a string.

1
2
string name;
cin>>name; // Pretty much as much as you like. 

Last edited on
closed account (LAfSLyTq)
cant use strings, the console wont accept it.
i just want it to stop at 10 "objects" how would i do so?
Did you #include <string> ?

The following code absolutely works on a console (I have just compiled and tested it).

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

using namespace std;

int main()
{
  string name;
  cin >> name;
  cout << name;
}



closed account (LAfSLyTq)
yes i included it, im not a retard lol.
i just want to know how to make char name; contain a maximum of 10 characters. im sorry for my little outburst a minute ago, i just felt like you were trying to insult me because you said something like "You need to know what you are doing" and i am a little frustrated without getting sleep. the reason i need it to contain 10 characters maximum is because its for a game and i dont want the player having a long name like shown above.
I would still use a string, you can use the length method to validate it.
Use getline().
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

using namespace std;

int main()
{
    char name[10] = "";
    cin.getline(name, sizeof(name));
    cout << name << endl;
    return 0;
}
i just want it to stop at 10 "objects" how would i do so?


Although strings are absolutely the way to go, you can limit input into an array as well:

cin >> setw(10) >> name;
(note, this will read 9 characters, since the last position is reserved for the null terminator. It will also read less than 9 if a space is encountered: to read with spaces included, see cin.getline() above)
Last edited on
try this

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include<string>
using namespace std;
void main(void)
{
       string name;
       cout<<"Enter name: ";
       getline(cin,name);
       cout<<name;
       system("pause");
}
you can use this overload:
1
2
3
4
5
6
7
8
9
template<int n>
istream& operator>>(istream& stm, char (&array)[n])
{
     for (int i=0; i<n; i++)
     {
          stm>>niz[i];
          }
     return stm;
     }
Topic archived. No new replies allowed.