473,396 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Writing two String Arrays to a class

I am really stuck on this. I am trying to write a string array
containing a "word" and a "definition" to a class called Entry.
Ultimately this will end up in another class called dictionary. No,
this is not a homework assignment. In fact it was a question on my
class midterm that everyone bombed. Unfortunately we never covered
this in class prior to the midterm.

Anyway, please help if you would be so kind.

Here is what I have. I think I am messing up on the proper syntax.
This errors when I try to compile.

Class File:

//CLASS FILE - Entry.h

#include <iostream>
using namespace std;

#include <string>
using namespace std;
class Entry
{
public:
Entry(string[], string[]);
void setEntry (string[], string[]);

private:
string word[10];
string definition[10];
};

//MEMBER FUNCTION FILE - Entry.cpp

#include <iostream>
using namespace std;

#include <iomanip>
using namespace std;

#include <string>
using namespace std;

#include "Entry.h"

Entry::Entry();

void Entry::setEntry(string w[], string d[])
{

int i = 0;
word [i] = w[i];
definition{i} = d[i];
i = ++i;

}
//MAIN FILE - Midterm.cpp

#include "Entry.h" // Entry class definition

// function main begins program execution
int main()
{

Entry w1;
w1.setEntry("Car", "Something you drive");

return 0;
} // end main

Here is the ERROR:
1>------ Build started: Project: Midterm, Configuration: Debug Win32
------
1>Compiling...
1>Entry.cpp
1>Midterm.cpp
>c:\documents and settings\john\my documents\visual studio 2005\projects\midterm\midterm\midterm.cpp(24) : error C2660: 'Entry::setEntry' : function does not take 2 arguments
1>Generating Code...
1>Build log was saved at "file://c:\Documents and Settings\John\My
Documents\Visual Studio
2005\Projects\Midterm\Midterm\Debug\BuildLog.htm"
1>Midterm - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped
==========

Nov 22 '06 #1
9 2077
je****************@gmail.com wrote:
I am really stuck on this. I am trying to write a string array
containing a "word" and a "definition" to a class called Entry.
Ultimately this will end up in another class called dictionary. No,
this is not a homework assignment. In fact it was a question on my
class midterm that everyone bombed. Unfortunately we never covered
this in class prior to the midterm.
Sure...
>
Anyway, please help if you would be so kind.

Here is what I have. I think I am messing up on the proper syntax.
This errors when I try to compile.

Class File:

//CLASS FILE - Entry.h

#include <iostream>
using namespace std;

#include <string>
using namespace std;
Read the FAQ about placing 'using' directives in headers.
And even if you keep them, you only need one, not one after
every header inclusion.
>

class Entry
{
public:
Entry(string[], string[]);
void setEntry (string[], string[]);

private:
string word[10];
string definition[10];
Drop the square brackets and everything between them.
};

//MEMBER FUNCTION FILE - Entry.cpp

#include <iostream>
using namespace std;

#include <iomanip>
using namespace std;

#include <string>
using namespace std;

#include "Entry.h"

Entry::Entry();

void Entry::setEntry(string w[], string d[])
{

int i = 0;
word [i] = w[i];
definition{i} = d[i];
i = ++i;

}
//MAIN FILE - Midterm.cpp

#include "Entry.h" // Entry class definition

// function main begins program execution
int main()
{

Entry w1;
w1.setEntry("Car", "Something you drive");

return 0;
} // end main

Here is the ERROR:
1>------ Build started: Project: Midterm, Configuration: Debug Win32
------
1>Compiling...
1>Entry.cpp
1>Midterm.cpp
>c:\documents and settings\john\my documents\visual studio
2005\projects\midterm\midterm\midterm.cpp(24) : error C2660:
'Entry::setEntry' : function does not take 2 arguments
1>Generating Code...
1>Build log was saved at "file://c:\Documents and Settings\John\My
Documents\Visual Studio
2005\Projects\Midterm\Midterm\Debug\BuildLog.htm"
1>Midterm - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped
==========
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 22 '06 #2

je****************@gmail.com wrote:
><snip>

void Entry::setEntry(string w[], string d[])
{

int i = 0;
word [i] = w[i];
definition{i} = d[i];
i = ++i;

}

