Check if a cin is null

This is a discussion on Check if a cin is null within the C++ Programming forums, part of the General Programming Boards category; Simple question... But not so sure if it's an easy answer... I want to check if a cin is null, ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    9

    Check if a cin is null

    Simple question...

    But not so sure if it's an easy answer...

    I want to check if a cin is null, because i have this:

    Code:
        char tipo;
        int linha, coluna;
    
        cin >> tipo >> linha >> coluna;
    
        ...
        ...
    That is suposed to accept 3 diferent commands in a single line, separated by spaces. That works just fine.

    But i want that to work with just the first cin (tipo), so i thought i'd check if the other two are null when enter is pressed.

    Is there anyway to do that?

    Thanks.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    cin is never null. What you want to do is check to see if there is any more input from the user after tipo. However, you cannot do that with cin >> because it will just wait (forever) for the user to type in a value for linha and coluna even if they have already hit enter.

    To do what you want, you have to use getline to get the line typed in by the user (everything up to the enter) and then parse that string to see if it has only a character or a character and two ints.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    9
    Thanks for your reply.

    So... How would I parse a string like this: "i 1 11" and separate it's three elements (i, 1 and 11), after having something like this:

    Code:
        ...
        ...
    
        string c;
    
        getline(cin, c);
    
        ...
        ...
    Sorry for all these questions but I'm kind of a noob in c++... :P

  4. #4
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    You could use a stringstream.

    Code:
    #include <iostream>
    #include <string>
    #include <sstream>
    
    int main()
    {
    	using namespace std;
    
    	char z;
    	int x, y;
    
    	string c;
    	stringstream ss;
    
    	getline(cin, c);
    
    	ss << c;
    	ss >> z >> x >> y;
    }
    Or just

    Code:
    	cin >> z >> x >> y;

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    9
    Quote Originally Posted by Tonto
    You could use a stringstream.

    Code:
    #include <iostream>
    #include <string>
    #include <sstream>
    
    int main()
    {
    	using namespace std;
    
    	char z;
    	int x, y;
    
    	string c;
    	stringstream ss;
    
    	getline(cin, c);
    
    	ss << c;
    	ss >> z >> x >> y;
    }
    Or just

    Code:
    	cin >> z >> x >> y;
    The stringstream is a good ideia, and let's me do as i want (i think, i have not properly tested it), but could you explain the code to me? Particularly this bit:

    Code:
    getline(cin, c);
    
    ss << c;
    ss >> z >> x >> y;
    Thanks!

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    What you want to do for the stringstream is test each read to see if it succeeds or fails. First, you initialize it with the line you read in with getline. I would use an istringstream since all you are really doing is reading data from the string and not writing data to a string:
    Code:
    istringstream iss(c);
    Then read in and check to see if the read succeeds. The stringstream works just like cin, so you can read in like this:
    Code:
    if (!(iss >> coluna))
    {
      // The user didn't enter a value for coluna, or it wasn't in the proper format.
    }
    That is like what you wanted before with checking for null. It works with the stringstream because the stringstream gets its data from the string you pass to it instead of the user. So cin would just wait for the user to enter a value for coluna, but the stringstream method would fail if there was no value available, which is what you want.

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    stringstreams are basically objects which work like istream and ostream (or cin and cout if you don't get what I'm saying), so these are technically equivalent:

    Code:
    int a, b, c;
    cin >> a >> b >> c;
    Code:
    string s;
    getline(cin, s, '\n');
    stringstream ss;
    ss << s;
    int a, b, c;
    ss >> a >> b >> c;
    Using stringstream::str() will return a string representing the current contents of the object, << will insert into it, and >> will empty it into a variable. The advantage is that you have a higher degree of control over input processing.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New string functions
    By Elysia in forum C Programming
    Replies: 11
    Last Post: 03-28-2009, 05:03 AM
  2. Memory leaks problem in C -- Help please
    By Amely in forum C Programming
    Replies: 14
    Last Post: 05-21-2008, 11:16 AM
  3. Wierd Segmentation Faults on Global Variable
    By cbranje in forum C Programming
    Replies: 6
    Last Post: 02-19-2005, 11:25 AM
  4. Problem with a menu.
    By Rare177 in forum Windows Programming
    Replies: 4
    Last Post: 09-07-2004, 11:51 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

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