Thread: Incredibly simple code with an incredible list of errors

  1. #16
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I'm so mad at this thread.
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main() {
       string name;
       cout << "Hello, what is your name?";
       getline(cin, name);
       cout << "Hello " << name;
       cin.get();
    }
    Thread over.

  2. #17
    Registered User
    Join Date
    Jun 2006
    Posts
    16
    Worked great. You have to remember, however, that I don't really need this program for practical purposes, it's to learn the language. Thus I must ask what purpose the getline (cin, name) line has in this particular program?

  3. #18
    Registered User
    Join Date
    Dec 2004
    Location
    UK
    Posts
    109
    The problem you described occurs only under windows when you launch a console application from otuside the console.

    Windows will start a shell, run your program and terminate the shell as soon as the program terminates. When the shell terminates the window it ran in is closed.

    The solution is, as citizen shows in his code, to make your program wait for further input when it has finished executing.

    getline(cin, name) reads one line of input from the cin stream into the name variable

    cin >> name by contrast will only read till the next separator (any white space by default) if I recall correctly
    Last edited by sigfriedmcwild; 06-26-2006 at 05:43 PM.

  4. #19
    Registered User
    Join Date
    Jun 2006
    Posts
    16
    Alright then, I think I understand that. Basically the program would normally terminate upon reaching a space, but using this method it will read an entire line? Just making sure I'm clear on all this...

  5. #20
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Better yet, go read the tutorials! most of these problems are solved in the tutorials with explination...

  6. #21
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Changing specs mid thread becomes rather confusing, don't you agree?

    Anyways, yes. It reads the entire line from the input and places the contents on the variable called name.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  7. #22
    Registered User
    Join Date
    Jun 2006
    Posts
    16
    I've read all the tutorials I think I'd understand. I've read the beginner tutorials all the way through, but when I see a tutorial that explains how to use a function I've never even heard of or can't imagine how to implement, that kind of puts up a red flag that I shouldn't even bother.

    Changing specs mid thread becomes rather confusing, don't you agree?
    What precisely have I done wrong? I still am running on the same computer with the same hardware, but I can't imagine that's what you mean.

    Thanks though, everyone, finally the program is finished and I understand how it works.
    Last edited by Twitchmokey; 06-26-2006 at 05:51 PM. Reason: Wha?

  8. #23
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> that didn't seem to work, at least not in the way I used it.
    The cin.get() has to be at the end of the function. The "Hello" won't be printed if it comes after the cin.get(). The whole point of the cin.get() is to pause the program so that you can see the output before it closes.

    Also, the getline function allows you to type in a name with spaces. If you use cin >> to read in a name, you cannot type a name with a space in it. If you are only typing a one word name, your code will work if you just move the cin.get() to the end of the main function.

  9. #24
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Quote Originally Posted by Twitchmokey
    What precisely have I done wrong?
    Actually nothing. My bad. I missed the post where you comment about pausing the console. That's when cin.get() become the focus.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  10. #25
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    > when I see a tutorial that explains how to use a function I've never even heard of or
    > can't imagine how to implement, that kind of puts up a red flag that I shouldn't even bother.
    Most tutorials will write the functions they use somewhere if they are nonstandard. If they are standard, the author expects that you understand enough to use that function already, or that you will look it up with your compiler's help file, or read the UNIX man pages.

    Reading the man pages is a really easy thing with search engines. Just type "unix man thing" and click Search or Go, or Fetch, whatever.

  11. #26
    Registered User
    Join Date
    Jun 2006
    Posts
    16
    If I read all the tutorials will you promise not to get angry with me anymore? These bad waves are totally harshing my good vibes. Harshing I say!

    I go now.

  12. #27
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Calm down. It's the internet: the one place where everyone is passive agressive.

    When we say "go read a tutorial" we are not giving you a punishment. By no means do you have to read ALL the tutorials... You are going to need to read a lot when you learn anything new and frankly no one here wants to retype an entire article for the sake of one person's understanding. No one wants to retype the standard either, because quzah wrote it so well. The whole point of my last post was to tell you how to read a tutorial effectively.

  13. #28
    Registered User
    Join Date
    Jun 2006
    Posts
    16
    Well, I'm going through them now, and getting about 25% comprehension. With that in mind, I think I'll have to read them all to be any good. The main problem with these things is they're abstract in that they describe how to better handle things I can't even handle in the first place. Like references, they're something about making references to static variables from a different class or outside a class (at least that's what I got out of them), but I have no idea how to even set up a class. I vaguely remember something about constructors and destructors and how destructors don't except arguments....or was it that they took no prisoners? Either way...I'll get it eventually.

  14. #29
    Registered User
    Join Date
    May 2006
    Posts
    2
    Well, I'm going through them now, and getting about 25% comprehension. With that in mind, I think I'll have to read them all to be any good. The main problem with these things is they're abstract in that they describe how to better handle things I can't even handle in the first place. Like references, they're something about making references to static variables from a different class or outside a class (at least that's what I got out of them), but I have no idea how to even set up a class. I vaguely remember something about constructors and destructors and how destructors don't except arguments....or was it that they took no prisoners? Either way...I'll get it eventually.
    I know how you feel man, keep working at it though, it took me a while to get basic concepts on c++....just keep on practicing, without giving up, and it will fall into place for you.

  15. #30
    System.out.println("");
    Join Date
    Jan 2005
    Posts
    84
    I feel you too. Something that seems to work for me is to read the tutorials here. Then read them again. Then go and find someone elses tutorials on the same topic. Seeing different examples and different perspectives on the same topic helps me understand things better.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  2. Pleas take a look & give a critique
    By sh3rpa in forum C++ Programming
    Replies: 14
    Last Post: 10-19-2007, 10:01 PM
  3. Anyone good with linked list.....I am not....
    By chadsxe in forum C++ Programming
    Replies: 11
    Last Post: 11-10-2005, 02:48 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM