Writing two String Arrays to a class

J

jerry.upstatenyguy

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 = w;
definition{i} = d;
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

Victor Bazarov

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 = w;
definition{i} = d;
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
 
J

jjds101

<snip>

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

int i = 0;
word = w;
definition{i} = d;
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.
 
J

jerry.upstatenyguy

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 said:
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 = w;
definition{i} = d;
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
 
G

Gavin Deane

Please do not top-post. Thanks. Rearranged.

Victor said:
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-lite/how-to-post.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
 
B

BobR

(e-mail address removed) 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 said:
class Entry{ public:

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

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

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

static size_t const sz = 10;
std::vector<std::string> words;
std::vector<std::string> definitions;

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



//MEMBER FUNCTION FILE - Entry.cpp

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

// >using namespace std;
// >#include said:
#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 = w;
// > definition{i} = d;

// 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::eek:stream &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 );
 
J

jerry.upstatenyguy

BobR said:
(e-mail address removed) 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 said:
class Entry{ public:

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

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

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

static size_t const sz = 10;
std::vector<std::string> words;
std::vector<std::string> definitions;

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



//MEMBER FUNCTION FILE - Entry.cpp

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

// >using namespace std;
// >#include said:
#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 = w;
// > definition{i} = d;

// 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::eek:stream &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





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....
 
B

BobR

(e-mail address removed) 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::eek:stream &out );
// > 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 said:
#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::eek:stream &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 );
 
B

BobR

BobR wrote in message ...// >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;
}
 
J

jerry.upstatenyguy

BobR said:
BobR wrote in message ...// >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;
}

Thanks for your help. I finally got it to work with your suggestions
here.

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

Thanks again Bob.
 

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

No members online now.

Forum statistics

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

Latest Threads

Top