newbie needs help with user input into strings

Discussion in 'C++' started by rob, Oct 17, 2007.

  1. rob

    rob Guest

    hello all. im a newbie in C++ and programing in general. what i am
    having problem with is taking user input, putting it into a string and
    then displaying that string again. i am using the dev C++ compiler.
    here is the code

    #include <iostream>
    #include <stdlib.h>
    #include <string>

    using namespace std;

    int main(int argc, char *argv[])
    {

    string name;

    cout << "Enter name: ";
    cin >> name;
    cout << endl << "Your name is " << name << endl;

    cin.get();

    return 0;
    }

    it will let me input what ever i want but nothing happens when i hit
    enter, the window just stays blank until i hit enter again then the
    window closes.

    thanks for any help
     
    rob, Oct 17, 2007
    #1
    1. Advertising

  2. * rob:
    > hello all. im a newbie in C++ and programing in general. what i am
    > having problem with is taking user input, putting it into a string and
    > then displaying that string again. i am using the dev C++ compiler.


    You're using the Dev C++ integrated development environment (IDE).

    Probably that means you're using the g++ compiler.

    But other compilers can be used with the same IDE.


    > here is the code
    >
    > #include <iostream>
    > #include <stdlib.h>
    > #include <string>
    >
    > using namespace std;
    >
    > int main(int argc, char *argv[])
    > {
    >
    > string name;
    >
    > cout << "Enter name: ";
    > cin >> name;
    > cout << endl << "Your name is " << name << endl;
    >
    > cin.get();
    >
    > return 0;
    > }
    >
    > it will let me input what ever i want but nothing happens when i hit
    > enter, the window just stays blank until i hit enter again then the
    > window closes.


    Try running your program from a command interpreter.

    For more help with that consult a group dedicated to your operating system.

    Cheers, & hth.,

    - Alf

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Oct 18, 2007
    #2
    1. Advertising

  3. rob

    Guest

    On Oct 17, 5:41 pm, rob <> wrote:
    > hello all. im a newbie in C++ and programing in general. what i am
    > having problem with is taking user input, putting it into a string and
    > then displaying that string again. i am using the dev C++ compiler.
    > here is the code
    >
    > #include <iostream>
    > #include <stdlib.h>
    > #include <string>
    >
    > using namespace std;
    >
    > int main(int argc, char *argv[])
    > {
    >
    > string name;
    >
    > cout << "Enter name: ";
    > cin >> name;
    > cout << endl << "Your name is " << name << endl;
    >
    > cin.get();
    >
    > return 0;
    >
    > }
    >
    > it will let me input what ever i want but nothing happens when i hit
    > enter, the window just stays blank until i hit enter again then the
    > window closes.
    >
    > thanks for any help



    // there is no need to be including arguments u don't need.
    // i just compiled this in .NET and it works fine
    //when u running the code make sure u not in debug mode so it dosen't
    blow by.




    #include <iostream>
    #include <string>


    using namespace std;

    int main()
    {
    string name;

    cout <<"Enter your name _\b";

    cin >> name;

    cout <<endl;

    cout << name<< endl;

    return 0;


    }
     
    , Oct 18, 2007
    #3
  4. rob

    utab Guest

    On Oct 18, 12:41 am, rob <> wrote:
    > hello all. im a newbie in C++ and programing in general. what i am
    > having problem with is taking user input, putting it into a string and
    > then displaying that string again. i am using the dev C++ compiler.
    > here is the code
    >
    > #include <iostream>
    > #include <stdlib.h>
    > #include <string>
    >
    > using namespace std;
    >
    > int main(int argc, char *argv[])
    > {
    >
    > string name;
    >
    > cout << "Enter name: ";
    > cin >> name;
    > cout << endl << "Your name is " << name << endl;
    >
    > cin.get();
    >
    > return 0;
    >
    > }
    >
    > it will let me input what ever i want but nothing happens when i hit
    > enter, the window just stays blank until i hit enter again then the
    > window closes.
    >
    > thanks for any help


    Something related to implementation, There was sth related to conio
    header and pause() function, try them and see what happens, your code
    looks fine at first sight.

    cin.get() just extracts the newline in the stream, and you are not
    using that for anything why did you put that there?

    HTH, Regards
     
    utab, Oct 18, 2007
    #4
  5. rob

    osmium Guest

    "rob" writes:

    > hello all. im a newbie in C++ and programing in general. what i am
    > having problem with is taking user input, putting it into a string and
    > then displaying that string again. i am using the dev C++ compiler.
    > here is the code
    >
    > #include <iostream>
    > #include <stdlib.h>
    > #include <string>
    >
    > using namespace std;
    >
    > int main(int argc, char *argv[])
    > {
    >
    > string name;
    >
    > cout << "Enter name: ";
    > cin >> name;
    > cout << endl << "Your name is " << name << endl;
    >
    > cin.get();
    >
    > return 0;
    > }
    >
    > it will let me input what ever i want but nothing happens when i hit
    > enter, the window just stays blank until i hit enter again then the
    > window closes.


    Sometimes one cin.get() is not enough in DevC, your program needs two of
    them To avoid such problems, I use this macro

    #define STOP while(1) std::cin.get();

    and then call it to freeze the display. It also looks neater, there are
    other occasions where you will have to use the STOP macro as well.
     
    osmium, Oct 18, 2007
    #5
  6. rob

    David Harmon Guest

    On Wed, 17 Oct 2007 22:41:31 -0000 in comp.lang.c++, rob
    <> wrote,
    >having problem with is taking user input, putting it into a string


    Look to the std::getline() function before anything else for that.
     
    David Harmon, Oct 18, 2007
    #6
  7. rob

    rob Guest

    forgive my lack of knowledge but i didn't understand most of what
    anyone said.

    1) im not sure how to run it in a command interpreter but i will look
    into it.

    2) i used #include <stdlib.h> because it was in the book, the book
    hasn't really explained what any of this does in detail yet. Im ---
    pretty sure im not in debug mode, i just use compile and run to
    execute the program.

    3) i used cin.get() at the end, so the program won't just quit when
    its done and waits so i can actually see the output. is that ok? or is
    ---there a better way of doing this?

    4) im not sure what you mean by that. but i took cin.get() out all
    together and the program still acts the same way.

    5) I have read up a bit about it on the internet but none of what i
    read has really helped. could someone please point me in the right ---
    direction?
     
    rob, Oct 19, 2007
    #7
  8. rob

    osmium Guest

    "rob" wrote:

    > forgive my lack of knowledge but i didn't understand most of what
    > anyone said.


    Did you try what I proposed? It *did* work for me on DevC. If the word
    "macro" has no meaning to you, here is a chance to try to figure out what it
    means. Wikipedia is often helpful in explaining geek-speak.
     
    osmium, Oct 20, 2007
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Catalina Scott A Contr AFCA/EVEO

    Newbie needs help with regex strings

    Catalina Scott A Contr AFCA/EVEO, Dec 14, 2005, in forum: Python
    Replies:
    5
    Views:
    377
    Michael Spencer
    Dec 14, 2005
  2. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    798
    Malcolm
    Jun 24, 2006
  3. Chris Carlen
    Replies:
    1
    Views:
    635
    Gabriel Genellina
    Sep 18, 2007
  4. Joe Todd
    Replies:
    2
    Views:
    393
    Joe Todd
    Nov 12, 2008
  5. Peter Vanderhaden

    Newbie needs help getting user input

    Peter Vanderhaden, Sep 30, 2007, in forum: Ruby
    Replies:
    12
    Views:
    191
    Sebastian Hungerecker
    Sep 30, 2007
Loading...

Share This Page