By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,406 Members | 888 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,406 IT Pros & Developers. It's quick & easy.

strings in classes

P: n/a
hi
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
}
}

Mar 8 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
baje wrote:
hi
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];
Thiat's one string consisting of a maximum of four characters (plus one
terminating null character). It's not an array of five names.

You want this

string lastname[5];

>
public:
Names::lastname();
That's not legal syntax.
void setnames();

//default Constructor function initialises array
Names::names()
Nor is that.
{
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
Where did 30 come from?
}
}
It's obvious that you are confused between C strings, and C++ strings.
Moreover you don't understand C strings.

In C

char name;

this is a single char, it is not a string. So

char name[5];

is five chars, it is not five strings.

In C++ we can forget about C strings, and just use C++ strings

class Names
{
private:
string lastname[5];
public:
void setnames()
{
for (int i = 0; i < 5; ++i)
getline(cin, lastname[i]);
}
}

getline(cin, s) instead of cin.getline(s, 30) is that way you read a C++
string in C++. cin.getline(s, 30) is for C strings.

john
Mar 8 '07 #2

P: n/a
"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.
Mar 8 '07 #3

P: n/a
"osmium" wrote:
A C string is an array of arrays. You only have a single dimensional
array [in] your code.

That's wrong. What I should have said is:

A C style string is an array of char. So if you want an array of five
different 29 char names, which is what you seem to want, you need an array
of arrays. Reading tea leaves, I think you intended to accommodate a 29 char
name terminated by a 0.

Note that you will eventually have to be fluent in both C and C++ strings,
which was why I was ambivalent in my earlier post about which method you
focused on.
Mar 8 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.