473,466 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

strings in classes

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
3 1753
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
"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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Gordon Airport | last post by:
Has anyone suggested introducing a mutable string type (yes, of course) and distinguishing them from standard strings by the quote type - single or double? As far as I know ' and " are currently...
3
by: Mikael Olofsson | last post by:
Hi! I've asked Google, but have not found any useful information there. Situation: I have a base class, say >>> class base(object): ImportantClassAttribute = None Now, I want to...
1
by: yuri | last post by:
Hello, Is it possible to create a binding that would map an input message with a part defined as a complex-type element to a url-encoded string? For example, wsdl file defines a message as ...
10
by: Aris | last post by:
A few days ago I asked from you how to join a string like "file" A number that change values from 1 to 5 and another string like ".txt" to have a result like "file1.txt","file2.txt" and so on ...
4
by: Kza | last post by:
Hi, just in the process of maintaining some software that used some funy old string library and char*s , and we are updating everything to use std::strings. (or should I say std::basic_string<>s) ...
4
by: CoreyWhite | last post by:
/* WORKING WITH STRINGS IN C++ IS THE BEST WAY TO LEARN THE LANGUAGE AND TRANSITION FROM C. C++ HAS MANY NEW FEATURES THAT WORK TOGETHER AND WHEN YOU SEE THEM DOING THE IMPOSSIBLE AND MAKING...
12
by: Martin Drautzburg | last post by:
I would like to validate sql strings, which are spread all over the code, i.e. I run ("prepare") them against a database to see if it happy with the statements. Spelling errors in sql have been a...
2
by: Lee J | last post by:
Gang, I am self taught in C and fairly proficient for last 20 years. Don't program much, just little utilities for work. Been wanting to create windows forms and found MS VC++ Express. ...
5
by: alan | last post by:
Hello world, I'm wondering if it's possible to implement some sort of class/object that can perform mapping from class types to strings? I will know the class type at compile time, like so:...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.