Newbie Help!

This is a discussion on Newbie Help! within the C++ Programming forums, part of the General Programming Boards category; Hi everyone, I'm a newbie to c++ and i was wondering if there is a script to do this: I ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    17

    Newbie Help!

    Hi everyone,

    I'm a newbie to c++ and i was wondering if there is a script to do this:

    I want it to be able to know the name that has just bin entered. i.e

    string name;
    cout << " Please enter your name: " << endl;
    cin >> name;

    if(name == bob)
    {
    cout << " COOL " << endl;
    {

    I know that that will not work.

    Is there a script that will work?

    thanks for your.

    Swink.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    bob is a variable. "bob" is the name "bob".

  3. #3
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    I won't write the EXACT code but I can point to the general direction.

    1. You need to indent your code and put it in the "CODE" tag on the forum.

    2.
    Code:
    if(name == bob)
    {
    cout << " COOL " << endl;
    {
    See anything peculiar?

    3. The "equal to"/ == can't compare a string. It would only compare a char / short / int / long / float whereas a string is actually a pointer of char (char *). You can use "strcmp" instead.
    ERROR: Brain not found. Please insert a new brain!

    “Do nothing which is of no use.” - Miyamoto Musashi.

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Try something like (name.compare("bob") == 0) as your comparison.

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    17
    Im sorry but i dont understand anything.

    That is how much of a newbie i am.

    Will you be able to explain it to me?

    But thanks for your help much appreciated.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    >> Im sorry but i dont understand anything.
    Swink, you have only one small problem. name is a string variable in your code. You want to compare it to a specific string, not another variable. When you typed bob without quotes, the compiler thought you were talking about a variable. You need to put bob in quotes to tell the compiler that you want to use the actual string, not a variable.



    >> The "equal to"/ == can't compare a string.
    This is wrong. The OP is using C++ strings, and == works fine for those.

    >> Try something like (name.compare("bob") == 0) as your comparison.
    Why? The existing code is better and clearer than that.

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Instead of this:

    Code:
    if(name == bob)
    Try this:

    Code:
    if(name.compare("bob") == 0)

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    >> Try this: if(name.compare("bob") == 0)

    I assume you posted while I was, so I'll just repeat it: I disagree. You made two changes. The change to use compare is unnecessary and adds confusion. The other change (to use quotes) is the only change necessary.

  9. #9
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    Quote Originally Posted by Daved View Post
    >> The "equal to"/ == can't compare a string.
    This is wrong. The OP is using C++ strings, and == works fine for those.
    Oh right, I didn't see the "string name;" part. I assume it was char * name. This is confusing without the code tags.

    BTW, what Daved say is correct. bob without the aphostrope is just another name to a variable.
    ERROR: Brain not found. Please insert a new brain!

    “Do nothing which is of no use.” - Miyamoto Musashi.

  10. #10
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Quote Originally Posted by Daved View Post
    I assume you posted while I was
    Indeed. I'm sort of preoccupied and didn't notice your post. My apologies for any misinformation caused on my part.

  11. #11
    Registered User
    Join Date
    Jul 2008
    Posts
    17
    Quote Originally Posted by MacGyver View Post
    Instead of this:

    Code:
    if(name == bob)
    Try this:

    Code:
    if(name.compare("bob") == 0)
    Thanks you so much.

    Is there any website that have tutorials that you could show.

    Thanks again tho much appreciated.

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    >> Thanks you so much.
    Please read the thread thoroughly. While MacGyver's solution works, it is not the best choice. You also just copy it without understanding, which doesn't help you learn.

    I'm curious, do you understand the small, simple mistake you made in the original code?

  13. #13
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    Quote Originally Posted by Swink View Post
    Thanks you so much.

    Is there any website that have tutorials that you could show.

    Thanks again tho much appreciated.
    I think the tutorials here will do just fine. And if they are not enough, you can just buy one of the books recommended here.
    ERROR: Brain not found. Please insert a new brain!

    “Do nothing which is of no use.” - Miyamoto Musashi.

  14. #14
    Registered User
    Join Date
    Jul 2008
    Posts
    17
    Quote Originally Posted by Daved View Post
    >> Thanks you so much.
    Please read the thread thoroughly. While MacGyver's solution works, it is not the best choice. You also just copy it without understanding, which doesn't help you learn.

    I'm curious, do you understand the small, simple mistake you made in the original code?
    I understand it now.
    The problem with mine was you cant == a string it has to be a int etc.

  15. #15
    Registered User
    Join Date
    Jul 2008
    Posts
    17
    Quote Originally Posted by g4j31a5 View Post
    I think the tutorials here will do just fine. And if they are not enough, you can just buy one of the books recommended here.
    I didn't know that you had tutorials.
    I just search on google for a forum to ask my question and this forum came up. With the amount of info that you lot know i think that i will take a look at the tutorials.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getting to grips with allegro and ms vc++ (newbie)
    By jimjamjahaa in forum C++ Programming
    Replies: 4
    Last Post: 11-18-2005, 06:49 PM
  2. Newbie in problem with looping
    By nrain in forum C Programming
    Replies: 6
    Last Post: 11-05-2005, 11:53 AM
  3. Newbie Programmer
    By Extropian in forum C++ Programming
    Replies: 3
    Last Post: 05-18-2004, 01:17 PM
  4. C++ newbie / linux not so newbie question
    By goldmonkey in forum C++ Programming
    Replies: 7
    Last Post: 12-13-2003, 11:27 AM
  5. Newbie Game Develpoers Unite!
    By Telenosis in forum Game Programming
    Replies: 10
    Last Post: 06-22-2002, 02:02 PM

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