<snip>
This probably isn't going to do what you wanted. Perhaps you meant to
enclose those lines inside a looping construct? But TBH, there is no
need to use arrays here. Like Victor noted, you can simply pass 2
single strings: "Car" and "Something you drive" to this function if you
implement it correctly.

Also, the line:

i = ++i; //no

is not the correct way to increment.

++i; //better
i++; //also will work
i = i + 1; //OK

are 3 correct ways to do this.

Nov 22 '06 #3
No offense, but this was not very helpful.

1) It didnt work after I dropped the square brackets (in class only as
your reply indicated).
2) regarding FAQ about using headers - I dont think this will help the
route question which is how to write two string arrays to a class.

Any more advice ?
Victor Bazarov wrote:
je****************@gmail.com wrote:
I am really stuck on this. I am trying to write a string array
containing a "word" and a "definition" to a class called Entry.
Ultimately this will end up in another class called dictionary. No,
this is not a homework assignment. In fact it was a question on my
class midterm that everyone bombed. Unfortunately we never covered
this in class prior to the midterm.

Sure...

Anyway, please help if you would be so kind.

Here is what I have. I think I am messing up on the proper syntax.
This errors when I try to compile.

Class File:

//CLASS FILE - Entry.h

#include <iostream>
using namespace std;

#include <string>
using namespace std;

Read the FAQ about placing 'using' directives in headers.
And even if you keep them, you only need one, not one after
every header inclusion.


class Entry
{
public:
Entry(string[], string[]);
void setEntry (string[], string[]);

private:
string word[10];
string definition[10];

Drop the square brackets and everything between them.
};

//MEMBER FUNCTION FILE - Entry.cpp

#include <iostream>
using namespace std;

#include <iomanip>
using namespace std;

#include <string>
using namespace std;

#include "Entry.h"

Entry::Entry();

void Entry::setEntry(string w[], string d[])
{

int i = 0;
word [i] = w[i];
definition{i} = d[i];
i = ++i;

}
//MAIN FILE - Midterm.cpp

#include "Entry.h" // Entry class definition

// function main begins program execution
int main()
{

Entry w1;
w1.setEntry("Car", "Something you drive");

return 0;
} // end main

Here is the ERROR:
1>------ Build started: Project: Midterm, Configuration: Debug Win32
------
1>Compiling...
1>Entry.cpp
1>Midterm.cpp
c:\documents and settings\john\my documents\visual studio
2005\projects\midterm\midterm\midterm.cpp(24) : error C2660:
'Entry::setEntry' : function does not take 2 arguments
1>Generating Code...
1>Build log was saved at "file://c:\Documents and Settings\John\My
Documents\Visual Studio
2005\Projects\Midterm\Midterm\Debug\BuildLog.htm"
1>Midterm - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped
==========

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 22 '06 #4
Please do not top-post. Thanks. Rearranged.

je****************@gmail.com wrote:
Victor Bazarov wrote:
je****************@gmail.com wrote:
Class File:
>
//CLASS FILE - Entry.h
>
#include <iostream>
using namespace std;
>
#include <string>
using namespace std;
Read the FAQ about placing 'using' directives in headers.
And even if you keep them, you only need one, not one after
every header inclusion.
class Entry
{
public:
Entry(string[], string[]);
void setEntry (string[], string[]);
>
private:
string word[10];
string definition[10];
Drop the square brackets and everything between them.
};
No offense, but this was not very helpful.

1) It didnt work after I dropped the square brackets (in class only as
your reply indicated).
What didn't work? What does your code look like after you change the
member variables from arrays to single string objects? What errors did
the compiler report that you want to understand? You original post
followed the posting guidelines here very well:

http://www.parashift.com/c++-faq-lit...t.html#faq-5.8

You've made changes so you've got new code now, but you still have
problems. You'll need to show minimal, complete code as you've modified
it and people will be able to help with those problems.
2) regarding FAQ about using headers - I dont think this will help the
route question which is how to write two string arrays to a class.
While not directly relevant to your question, Victor's advice is very
sound. using directives in headers are generally a Bad Thing and should
be avoided in favour of fully qualified names (put std:: in front of
each individual occurrance) unless you have good reason. Repeating the
using directive is completely unnecessary.

