From: Jim Langston on
Hyperion_dani wrote:
> On Jan 26, 12:45 am, "Jim Langston" <tazmas...(a)rocketmail.com> wrote:
>> Hyperion_dani wrote:
>>> Hi, to all. I'm new here and I'm a student at Computer Science
>>> Faculty.
>>> I started to learn C++ and I'm having a problem to wich solution
>>> should be simple but I haven't found it yet.
>>> Let's say I want to make it work the following code:
>>> #include <iostream.h>
>>> void main()
>>> { char comand, char object[10];
>>> cout<<"Input comand";
>>> cin>>comand;
>>> if (comand=="pencil") cout<<"Yes, is a pencil";
>>> else cout<<"Error";
>>> }
>>> What I'm trying to get here is to compare the user input against a
>>> word, a string. If I use that code returns erorr. Aparantly I need
>>> string comparison functions. It must be that complicated to work
>>> with words? I have in mind a program that works with words mainly,
>>> variables stores words insead of numbers.
>>> Also lets say that I want to store a word.
>>> if (comand != 0) object[1]=comand;
>>
>> You have a mixture of C and C++ code here. You are using cout, but
>> are not using std::string. For a std::string it's simple.
>>
>> std::string command;
>> std::cout >> "Input Command:";
>> std::cin >> command;
>> if ( command == "pencil" )
>> std::cout << "yes, it is a pencil.";
>>
>> However, your code has some problems. Your command is a single
>> character, one byte. "pencil" is 7 bytes counting the null
>> terminator. It won't fit in a single char. It is better to use
>> std::string as shown, although if you absolutly had to you could use
>> a c-style string which is an array of chracters something like:
>> char command[100];
>> but then would have to use a string compare such as
>> if ( strcmp( command, "pencil" ) == 0 )
>> std::cout >> "yes, it is a pencil";
>>
>> Stick with std::string as much as you can, it is much easier.
>>
>> --
>> Jim Langston
>> tazmas...(a)rocketmail.com
>
> Thanks for your feedback.
> I will answer to you and to LR in the same post because your advises
> have common points.
> First of all to answer to LR. I'm using Microsoft Visual C++ and for
> reading, the material I have from the teacher courses.
> Jim you said I have a mixture of C and C++ code here. Actually is a
> mixture of C++ and the early BASIC. I started this project then and
> now I have the posibility to make it here in C++ which is more
> evoluate. The snipet I give was for me to express what I want to do. I
> have better used pseudocode.
> if ( strcmp( command, "pencil" ) == 0 )
> std::cout >> "yes, it is a pencil";
> The first line is what my laboratory teacher showed me but forgoten.
> Thanks.
> The second line I'm not familiar with. We used just cout<<
> "yes...pencil"; maybe differs from a compilator to another, I dont
> know.

Well, are you confused about the typo I made, it should of been
std::cout << "yes, it is a pencil"
or the use of std:: ?

There is a line you either didn't show in your code
using namespace std;
or you are using an old compiler such as MS VC++ 6.0

I won't go into namespace usage until you let me know if it was the type
that confused you, or the use of std::

> Probably is best that I learn more C++ at school but is frustrating to
> stomp in something that should be easy.

c-style strings are not really easy, they are not intiutive. C++'s
std::string, however, is much simpler and much more intiutive. For example.
This will not work in C:
char c[10];
c = "pencil";
But this will work in C++
std::string c;
c = "pencil";

std::strings work a lot more like basic strings, you can assign them, add to
them and compare them with each other and usually c-style strings. c-style
strings, however, require use of functions for most things.
strcpy() strncpy() - String Copy
strcmp() - String Compare
being the big 2 (3). Using std::strings we don't need those. I would
suggest you learn std::strings now. Your teacher, however, is most likely
going to continue to teach c-style strings. In my opionion he should go
through c-style strings and how they are used, but have you use std::strings

> I will use pseudocode here te ilustrate what i'm trying to get.
> Let's say that I want to reproduce a section of our reality but not in
> picture or graphics like they do in video games but in words. I would
> categorize words in object, actions, etc. So I would need variables or
> something to store objects(real objects like pencil) and let's say 20
> properties for them like color, lenth etc.
> Pseudocode Input sentence
> Read sentence (ie. "the pencil is red")

For std::strings this is relatively easy.
std::string Line;
std::getline( std::cin, Line );

That would read a line up to a carriage return into the std::string Line.

> Break down the sentence into words(results 4 words)

Here you need to parse the sentance. An easy way to do that with
std::string is by using a stringstream. You can specialize for an input
string stream or an output string stream, but I generally get lazy and just
use the all encompassing string string.

std::stringstream MyStream( Line );
std::string Word;
while ( MyStream >> Word )
{
// At this point the variable Word contains one word from the sentance
}

> Compare each word with all the other words(previosly
> stored)

With std::strings again easy. Depending on how you store the words.

if ( Word == Words[0] )
// It is word 1

for ( int i = 0; i < 10; ++i )
if ( Word == Words[i] )
// it is word # i

if ( Word == "pencil" )
// the word is pencil

