problem with "cin.getline"

I have a problem with "cin.getline", please help! It's not getting a value.

//"MayanNumber.cpp"
#include <iostream>
#include <string>
#include <cmath>
#include "MayanNumberType.h"
using namespace std;
MayanNumberType:: MayanNumberType ()
{
char MayanNumber [50]= NULL;
}
void MayanNumberType:: GetMayanNumber (char MayanNumber[50])
{
cout << "Enter Mayan Number" << endl;
cin.getline (MayanNumber,50); // PROBLEM HERE????
}
_____________________________________________________________________
//CLASS "MayanNumberType.h"
#include <string>
#include <iostream>
#pragma once
class MayanNumberType
{
public:
MayanNumberType ();
void GetMayanNumber (char MayanNumber[50]);
private:
char MayanNumber [50];
};
_____________________________________________________________________
//main.cpp
#include <string>
#include <iostream> // gives access to the io stream operations
#pragma once
#include "MayanNumberType.h"
using namespace std;// grants access to the std namespace that includes C++ I/O objects cout and cin.
//Function prototypes

int main ()
{
MayanNumberType mn;
mn.GetMayanNumber (MayanNumber);
return 0;
}
cin.getline() reads into a cstring which needs a null terminator. If you have a cstring size 50 then you can only read 49 characters into it in order to leave space for the null terminator.
yes,
but ...what am I missing?

Thanks.:)
1
2
3
4
MayanNumberType:: MayanNumberType ()
{
          char MayanNumber [50]= NULL;
}


What this does:

Defines a local variable inside the default constructor of MayanNumberType which immediately goes out of scope.

What you meant to do:

1
2
3
4
MayanNumberType:: MayanNumberType ()
{
	MayanNumber[0] = '\0' ;
}


1
2
3
4
5
void MayanNumberType:: GetMayanNumber (char MayanNumber[50])
{
	cout << "Enter Mayan Number" << endl;
	cin.getline (MayanNumber,50); // PROBLEM HERE????
}


What this does: Stores user input in the area of storage indicated by the parameter MayanNumber.
What it doesn't do: Modify the member named MayanNumber of the object it's invoked with.

1
2
MayanNumberType mn;
mn.GetMayanNumber (MayanNumber);


What this does: Causes your compiler to barf because there is no variable name MayanNumber.



thank you! it works)))
Topic archived. No new replies allowed.