Problems With C++

This is a discussion on Problems With C++ within the C++ Programming forums, part of the General Programming Boards category; Hey everybody! I just started to use C++ programming and haven't used any other kind before. After a few days ...

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

    Problems With C++

    Hey everybody! I just started to use C++ programming and haven't used any other kind before. After a few days of learning how to use it, I tried to make this program. A have a few friends coming over and I made it to try to play a trick on them so that it says a funny quote when they input their specific names. I also want it to say a different quote if anyone else tries to input a name that isn't one of the few specified ones. I have encountered a problem, can anyone point out what I did wrong? Much appreciated =D

    insert
    Code:
    // If all of this works the way I want it to...this will be quite entertaining
    
    #include <iostream>
     int main ()
     {
    	 int name;         // Need a variable (name)
         cout<<"Hello World! I am Scott's most interesting and entertaining C++ program yet!"\n;
    	 cout<<"To begin, please enter your name:";
    cin>> name;           // Inputs name
    cin.ignore ()         // Throws away "enter"
    if (name == Scott) {
    	cout<<"HAHA! That is a moronic name! Why did your parents ever name you that! Geez, your parents must love you a lot! OH! MASTER SCOTT! I didn't realize it was you, can I ever repay you for my injustice? I bow down to you and your awesomeness!"\n;
    }
    else if (name == Gabby) {
    	cout<<"Wow...I must be a psychic C++ program!  I can already tell that yours is a name of someone who studies way too much! Get a hobby, punk!"\n;
    }
    else if (name == Sarah) {
    	cout<<"Wow...I must be a psychic C++ program!  I can already tell that yours is a name of someone who has way too much energy for some situations! Take a chill pill and let your endorphins stop pumping!"\n;
    }
    else if (name == Alex) {
    	cout<<"Wow...I must be a psychic C++ program!  I can already tell that yours is a name of someone who has way too hairy of a chest and like Guitar Hero too much! Stop playing, or else your fingers will fall off!"\n;
    }
    else if (name == Christian) {
    	cout<<"Wow...I must be a psychic C++ program!  I can already tell that yours is a name of someone who plays way too many video games!  I must say that your computer is rather 1337 and I must find the opportunity to meet it and have a byte to eat sometime. Wanna hook me up?"\n;
    }
    else if (name != Scott, Gabby, Sarah, Alex, Christian) {
    	cout<<"HEY! Who do you think you are!?  Did I let you bask in the glory of Scott's Almighty C++ Program!  Cause that's what I am!  If you can't handle it, go cry in the corner!"\n;
    }
    cin.get ();
     }

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,635
    Encountered a problem? You aren't writing valid code at all.
    I suggest you go back to the books or tutorials or wherever you are learning from and study some more!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,781
    You want to compare strings, so you should use a string (e.g., std::string), not an int, variable. Besides that, names like Scott and Gabby should be string literals, i.e., "Scott" and "Gabby".

    What book are you using to learn C++?
    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

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    4
    I'm not using a book, but rather this website and http://www.cplusplus.com/doc/tutorial/
    I'm not sure what I'm doing wrong...

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,635
    1) How the heck are you supposed to read a name (string) as an integer? You must read into std::string.
    2) Strings are marked as such by using "", so "test" is a string and simply test is not.
    3)
    Code:
    else if (name != Scott, Gabby, Sarah, Alex, Christian) {
    Not valid code. You must check if name is not equal to Scott AND it's not equal to Gabby AND, and so on. Logical and is an operator named &&.
    4) \n is a string literal, so it must be inside a string and not outside -> "test"\n BAD! "test\n" GOOD.

    All these mistakes suggest you don't actually know the language you're trying to write, so I do suggest you go back to reading tutorials or getting a book and properly learning the language first.
    Last edited by Elysia; 01-12-2008 at 12:37 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    4
    So am I supposed to put std::string in for int name ? What else do I need to do after that?

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,635
    You're supposed to learn the language.
    There's no use in asking all these questions because you obviously do not understand the basic syntax. Books are great on teaching all of that.
    You can look at strings, reading strings and if statements.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,781
    I'm not using a book, but rather this website and http://www.cplusplus.com/doc/tutorial/
    I'm not sure what I'm doing wrong...
    There are many, many errors. Online resources are fine, but I suggest that you read You Can Do It! A Beginner's Introduction to Computer Programming by Glassborow or Accelerated C++ by Koenig and Moo.

    So am I supposed to put std::string in for int name ? What else do I need to do after that?
    Below is an example program that does roughly what you want to do. You can compare it to your current attempt and spot the differences, though it may not make that much sense to you unless you learn more.
    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
        std::cout << "To begin, please enter your name: ";
        std::string name;
        std::cin >> name;
        std::cin.ignore(); // Throws away "enter".
    
        if (name == "Scott")
        {
            std::cout << "HAHA! That is a moronic name!\n";
        }
        else if (name == "Gabby")
        {
            std::cout << "Get a hobby, punk!\n";
        }
        else if (name == "Sarah")
        {
            std::cout << "Take a chill pill and let your endorphins stop pumping!\n";
        }
        else if (name == "Alex")
        {
            std::cout << "Stop playing, or else your fingers will fall off!\n";
        }
        else if (name == "Christian")
        {
            std::cout << "Wanna hook me up?\n";
        }
        else
        {
            std::cout << "If you can't handle it, go cry in the corner!\n";
        }
    
        std::cin.get();
    }
    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

  9. #9
    Registered User
    Join Date
    Jan 2008
    Posts
    4
    OH! I get it! Thank you both so much for your help! =]

  10. #10
    The larch
    Join Date
    May 2006
    Posts
    3,573
    And the trick to avoid extremely long lines is that you can break up string literals:
    Code:
    cout << "this is some very long text "
            "but thankfully I can split it up "
            "over several lines";
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,635
    You can also save lines and width by outputting common text:
    Code:
    if (name == "Gabby" || name == "Sarah")
    {
    	cout << ""Wow...I must be a psychic C++ program!  I can already tell that yours is a name of someone who ";
    	if (name == "Gabby") cout << "studies way too much! Get a hobby, punk!\n";
    	else if (name == "Sarah") cout << "has way too much energy for some situations! Take a chill pill and let your endorphins stop pumping!\n";
    }
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    And \n comes inside the quotes.........or you can use <<endl;

    And you forgot to add return 0;

  13. #13
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    Quote Originally Posted by scottydosntkno View Post
    I'm not using a book, but rather this website and http://www.cplusplus.com/doc/tutorial/
    I'm not sure what I'm doing wrong...
    Also try this:

    www.learncpp.com

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,781
    And you forgot to add return 0;
    Explicitly returning 0 at the end of the global main() function is optional in C++.
    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

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Quote Originally Posted by laserlight View Post
    Explicitly returning 0 at the end of the global main() function is optional in C++.
    After a recent debate on this board both on whether the existence of the implicit return 0 was a good idea, and whether using it is bad practice, I checked and found that Stroustrup himself uses it - a casual perusal of TC++PL indicates that he never uses an explicit "return 0;" at the closing brace, and in this page

    http://www.research.att.com/~bs/bs_faq2.html

    which was last updated 9 days ago (*), only the first two examples of main() use an explicit return 0, presumably for pedagogical reasons (he's emphasizing that main() returns int) and none of the dozen or so that follow.

    (*) Edit: According to Firefox's Page Info - the text itself says October 10. Anyway, it's much more recent than TC++PL.
    Last edited by robatino; 01-13-2008 at 06:27 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 05:35 AM
  3. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 04:48 PM
  4. contest problems on my site
    By DavidP in forum Contests Board
    Replies: 4
    Last Post: 01-10-2004, 08:19 PM
  5. DJGPP problems
    By stormswift in forum C Programming
    Replies: 2
    Last Post: 02-26-2002, 03:35 PM

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