help me i am noob

This is a discussion on help me i am noob within the C++ Programming forums, part of the General Programming Boards category; how do i write a name as a variable? like (Program)Whats your name? (User)John (Program)John, what you want do do? ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Location
    Brasil
    Posts
    220

    help me i am noob

    how do i write a name as a variable?
    like (Program)Whats your name?
    (User)John
    (Program)John, what you want do do?
    plz help me ^^ thank you guys

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    You probably want the name variable to be of type string. Then it's just a matter of I/O using cin/cout.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Example if you get baffled. (Like I did. )

    Code:
    ...
     
    char* name;
    cout << "What's you'r name?" << endl;
    cin >> name;
    cout << name << " what do you want to do? << endl;
     
    ...
    Last edited by Queatrix; 10-19-2005 at 10:16 AM. Reason: changed "string name;" to "char* name;".

  4. #4
    Registered User
    Join Date
    Oct 2005
    Location
    Brasil
    Posts
    220
    o man i love u thanks ^^

  5. #5
    Registered User
    Join Date
    Oct 2005
    Location
    Brasil
    Posts
    220
    damn I tried it, but it is not working why? maybe I have to put another lybrary besides <iostream>?

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    Quote Originally Posted by Cool-August
    Example if you get baffled. (Like I did. )

    Code:
    ...
     
    char* name;
    cout << "What's you'r name?" << endl;
    cin >> name;
    cout << name << " what do you want to do? << endl;
     
    ...
    Why the change from string to char*? That's a very dangerous move and going in the wrong direction. Stick to the string type.

    Quote Originally Posted by Scarvenger
    damn I tried it, but it is not working why? maybe I have to put another lybrary besides <iostream>?
    They are header files, not libraries and the one for the string container class happens to be <string>. You may need to put a using namespace std; statement after all your #include statements to get this working or use std:: all over the place. You may have also forgotten a doublequote if you copied the code posted by Cool-August as is.

    Code:
    #include <string>
    #include <iostream>
    
    int main()
    {
        std::string name;
    
        std::cout << "What's your name?" << std::endl;
        std::cin >> name;
        std::cout << name << ", what do you want to do?" << std::endl;
    
        return 0;
    }
    Last edited by hk_mp5kpdw; 10-19-2005 at 12:23 PM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    char* name;
    cout << "What's you'r name?" << endl;
    cin >> name;
    Did you ever hear of memory allocation?

    How about learning about nice safe std::strings for strings rather than massively unsafe C-style char arrays or the outright dangerous uninitialised char* pointers.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    also, if you're going to use std::cin or std::cin.get() for input, don't forget about the possibility of leaving garbage in the input stream. for example, if the user enters "john doe" and your next question is about their eye color, you're screwed.

    a simple std::cin.ignore(32000,'\n'); right after cin or cin.get() should do the trick.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Noob to programming need help with a project
    By Wheelsonbus in forum C Programming
    Replies: 6
    Last Post: 02-25-2009, 02:46 AM
  2. Noob in need of help.
    By gec5741 in forum C++ Programming
    Replies: 18
    Last Post: 01-23-2009, 02:25 PM
  3. Noob printf question
    By lolguy in forum C Programming
    Replies: 3
    Last Post: 12-14-2008, 07:08 PM
  4. noob needs help!!!!:(
    By tykenfitz in forum C Programming
    Replies: 1
    Last Post: 07-10-2005, 08:49 AM
  5. Just a little request from a noob...
    By Lee134 in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 03-18-2005, 04:45 AM

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