Stupid Question

This is a discussion on Stupid Question within the C++ Programming forums, part of the General Programming Boards category; Here's a stupid question, if a user enters there name into my program, I can't get it to say the ...

  1. #1
    Pokemon Master digdug4life's Avatar
    Join Date
    Jan 2005
    Location
    Mystic Island, NJ
    Posts
    91

    Stupid Question

    Here's a stupid question, if a user enters there name into my program, I can't get it to say the name back. I'm using an array, and i know theres a better way to do it, but i can't figure it out. Sorry for the trouble but this is a shortened of what i have

    Code:
    #include <cstdlib>
    #include <iostream>
    
    char name [30];
    
    int main()
    {
       std::cout <<"Welcome, please enter your name: ";
       std::cin >> name;
       std::cout <<"Hello " << name << ", how are things going";
    }
    Verbal Irony >>

    "I love english homework!" When really nobody like english homework.
    -Mrs. Jennifer Lenz (English Teacher)

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    use:

    Code:
    std::cin.get(name,29,'\n');
    instead.

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    41
    Quote Originally Posted by digdug4life
    Here's a stupid question, if a user enters there name into my program, I can't get it to say the name back. I'm using an array, and i know theres a better way to do it, but i can't figure it out. Sorry for the trouble but this is a shortened of what i have

    Code:
    #include <cstdlib>
    #include <iostream>
    
    char name [30];
    
    int main()
    {
       std::cout <<"Welcome, please enter your name: ";
       std::cin >> name;
       std::cout <<"Hello " << name << ", how are things going";
    }
    This is because you cant print an array like that. An array stores data at particular indexes or coordinates. for example for a one dimenesional array like the one you have now:

    Index: 0 |1| 2|3
    Eleme: M |I |K |E

    so for example if you cout<<name[0] it will output M.

    Therefor in order to output the whole thing you need to make a for loop that runs through the indexes and outputs the corresponding value. A better way to do this is to use string instead of an array.
    Last edited by blindman858; 05-14-2005 at 04:24 PM.

  4. #4
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    Just odd but it seems your program works fine for me? I just copied pasted and compiled and it worked fine?
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  5. #5
    Let's do some coding! Welshy's Avatar
    Join Date
    Mar 2005
    Location
    Staffordshire University, UK
    Posts
    168
    Why not just use the string data type, with the <string> header, and use getline(cin, name) instead of cin, job done
    Last edited by Welshy; 05-14-2005 at 04:37 PM. Reason: 100th post, w00t!

  6. #6
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Quote Originally Posted by blindman858
    This is because you cant print an array like that. An array stores data at particular indexes or coordinates. for example for a one dimenesional array like the one you have now:

    Index: 0 |1| 2|3
    Eleme: M |I |K |E

    so for example if you cout<<name[0] it will output M.

    Therefor in order to output the whole thing you need to make a for loop that runs through the indexes and outputs the corresponding value. A better way to do this is to use string instead of an array.
    ostream is design to work with character arrays (c-style strings) appropriately, so that is not an issue.

    Prefer jverkoey's method to your original one, because you can run into problems overrunning the bounds of your array.

    Don't forget to flush the buffer at the end: cout.flush(); or cout << endl;
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Quote Originally Posted by jverkoey
    use:

    Code:
    std::cin.get(name,29,'\n');
    instead.
    Or:
    Code:
    std::cin.get(name,30,'\n');

  8. #8
    Pokemon Master digdug4life's Avatar
    Join Date
    Jan 2005
    Location
    Mystic Island, NJ
    Posts
    91
    Ok, it still closes after the user presses enter, it still doesn't work

    Code:
    #include <cstdlib>
    #include <iostream>
    
    char name [30];
    
    int main()
    {
       std::cout <<"Welcome, please enter your name: ";
       std::cin.get(name,29,'\n');
       std::cout <<"Hello " << name << ", how are things going";
    }
    Last edited by digdug4life; 05-14-2005 at 07:14 PM.
    Verbal Irony >>

    "I love english homework!" When really nobody like english homework.
    -Mrs. Jennifer Lenz (English Teacher)

  9. #9
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    The issue you seem to be having is that the program is finishing, and your window has no reason to stay open once the program has finished. Try a) running your program from the command line so that the window won't close, or b) reading the FAQ. And you should still flush the output buffer.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  10. #10
    Pokemon Master digdug4life's Avatar
    Join Date
    Jan 2005
    Location
    Mystic Island, NJ
    Posts
    91
    And you should still flush the output buffer
    Umm...yea i don't know what that means. I don't know much computer lingo
    Verbal Irony >>

    "I love english homework!" When really nobody like english homework.
    -Mrs. Jennifer Lenz (English Teacher)

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Try this.
    Code:
    #include <cstdlib>
    #include <iostream>
    
    int main()
    {
       char name [30];
    
       std::cout <<"Welcome, please enter your name: ";
       std::cin.get(name,30,'\n');
       std::cout <<"Hello " << name << ", how are things going" << endl;
    
       std::cin.get();
    }
    Last edited by swoopy; 05-14-2005 at 07:44 PM.

  12. #12
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Flushing the output buffer ensures that what you have specified should be displayed will appear on the output device (screen).
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  13. #13
    Pokemon Master digdug4life's Avatar
    Join Date
    Jan 2005
    Location
    Mystic Island, NJ
    Posts
    91
    ok this isnt working, can someone telll me something to read on this site that might answer my question
    Last edited by digdug4life; 05-14-2005 at 08:02 PM.
    Verbal Irony >>

    "I love english homework!" When really nobody like english homework.
    -Mrs. Jennifer Lenz (English Teacher)

  14. #14
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Copy and paste it, it'll work.

  15. #15
    Pokemon Master digdug4life's Avatar
    Join Date
    Jan 2005
    Location
    Mystic Island, NJ
    Posts
    91
    no, i can't get it to work, theses are my errors
    C:\Dev-Cpp\Untitled1.cpp In function `int main()':
    C:\Dev-Cpp\Untitled1.cpp `endl' undeclared (first use this function)
    C:\Dev-Cpp\Makefile.win [Build Error] [Untitled1.o] Error 1
    Verbal Irony >>

    "I love english homework!" When really nobody like english homework.
    -Mrs. Jennifer Lenz (English Teacher)

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

Similar Threads

  1. Stupid Question
    By Dave3of5 in forum C++ Programming
    Replies: 7
    Last Post: 05-24-2006, 03:52 PM
  2. Stupid Question Probably
    By Kyrin in forum C Programming
    Replies: 2
    Last Post: 05-07-2006, 12:51 AM
  3. Replies: 7
    Last Post: 11-03-2005, 11:17 PM
  4. stupid, stupid question
    By xelitex in forum C++ Programming
    Replies: 5
    Last Post: 12-22-2004, 07:22 PM
  5. Stupid question: What does Debugger do?
    By napkin111 in forum C++ Programming
    Replies: 6
    Last Post: 05-02-2002, 10:00 PM

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