string problems

This is a discussion on string problems within the C++ Programming forums, part of the General Programming Boards category; i am trying to make a spanish program whereit gives you a word to translate and you must translate it ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    4

    string problems

    i am trying to make a spanish program whereit gives you a word to translate and you must translate it to english. if you get it right it says correct and if wrong it says incorrect. (this is just for learning purposes.)
    Code:
    #include <iostream>
    using namespace std;
    string dormir;
    int main()
    {
           cout<<"welome to my spanish program!\n";
           cout<<"Translate each spanish word into english. Here is the first question out of ten.\n";
           cout<<"Dormir\n";
           cin>>dormir;
           cin.ignore();
           if (sleep == dormir) { cout<<"correct!\n"; }
           else if (dormir != sleep) { cout<<"incorrect...\n"; }
           system("Pause");
    
    }

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    You didn't tell us what problems you're having, but just from looking at the code, I'm guessing it's complaining about:
    Code:
    if (sleep == dormir)
    since you didn't define a variable called sleep.
    If you're trying to compare the dormir variable against the string "sleep" then you need to put it in quotes like that.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    4
    o ya. sorry. i can't believe i actually forgot to say what was wrong. i'm stupid. that was the problem anyway. thx.

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    you do not need dormir var to be global

    if you are planning to have new var for each word user needs to enter - it is also something overstated - enough to have
    std::string word;
    to store each input one by one in this word and compare it to the corresponding hard-coded string
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Don't forget to #include <string>.

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    and while you're at it, don't forget to return a value from main(), usually return 0; if there were no problems, or return some other value if an error occurred.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,442
    and while you're at it, don't forget to return a value from main(), usually return 0; if there were no problems, or return some other value if an error occurred.
    This is the C++ forum though, and in C++ that explicit return 0 is optional.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by laserlight View Post
    This is the C++ forum though, and in C++ that explicit return 0 is optional.
    Just because it's optional, doesn't mean you shouldn't get into the habit of doing it.
    Why is it optional anyways? It's not like it takes that much time to type 9 characters.

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    It was to make the conversion from legacy void main programs easier.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    I'm guessing it's optional because many pre-standard (and unfortunately current) programs used void main, so it would be a simple change to make a main function without a return just return 0 automatically.

    I personally prefer not to add the return 0 to main unless I'm specifically giving meaning to the return values. But I wouldn't argue with someone if they preferred the opposite.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,442
    Just because it's optional, doesn't mean you shouldn't get into the habit of doing it.
    There is no benefit to adopting that habit other than consistency with normal functions, and that consistency is an illusion since the global main function is different anyway (e.g., it cannot be used within the program).

    Why is it optional anyways? It's not like it takes that much time to type 9 characters.
    Someone in this forum community speculated that the standard committee was trying to make it easier for the void main bunch to convert their programs. Personally, I would like to ask them and find out. I suspect that the rather lame "less unnecessary typing" reason might actually have been a (secondary?) motivation.

    EDIT:
    I personally prefer not to add the return 0 to main unless I'm specifically giving meaning to the return values. But I wouldn't argue with someone if they preferred the opposite.
    Same here. I find the issue sufficiently trivial that as long as it complies with the standard, I really do not care if it is not what I would write.
    Last edited by laserlight; 05-04-2008 at 12:51 PM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by laserlight View Post
    There is no benefit to adopting that habit other than consistency with normal functions, and that consistency is an illusion since the global main function is different anyway (e.g., it cannot be used within the program).
    But it returns to the OS, where whatever launched it (shell script, batch file, etc.) will usually care if it terminated normally or not. What is returned if the explicit return is elided? Is it undefined?

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,442
    But it returns to the OS, where whatever launched it (shell script, batch file, etc.) will usually care if it terminated normally or not. What is returned if the explicit return is elided? Is it undefined?
    0 is returned.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  3. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  4. io stream and string class problems
    By quizteamer in forum C++ Programming
    Replies: 2
    Last Post: 04-25-2005, 12:07 AM
  5. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21