hi everyone
i have written a simple program which asks for the colour and no of wheels of a vehicle.
i used gets() for inputting colour of vehicle.but it was skipping that line.
some body suggested me that it is buffer problem and use cin.getline() with getchar() function in while loop.but still i am unable to input colour from user please help...

thanks in advance

void two_wheeler::getdata()
          {
              vehicle::getdata();/*this is the base class function*/
              cout<<"enter colour"<<endl;
              getline(colour,sizeof(colour));/* the variable colour does not get value when user gives input  */
              while(getchar()!= '\n');
              cout<<"enter no. of wheels"<<endl;
              cin>>noofwheels;
           }

My assumption is that you are using getline wrong. If should be from a buffer and to a variable. What you need is getline(cin, colour); I believe.

There are two versions of getline() -- one for character arrays and the other for std::string. If variable colour is a character array then use cin.getline(colour, sizeof(colour)); , otherwise if colour is a std::string then getline(cin, colour);

sizeof( colour ) probably isn't going to work. The sizeof( ) operator only gives the size of the array in the function where the array was declared. All you are likely to get here is the size of a pointer.

Whether the string colour is char array or std::string, I find it hard to believe that line would even compile.

sizeof( colour ) probably isn't going to work. The sizeof( ) operator only gives the size of the array in the function where the array was declared. All you are likely to get here is the size of a pointer.

.

I was assuming colour was a character array, not a pointer. But if it is a pointer then of course you are right and the op needs to pass an integer constant that indicates the max size.

This gets more interesting. Within the class's functions, sizeof will give the array size if it was declared as char[], but not if created as a pointer.

The size of the char[] is also still accessible in the function where an instance of the class is created, but not when that instance is passed to another function.

The size of the char[] is also still accessible in the function where an instance of the class is created, but not when that instance is passed to another function.

Perhaps I'm misunderstanding.

class MyClass
{
public:
  char mystring[15];
};

void function1()
{
  MyClass myobject;
  std::cout << "function1() sizeof mystring = " << sizeof(myobject.mystring) << std::endl;
  function2(&myobject);
}

void function2(MyClass* myobject)
{
  std::cout << "function2() sizeof mystring = " << sizeof(myobject->mystring) << std::endl;
}

Those two should equal 15, even if function2 accepts myobject as a new instance instead of a pointer.

I don't think that's what he meant. Character arrays passed as a parameter to other functions can be passed as either a pointer or a character array. So the same rule applies to the sizeof() operator there as well

void foo1(char *mystring)
{
   // here mystring is just a pointer, so the sizeof() operator
   // will produce unexpected results
}

void foo2(char mystring[25])
{
   // here mystring is a charcter array and the sizeof() operator 
   // will work as expected
}

int main()
{
    char mystring[25];
    foo1(mystring);
    foo2(mystring);
}

foo2( ) will also give the size of the pointer, not the array.

commented: right! :) +36

sizeof( colour ) probably isn't going to work. The sizeof( ) operator only gives the size of the array in the function where the array was declared. All you are likely to get here is the size of a pointer.

Whether the string colour is char array or std::string, I find it hard to believe that line would even compile.

sorry for replying late,i am a newbie can u please explain me what is the difference between character array and and std::string?
all i want do that if a user has to input the data "red" how can i store that in the variable "colour" like if i have defined

char *colour;
colour=new char[10];
cout<<"enter colour"<<endl;
???

thanks in advance

cin.getline( colour, 10 );

foo2( ) will also give the size of the pointer, not the array.

You are absolutely right!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.