Depending on how you are storing your words, etc.. depends on what you want
to compare them against.

> If a word matches obtain his category(ie. object or
> property)

Not sure what you mean by "category", is it an integer? A character? A
string? However you care to associate it with each word. You could maybe
even use a std::map, such as
std::map<std::string, std::string> Words;

> If not ask user input
> Request "What kind of word is it?"
> User "property" (refering to the word 'red')
> Request "What kind of property is it?"
> User "color"
> Store(save) - pencil.property.color = "red"
> Go back to input sentence

Okay, so you have phsical properties stored in strings and attributes of
that property stored in strings. So each object has one (or more) property.

> I don't need atm feedback to all that I presented here(I hope you
> understood what I wrote above), atm I only need to know how to work
> with words also feedback on using a construction like
> pencil.property.color would be great.
> In BASIC I coded something like this, from what I recall.
> object (100,100,100); command;
> INPUT command
> for i = 1 to 100
> for j = 1 to 100
> for k = 1 to 100
> if command = object (i,j,k) then go to...
> next i,j,k

One way to do it would be to have a class with a constructor.

class Property
{
public:
Property( std::string Kind, std::string Attribute ): Kind( Kind ),
Attribute( Attribute ) {}
std::string Kind;
std::string Attribute;
}

> I barely remember the code but I hope you got the picture. Even it was
> much simpler to work with words in BASIC there other problems like the
> fact that it used lines instead of functions 10 PRINT
> 20 INPUT
> object (100,100,100) was for storing object and properties values like
> object(color, lenth, wight)
> I hope I made my self clear this time in what I want to acomplish,
> sorry for my usage of English language, I'm from Romania. Any help
> would be apreciated thanks.

In Basic you were using a fixed array it seems like, in C++ you would use
one of the STL containers. It sounds like a std::map might serve you well
for the known properties. I don't know how far you are in your class,
however, and not sure if you should be using std::maps yet.. But sooner is
probably better than later in this instance.

--
Jim Langston
tazmaster(a)rocketmail.com


From: Hyperion_dani on
On Jan 26, 1:10 pm, Francis Glassborow
<francis.glassbo...(a)btinternet.com> wrote:
> Hyperion_dani wrote:
> >> std::string command;
> >> std::cout >> "Input Command:";
> >> std::cin >> command;
> >> if ( command == "pencil" )
> >> std::cout << "yes, it is a pencil.";
>
> >> However, your code has some problems. Your command is a single character,
> >> one byte. "pencil" is 7 bytes counting the null terminator. It won't fit
> >> in a single char. It is better to use std::string as shown, although if you
> >> absolutly had to you could use a c-style string which is an array of
> >> chracters something like:
> >> char command[100];
> >> but then would have to use a string compare such as
> >> if ( strcmp( command, "pencil" ) == 0 )
> >> std::cout >> "yes, it is a pencil";
>
> >> Stick with std::string as much as you can, it is much easier.
>
> > Thanks for your feedback.
> > I will answer to you and to LR in the same post because your advises
> > have common points.
> > First of all to answer to LR. I'm using Microsoft Visual C++ and for
> > reading, the material I have from the teacher courses.
>
> OK, but are you saying your course has no text book? That is fine if the
> teacher provided material is sound but many of us have experiences the
> problems of well intentioned teachers whose style and use of idioms is a
> descade out of date.

No text book.

From: Hyperion_dani on
On Jan 27, 12:03 am, "Jim Langston" <tazmas...(a)rocketmail.com> wrote:


> Well, are you confused about the typo I made, it should of been
> std::cout << "yes, it is a pencil"
> or the use of std:: ?

The use of std
> There is a line you either didn't show in your code
> using namespace std;
> or you are using an old compiler such as MS VC++ 6.0

I'm using MS VC++ 6.0



> Not sure what you mean by "category", is it an integer? A character? A
> string?

Is not from programing. Ie. of category: object, action, property.

I don't know how far you are in your class,
> however, and not sure if you should be using std::maps yet.. But sooner is
> probably better than later in this instance.
>
> --
> Jim Langston
> tazmas...(a)rocketmail.com

Not so far in my class. I will look into std::maps thanks.
From: Hyperion_dani on
Thanks again for your feedbacks. I will start to go deeper into the
teacher courses to see what I can make of them and then follow more on
your answers.
Till then I wanted to say thanks and to reply to your questions(as I
did in previous posts).
Also I want to mention that our course is named C/C++ programing. So
we are doing C and C++ in the same time. Is good I guess to know about
both of them but can arise confusions if the teacher doesn't
accentuate enough on the difference between them or when the student
doesn't pay to much attention. :)
From: Ian Collins on
Hyperion_dani wrote:
> On Jan 27, 12:03 am, "Jim Langston" <tazmas...(a)rocketmail.com> wrote:
>
>> There is a line you either didn't show in your code
>> using namespace std;
>> or you are using an old compiler such as MS VC++ 6.0
>
> I'm using MS VC++ 6.0
>
You really should get something newer if you wish to learn standard C++.

--
Ian Collins.