Thread: cin.get() Helppp!!!

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    48

    Question cin.get() Helppp!!!

    If somebody could point me in the right direction.

    instead of using cin.get() to extract characters from input such as keyboard and count them , than add them to variable such as num_of_characters , what would be an alternative in doing this but only using cin>> and it doesnt matter that spaces would be also counted..
    Also its a indefinite iteration....

    Please somebody assist on a begginers level

    Would greatly appreciate it...

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    You could use getline() to read in a line, or input up until a delimiter is encountered, then use the length() member of string to determine how long a string was read.
    Code:
    #include <string>
    .
    .
    std::string line;
    std::getline(std::cin, line);

  3. #3
    Code Injector Gaming's Avatar
    Join Date
    Mar 2008
    Posts
    19
    I prefer you use namespace std, so you don't have to use the standard scope for basic I/O.
    do this after you declare your libraries:
    using namespace std;
    and now you don't have to do std:: anymore

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    I prefer you use namespace std, so you don't have to use the standard scope for basic I/O.
    as well as you increase highly probabylity of naming conflicts...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Banned
    Join Date
    Nov 2007
    Posts
    678
    vart, a quick note here:
    the std stuff is so well known, so every library would avoid name conflict, besides, the other C++ libs may have their own namespace, naming conventions also.

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    the std stuff is so well known
    well time() function is also well known to me...
    still I write
    double time; here and there in my code... and don't catch it till PC-Lint warns me...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    The namespace discussion is off topic, but this thread might help explain why it is not so simple to just avoid naming conflicts with the std namespace: http://cboard.cprogramming.com/showthread.php?t=55269

    If someone prefers to explicitly state the namespace, I don't know why you would tell them not to.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cin.get(); not working with my switch statement
    By tenor_jazz13 in forum C++ Programming
    Replies: 2
    Last Post: 01-12-2008, 10:33 PM
  2. cin.get() curiosity
    By lilrayray in forum C++ Programming
    Replies: 3
    Last Post: 09-03-2006, 10:07 PM
  3. cin.get() problem
    By Cilius in forum C++ Programming
    Replies: 20
    Last Post: 07-28-2005, 05:32 PM
  4. Confused about cin.get(); and classes.
    By RaccoonKing in forum C++ Programming
    Replies: 6
    Last Post: 07-17-2005, 11:44 AM
  5. cin.get();
    By swgh in forum C++ Programming
    Replies: 2
    Last Post: 06-29-2005, 07:51 AM