Thread: null string?

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    83

    null string?

    in strings, what should i use? cin.getline or cin or getline(cin, var)?

    right now i'm using cin to take in 3 variables and then rearranging them. however if only 2 variables are filled in, then it should execute my instructions. however cin waits for the third variable to be inputted before proceeding. which i understand. what can i use to take in 3 variables but if 2 are given, then it should proceed?

    i was thinking of searching for a "null" string somehow and then use a conditional if statement.

    should i use getline to take the whole line as a string and then divide it accordingly?

    thanks,
    barneygumble742

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Use getline(cin, var) if var is a C++ string. Use an empty string instead of null unless an empty string is valid input. If you have three getlines, and the user just hits enter on the last one, then the 3rd string variable will be empty.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    83
    i can use the str.empty() function to test to see if a variable is empty. so that part is easy.

    however, i want to take in 3 variables in one line of input: first middle last. however i want to test the middle and last to see if either one is empty.

    is there any variation of cin or get or even getline that i can use?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Did you read the FAQ on getting a line of input from the user?

    *shakes magic 8-ball"

    "My sources say no."


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    take a look at getline's prototype: http://www.cppreference.com/cppstring/getline.html

    if that's too confusing, they break it down further in the other getline's prototype

    it's probably better to explicitly ask the user for their first, middle (optional), and last name seperately though... people do stupid things when you give them the freedom to.
    Last edited by major_small; 07-12-2005 at 08:09 PM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You don't need a variation. Just call getline and then check the string to see if its empty.

    The user will have to hit enter for each one, even if they don't type anything. If you don't want the user to have to hit enter for the second and third ones if they don't want to, then that is much more difficult and involves platform specific stuff.

  7. #7
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    no... if I understand correctly, what he wants is the user to enter:
    Code:
    <firstname> <middlename> <lastname>
    or
    Code:
    <firstname> <lastname>
    on the same line before hitting enter - and he wants to be able to differentiate between the two inputs.

    and the whole not hitting enter for the last one if they don't want to thing, there's nothing platform-specific about it... just whenever they enter an empty string, don't ask them for input anymore.
    Last edited by major_small; 07-12-2005 at 08:13 PM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Gotcha. And you're right, it's probably better to ask for each separately.

    I'd use getline to read the line and use a stringstream to parse each word in the line.

  9. #9
    Registered User
    Join Date
    Sep 2004
    Posts
    83
    yes. thanks daved. that's exactly what i'm thinking of.

    let's say that i have one whole line of strings. how can i separate strings from a line of strings?

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Sorry to just give code without explanation, but here you go:
    Code:
    #include <iostream>
    #include <sstream>
    #include <string>
    
    int main()
    {
    	std::string line("This is a line of text.");
    	std::istringstream istrm(line);
    	std::string word;
    	while (istrm >> word)
    		std::cout << word << std::endl;
    	std::cout << "---Done---" << std::endl;
    }

  11. #11
    Registered User
    Join Date
    Sep 2004
    Posts
    83
    is there another way to do it besides streaming? i've done it using characters but i guess it's best to do it using strings.

    is there a way to convert chars to strings?

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    There are other ways to do it, but IMO that way is the easiest. You'd probably want to give more specifics on what you are working with, like example code, so we can help you with alternative solutions.

  13. #13
    Registered User
    Join Date
    Sep 2004
    Posts
    83
    so far my program accomplishes the task. whereas it takes in FIRST MIDDLE LAST names and outputs LAST, FIRST M.

    i'm taking the whole line via getline and then searching for spaces. then using the spaces as markers to output from one marker to the other.

    right now my line is declared as "char line[30]" but i would like to do it via strings "string first, middle, last".

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Whichever way you are doing it now with C style strings you can also do it the same way with C++ strings. However, the stringstream might be easier.

    Look at the example. A stringstream works just like a file stream or cin. Just load in the first name by calling stream >> firstname, then the middle name by stream >> middlename, etc. You can also check the return value of those operations to tell if the user didn't type enough words.

  15. #15
    Registered User
    Join Date
    Sep 2004
    Posts
    83
    to be honest with you...we haven't covered it in class so i don't know if i'm allowed to stream it. although you did open my mind up to other possibilities...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. addrinfo Structure Not Working
    By pobri19 in forum Networking/Device Communication
    Replies: 9
    Last Post: 10-22-2008, 10:07 AM
  2. . . . . . . - . . . - -
    By The Brain in forum C++ Programming
    Replies: 17
    Last Post: 05-17-2005, 04:01 AM
  3. Help with yacc/compiler design/seg fault
    By trippeer in forum C Programming
    Replies: 1
    Last Post: 04-08-2005, 03:43 AM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. File Reading and storing to 1 variable
    By Rare177 in forum C Programming
    Replies: 34
    Last Post: 07-13-2004, 12:58 PM