Gavin Deane

Nov 22 '06 #5

je****************@gmail.com wrote in message ...
>I am really stuck on this. I am trying to write a string array
You are doing it "THE HARD WAY".
>containing a "word" and a "definition" to a class called Entry.
Ultimately this will end up in another class called dictionary. No,
this is not a homework assignment.
If I find out it is, I'll take back every word, and tell your instructor!
>Anyway, please help if you would be so kind.
Here is what I have. I think I am messing up on the proper syntax.
This errors when I try to compile.

Class File:
//CLASS FILE - Entry.h
#include <iostream>
// >using namespace std;
>#include <string>
// >using namespace std;

#include <vector>
>
class Entry{ public:
// Entry(string[], string[]);
// void setEntry (string[], string[]);

Entry( std::string, std::string );
void setEntry( std::string, std::string );

void PrintAll( std::ostream &out );
>
private:
// string word[10];
// string definition[10];

static size_t const sz = 10;
std::vector<std::stringwords;
std::vector<std::stringdefinitions;

// or, even better:
// struct dict{
// std::string word;
// std::string def;
// };
// std::vector<dictdictionary;
};

//MEMBER FUNCTION FILE - Entry.cpp

#include <iostream>
#include <ostream// for std::endl;

// >using namespace std;
// >#include <iomanip// unused
// >using namespace std;
>
#include <string>
// >using namespace std;

// using std::string; // if you simply must!
>
#include "Entry.h"
// >Entry::Entry();

Entry::Entry(std::string word, std::string def) : words(sz), definitions(sz){
words( 0 ) = word;
definitions( 0 ) = def;
}
>
// >void Entry::setEntry(string w[], string d[]){
// int i = 0;
// word [i] = w[i];
// definition{i} = d[i];

// note: even if you did do that, the syntax would be:
// definition[ i ] = d[ i ]; // lose the '{}'

// i = ++i; // NO!!! If you must, just do '++i'
// >}

void Entry::setEntry( std::string w, std::string d, size_t index ){
if( index >= words.size() ){ return;}
// or: if( index >= sz ){ return;}
words.at( index ) = w;
definitions.at( index ) = d;
return;
}

void Entry::PrintAll( std::ostream &out ){
for( size_t i(0); i < words.size(); ++i){
out << words.at( i ) << ": "
<< definitions.at( i ) << std::endl;
} // for(i)
return;
} // Entry::PrintAll(ostream&)
>
//MAIN FILE - Midterm.cpp
#include "Entry.h" // Entry class definition

// function main begins program execution
int main(){
// Entry w1;

Entry w1( "string0", "This is string0" );

w1.setEntry( "Car", "Something you drive", 1 );
w1.setEntry( "Newbie", "Someone new", 2 );
w1.setEntry( "Top-Post", "Hate and discontent in post!", 3 );

w1.PrintAll( std::cout );
>
return 0;
} // end main
--
Bob R
POVrookie
Nov 22 '06 #6

BobR wrote:
je****************@gmail.com wrote in message ...
I am really stuck on this. I am trying to write a string array

You are doing it "THE HARD WAY".
containing a "word" and a "definition" to a class called Entry.
Ultimately this will end up in another class called dictionary. No,
this is not a homework assignment.

If I find out it is, I'll take back every word, and tell your instructor!
Anyway, please help if you would be so kind.
Here is what I have. I think I am messing up on the proper syntax.
This errors when I try to compile.

Class File:
//CLASS FILE - Entry.h
#include <iostream>
// >using namespace std;
#include <string>
// >using namespace std;

#include <vector>

class Entry{ public:

// Entry(string[], string[]);
// void setEntry (string[], string[]);

Entry( std::string, std::string );
void setEntry( std::string, std::string );

void PrintAll( std::ostream &out );

private:
// string word[10];
// string definition[10];

static size_t const sz = 10;
std::vector<std::stringwords;
std::vector<std::stringdefinitions;

// or, even better:
// struct dict{
// std::string word;
// std::string def;
// };
// std::vector<dictdictionary;
};

//MEMBER FUNCTION FILE - Entry.cpp

#include <iostream>
#include <ostream// for std::endl;

