|
Prev: How to compile the following on Win64bit using Visual Studio2005 x64
Next: char arrays in C++, why cant asign a string literal,
From: Hyperion_dani on 25 Jan 2008 13:56 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;
From: LR on 25 Jan 2008 15:23 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> I'm not sure what the official status of <iostream.h> is, but <iostream> is a much better bet. If your compiler doesn't support <iostream> look into a new compiler. > void main() int main() > { char comand, char object[10]; I don't think that this code compiled. When you post, it's best to post code that you actually compiled. You may find suggestions about how to post and other useful information here http://www.parashift.com/c++-faq-lite/ char command; creates a variable named command that can store one char. You could initialize it like this, char command = 'a'; char object[10]; creates an array of type char that can store up to ten chars. Usually in this kind of variable the chars you want to store are followed by a trailing zero value char. You could initialize it like this, char object[10] = "hello"; Then, object[0] contains an 'h', object[1] contains an 'e', ... object[4] contains an 'o' and object[5] contains a zero. > cout<<"Input comand"; > cin>>comand; Reads one character. > if (comand=="pencil") I think that perhaps you'd be better off using std::string. #include <iostream> #include <string> int main() { std::string command; std::cin >> command; if(command == "pencil") std::cout << "Hurray" << std::endl; } [snip] > What I'm trying to get here is to compare the user input against a > word, a string. Words and strings are different things. Sorry, I know that sounds pedantic, but really, understanding the difference between things like char, std::string and what a word is useful. > If I use that code returns erorr. Sorry, I don't know what this means. > Aparantly I need string comparison functions. Ok, well then use std::string and compare as above. > It must be that complicated to work with words? The difficulty of working with text depends on what you're doing. > I have in mind a program that works with words mainly, > variables stores words insead of numbers. Sounds interesting. Please let us know how it goes. > Also lets say that I want to store a word. > if (comand != 0) object[1]=comand; I'm not quite sure what you wanted to do there, but perhaps you should try something like this snippet, std::string command; std::vector<std::string> object; if(!command.empty()) object.push_back(command); May I ask what compiler and book you're using? LR
From: Jim Langston on 25 Jan 2008 17:45 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 tazmaster(a)rocketmail.com
From: Hyperion_dani on 26 Jan 2008 02:56 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. Probably is best that I learn more C++ at school but is frustrating to stomp in something that should be easy. 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") Break down the sentence into words(results 4 words) Compare each word with all the other words(previosly stored) If a word matches obtain his category(ie. object or property) 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 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 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.
From: Francis Glassborow on 26 Jan 2008 06:10
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. > 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 ) And here we see a case in point. No current experienced user of C++ would make that their first choice, they would almost reflexively shange the type of 'command' to a std::string because that avoids so many potential problems (including the one you had). It is code like that the gave rise to the comment that you were using a mixture of C and C++. Though valid C++ that line is straight C. > std::cout >> "yes, it is a pencil"; He was guilty of a common typo and inverted the operator (almost all of us do that from time to time but fortunately compilers report it as an error -- there was a brief time about 1995 for four months when that code was valid but was rubbish and in that time Borland released a compiler that accepted it. The Standard's Committees quickly saw the error that caused the problem and corrected it at the next meeting. > The first line is what my laboratory teacher showed me but forgoten. And that worries me because it suggest s/he is thinking in C rather than C++. > 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. see above. > Probably is best that I learn more C++ at school but is frustrating to > stomp in something that should be easy. And is easy if you are taught the proper tools. Using C for this is what makes it hard. > 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 use getline() (in one of its two forms, my preference is for the free function that take a std::istream and an std::string as arguments, but opinions differ. What I would not do is use the extraction operator (operator>>) to obtain it because that uses whitespace as a delimiter. > Read sentence (ie. "the pencil is red") getline() will do that fine, the extraction operator won't, nor will using an array of char. > Break down the sentence into words(results 4 words) So you need a mechanism for parsing. There are several available but for a student I would ask them to write their own function (called for example "getword" with a declaration such as std::string getword(std::string & sentence); > Compare each word with all the other words(previosly > stored) Simple use of find() applied to an STL container of your choice. I think that is enough for starters. Get a program that will read a line from an input source and parse it outputting the individual words. When you have done that it will be time to add the other features step by step. |