"baje" writes:
new to c++; have an array to hold five names; however having problems
reading in the names into the array.here the code i have
#include<iostream>
using std::cout;
using namespace std;
#include<conio.h>
#include<iomanip>
#include<string>
class Names
{
private:
char lastname[5];
public:
Names::lastname();
void setnames();
//default Constructor function initialises array
Names::names()
{
for (int i = 0; i < 5; i++)
{
strcpy(lastname, "");
}
}
void setnames()
{
for (int i = 0; i < 5; i++)
{
cin.getline(lastname, 30))//not working to read in five names
}
}
That didn't compile did it? Use cut and paste. There are missing
semi-colons and misspelled identifiers in what you posted. Remove stuff
never used such as <conio.h>.
You are confused, understandably, by the terminology String has no
definitive meaning in C++. It can mean a C++ style string (as in <string>)
or an old C style string, as in <cstring>. Your thoughts seem to be a
mixture of these two vastly different things. A C string is an array of
arrays. You only have a single dimensional array ion your code. You
include <stingbut then use strcpy() which is part of a different animal.
I suggest this:
o decide what kind of string you want to use.
(If you continue your interest in C++, you will eventually want to be
able to use the C++ string. )
o write a program implementing that choice. ONLY.