strings in classes

B

baje

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
}
}
 
J

John Harrison

baje said:
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);
}
}

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
 
O

osmium

baje said:
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 <sting> but 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.
 
O

osmium

osmium said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top