Just started learning C++

This is a discussion on Just started learning C++ within the C++ Programming forums, part of the General Programming Boards category; Ok, I just started learning C++ so please bear with me. Me and a friend are learning together and making ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    5

    Just started learning C++

    Ok, I just started learning C++ so please bear with me. Me and a friend are learning together and making a simple program where someone enters their name and have the program print their name.
    Code:
    int name;
    cout << "Enter your name:\n";
        cin >> name;
        cout << "your name is " << name << ".\n";
    Thats just a little bit of the code, im just not sure how to get it working because when i type in any name at all it prints this out: Your name is 2.
    I don't know why it keeps printing 2 instead of the name.
    Thanks guys.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Matt51723 View Post
    Code:
    int name;
    How many people do you know who have numbers (integers) for names? Try std::string.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    cue: "I'm a person, not a number".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    5
    In my c++ class our teacher said use int, wow thanks for the jackass answer matsp. thanks for being an .............. to someone who literally just started learning a few days ago. [sarcasam]ur gonna make some son or daughter real happy[/sarcasm]

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Matt51723 View Post
    In my c++ class our teacher said use int, wow thanks for the jackass answer matsp. thanks for being an .............. to someone who literally just started learning a few days ago. [sarcasam]ur gonna make some son or daughter real happy[/sarcasm]
    Look, I was just trying to make a joke - obviously, it didn't quite cross over the internet quite as funny as I thought it was. And I replied AFTER you had the correct answer, so I didn't see what the problem with that would be.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    5
    Thank you, im just a little angry because i went to another support forum and got flammed to death, ill try not to be an .............. back.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Matt51723 View Post
    In my c++ class our teacher said use int,
    I would be willing to bet any amount of money that this is not true. Your instructor may have told you to use an int in some other situation; your instructor did not tell you to use an int to hold a string. As you have noticed, it can't be done.
    Quote Originally Posted by Matt51723 View Post
    wow thanks for the jackass answer matsp. thanks for being an .............. to someone who literally just started learning a few days ago. [sarcasam]ur gonna make some son or daughter real happy[/sarcasm]
    Yes! I get to give a jackass answer and mats gets the blame! I love Christmas.

    [edit]: Or at least I read my response as much more snarky than mats'. We'll chalk it all up to insufficient smiley's. [/edit]
    Last edited by tabstop; 12-19-2007 at 08:15 PM.

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    8

    Strings

    Here is a valid code for what you are trying to do.

    This makes the name a string and uses char instead of int because you are using the alphabet and not integers ( Numbers ).

    If you need more help on strings try this tutorial

    http://www.cprogramming.com/tutorial/lesson9.html

    This code it modeled after it.
    Attached Files Attached Files

  9. #9
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Quote Originally Posted by Matt51723 View Post
    Thank you, im just a little angry because i went to another support forum and got flammed to death, ill try not to be an .............. back.
    You probably got flamed because you're a hot-head and picked a fight with the people there.

    I mean come on.... You got angry at matsp. He's so hard to pick a fight with. lol....
    Last edited by MacGyver; 12-19-2007 at 11:08 PM.

  10. #10
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479
    You may also want to learn about getline() which reads a spaced string.

    Code:
    std::string name;
    
    std::cout << "Enter your full name: ";
    std::getline ( std::cin, name );
    I'm just trying to be a better person - My Name Is Earl

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Machine Learning with Lego Mindstorms
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 01-30-2009, 01:34 PM
  2. Is learning C/C++ worth it?
    By C/C++ Learner in forum A Brief History of Cprogramming.com
    Replies: 44
    Last Post: 11-05-2008, 02:55 PM
  3. Best Approach for Learning
    By UCnLA in forum C Programming
    Replies: 5
    Last Post: 03-21-2008, 02:35 AM
  4. Help getting started :(
    By blackocellaris in forum C Programming
    Replies: 4
    Last Post: 11-05-2006, 05:50 PM
  5. Need help getting started
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 09-02-2001, 11:08 PM

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