Thread: Question about cin function

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    2

    Question about cin function

    Hi dudes,
    i have a problem in using cin function.
    i've written code for checking parantheses Balance,
    it works only for non-empty strings, but doesn't work for empty strings.
    however i know the problem is coming from cin function, but i can't handle it!
    here is a part of my code,
    thanks in advance, for any help

    Code:
    int main()
    {
    	int n; 
    	cin>>n;
    	string st;
    	vector<string> vct;
    	
    	while(n!=0)
    	{
    		cin>>st;
    		if (st.empty())
    		{
    			//string is empty and Balanced
    			//n--;
    		}
    
    
    		else
    		{
          if(checkBalanced(st))
    		vct.push_back("Yes");
    	  else	
    		vct.push_back("No"); 
    	   n--;
    		}
    	}
    	for (int i=0 ; i<vct.size() ; i++)
    		cout << vct.at(i) << endl;
    	
    	return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    How does it not work?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2014
    Posts
    2
    i mean when the user pressed Enter (empty string), the program goes to newline! like that you do not anything , so here is my full code.

    Code:
    #include<iostream>
    #include<stack>
    #include<string>
    #include <vector>
    #include <stdio.h>
    using namespace std;
    bool ArePair(char opening,char closing)
    {
    	if(opening == '(' && closing == ')') return true;
    	else if(opening == '[' && closing == ']') return true;
    	return false;
    }
    bool checkBalanced(string st)
    {
    	stack<char>  S;
    	for(int i =0 ; i<st.length() ; i++)
    	{
    		if(st[i] == '(' || st[i] == '[')
    			S.push(st[i]);
    		else if(st[i] == ')' || st[i] == ']')
    		{
    			if( (S.empty() ) || !ArePair(S.top(),st[i]))
    				return false;
    			else
    				S.pop();
    		}
    	}
    	return S.empty() ? true:false ;
    }
    
    
    
    
    int main()
    {
    	int n; 
    	cin>>n;
    	string st;
    	vector<string> vct;
    	
    	while(n!=0)
    	{
    		cin>>st;
    		if (st.empty())
    		{
    			//string is empty and Balanced
    			//n--;
    		}
    
    
    		else
    		{
          if(checkBalanced(st))
    		vct.push_back("Yes");
    	  else	
    		vct.push_back("No"); 
    	   n--;
    		}
    	}
    	for (int i=0 ; i<vct.size() ; i++)
    		cout << vct.at(i) << endl;
    	
    	return 0;
    }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > cin>>n;
    This leaves a \n on the input stream.

    > cin>>st;
    This uses \n as a terminator.

    So after reading your integer, use ignore()
    c++ - Why would we call cin.clear() and cin.ignore() after reading input? - Stack Overflow
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jul 2014
    Location
    Central Arizona
    Posts
    61
    clue 1: cin>> the (c in) function with the input operators, are used to have an operator input a variable.
    clue 2: you have cin>> in the area of int main where variables a usually declared.
    clue 3: n has not value until you give it one.

  6. #6
    Registered User
    Join Date
    Dec 2013
    Posts
    241
    one thing that really bothered me while reading this thread:
    cin is not a function , it's an object.
    it's >> operator was overloaded to provide functionality. still , it's an object and not a function.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. a question on the use of pow() function
    By brassbin in forum C Programming
    Replies: 8
    Last Post: 10-18-2013, 04:51 AM
  2. function question
    By joerules22 in forum C++ Programming
    Replies: 3
    Last Post: 08-31-2005, 04:17 PM
  3. Question on function syntax and calling function
    By cbrman in forum C Programming
    Replies: 10
    Last Post: 10-05-2003, 05:32 PM
  4. A function question
    By axon in forum C++ Programming
    Replies: 9
    Last Post: 02-21-2003, 06:26 PM
  5. question on gets() function
    By Unregistered in forum C++ Programming
    Replies: 11
    Last Post: 03-07-2002, 03:47 PM

Tags for this Thread