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

Writing two String Arrays to a class

P: n/a
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
Share this Question
Share on Google+
9 Replies


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

P: n/a

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

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

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

P: n/a

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

P: n/a

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

P: n/a

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

P: n/a

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

P: n/a

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 discussion thread is closed

Replies have been disabled for this discussion.