// >using namespace std;
// >#include <iomanip// unused
// >using namespace std;

#include <string>
// >using namespace std;

// using std::string; // if you simply must!

#include "Entry.h"
// >Entry::Entry();

Entry::Entry(std::string word, std::string def) : words(sz), definitions(sz){
words( 0 ) = word;
definitions( 0 ) = def;
}
// >void Entry::setEntry(string w[], string d[]){
// int i = 0;
// word [i] = w[i];
// definition{i} = d[i];

// note: even if you did do that, the syntax would be:
// definition[ i ] = d[ i ]; // lose the '{}'

// i = ++i; // NO!!! If you must, just do '++i'
// >}

void Entry::setEntry( std::string w, std::string d, size_t index ){
if( index >= words.size() ){ return;}
// or: if( index >= sz ){ return;}
words.at( index ) = w;
definitions.at( index ) = d;
return;
}

void Entry::PrintAll( std::ostream &out ){
for( size_t i(0); i < words.size(); ++i){
out << words.at( i ) << ": "
<< definitions.at( i ) << std::endl;
} // for(i)
return;
} // Entry::PrintAll(ostream&)

//MAIN FILE - Midterm.cpp
#include "Entry.h" // Entry class definition

// function main begins program execution
int main(){
// Entry w1;

Entry w1( "string0", "This is string0" );

w1.setEntry( "Car", "Something you drive", 1 );
w1.setEntry( "Newbie", "Someone new", 2 );
w1.setEntry( "Top-Post", "Hate and discontent in post!", 3 );

w1.PrintAll( std::cout );

return 0;
} // end main

--
Bob R
POVrookie

Thanks for your response. My instructor is much younger than I am so
if you feel you need to tell him you helped me solve my midtrem
problem...go right ahead....lol Im going back to school not still in
school....16 years later no less...

Regarding the "Im doing it the hard way". Im doing it his way. the
problem said to use a class called Dictionary that would store an array
of strings including 10 words and 10 definitions. Use a class called
entry to write the entries and retrieve them from dictionary.

As we were trying to take the midterm his requirements kept changing
(started with pointers, etc.) because he was writing the program (as we
were) and found out he couldnt get it to work....lol.

Regarding not posting code? I did what the post said and removed
brackets and everything in between (in the class only) .....after
realizing you were trying to have me eliminate the array all together.
That I can do. Writing strings to classes is not what the problem is.
Its writing string arrays to classes.

The post here uses vectors. We havent learned about those yet so that
wouldnt be included on the midtrem results.

I was simply looking for a way to write string arrays to a class for
storage purposes becuase thats what I was asked to do. I am pretty
frustrated with school right now as I think what should be very simple
is being overly complicated. I didnt want someone to solve my problem
I simply wanted to see an example of how I would write this scenario to
a class that stored it in an array...

now you know what my world is like.... thanks for your help....

Nov 22 '06 #7

je****************@gmail.com wrote in message ...
>
BobR wrote:

Regarding the "Im doing it the hard way". Im doing it his way. the
problem said to use a class called Dictionary that would store an array
of strings including 10 words and 10 definitions. Use a class called
entry to write the entries and retrieve them from dictionary.

Regarding not posting code? I did what the post said and removed
brackets and everything in between (in the class only) .....after
realizing you were trying to have me eliminate the array all together.
That I can do. Writing strings to classes is not what the problem is.
Its writing string arrays to classes.

The post here uses vectors. We havent learned about those yet so that
wouldnt be included on the midtrem results.

I was simply looking for a way to write string arrays to a class for
storage purposes becuase thats what I was asked to do. I am pretty
frustrated with school right now as I think what should be very simple
is being overly complicated. I didnt want someone to solve my problem
I simply wanted to see an example of how I would write this scenario to
a class that stored it in an array...
now you know what my world is like.... thanks for your help....

Revised for no vector:
>Class File:
//CLASS FILE - Entry.h
#include <iostream>
#include <string>
// hint:
class Dictionary{ public: // or struct
static size_t const sz = 10;
std::string word[ sz ];
std::string definition[ sz ];
};

>class Entry{ public:
Entry( std::string, std::string );
void setEntry( std::string, std::string );
void PrintAll( std::ostream &out );
private:
// string word[10];
// string definition[10];
static size_t const sz = 10;
std::string word[ sz ];
std::string definition[ sz ];

// hint: Dictionary DictArray;
};
>//MEMBER FUNCTION FILE - Entry.cpp

#include <iostream>
#include <ostream// for std::endl;
>#include <string>
// using std::string; // if you simply must!
>
#include "Entry.h"
Entry::Entry(std::string word, std::string def){
word[ 0 ] = word;
definition[ 0 ] = def;

// hint: DictArray.word[ 0 ] = word;
// hint: DictArray.definition[ 0 ] = def;

}

void Entry::setEntry( std::string w, std::string d, size_t index ){
if( index >= sz ){ return;}
word [ index ] = w;
definition[ index ] = d;
return;
}

// if you can't use ostreams change:
// void Entry::PrintAll(){
// ... and 'out' to 'std::cout' and change the call in main().

void Entry::PrintAll( std::ostream &out ){
for( size_t i(0); i < sz; ++i){
out << word[ i ] << ": "
<< definition[ i ] << std::endl;
} // for(i)
return;
} // Entry::PrintAll(ostream&)
>
//MAIN FILE - Midterm.cpp
#include "Entry.h" // Entry class definition

int main(){ // function main begins program execution
Entry w1( "string0", "This is string0" );
// or: Entry w1( "--- word ---", "|------ definition ---" );
w1.setEntry( "Car", "Something you drive", 1 );
w1.setEntry( "Newbie", "Someone new", 2 );
w1.setEntry( "Top-Post", "Hate and discontent in post!", 3 );
w1.PrintAll( std::cout );
>
return 0;
} // end main
--
Bob R
POVrookie
Nov 23 '06 #8

BobR wrote in message ...
>
>>//MEMBER FUNCTION FILE - Entry.cpp
// >Entry::Entry(std::string word, std::string def){
// word[ 0 ] = word;
// Duh!!!

Entry::Entry(std::string wrd, std::string def){
word[ 0 ] = wrd;
definition[ 0 ] = def;
}
--
Bob R
POVrookie
Nov 23 '06 #9

BobR wrote:
BobR wrote in message ...
>//MEMBER FUNCTION FILE - Entry.cpp
// >Entry::Entry(std::string word, std::string def){
// word[ 0 ] = word;
// Duh!!!

Entry::Entry(std::string wrd, std::string def){
word[ 0 ] = wrd;
definition[ 0 ] = def;
}

--
Bob R
POVrookie
Thanks for your help. I finally got it to work with your suggestions
here.

Discontent, yes! Hate, no...........

Thanks again Bob.

Nov 26 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: rob | last post by:
hey every1, I've got alot of data to write out to file and it's all just 1's and 0's. It's all stored in 2 dimensional arrays of width 32 and varying height. At the moment it's all just...
7
by: Eric | last post by:
Hi All, I need to XOR two same-length Strings against each other. I'm assuming that, in order to do so, I'll need to convert each String to a BitArray. Thus, my question is this: is there an...
2
by: Robert Reijntjes | last post by:
Hi, I need to read/write data from/to binary files that have an already defined. This means I can't define classes with the attribute. The files also have arrays with variable length. This...
14
by: rohitpatel9999 | last post by:
Hi While developing any software, developer need to think about it's possible enhancement for international usage and considering UNICODE. I have read many nice articles/items in advanced C++...
3
by: SneakyElf | last post by:
i am very green with c++ so i get stuck on very simple things anyway, i need to write a program that would read data from file (containing names of tv shows and their networks) one line at a time...
19
by: rmr531 | last post by:
First of all I am very new to c++ so please bear with me. I am trying to create a program that keeps an inventory of items. I am trying to use a struct to store a product name, purchase price,...
2
by: Tony Winslow | last post by:
Hi, there! Some users of this group mentioned before that being able to write one's own string class in C++ is a sign of mastering the basics of the language. My problem is that I don't quite...
6
by: Patient Guy | last post by:
I am a newcomer to using PHP but not to programming (C, C++, Javascript). I am playing around with classes and wanted to make a function that has a method simply for producing either plain text...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
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,...

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.