Thread: What's wrong with this code?

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    2

    What's wrong with this code?

    Sorry, I'm quite new to this, but could someone explain to me why the function pause() works perfectly in this instance:
    Code:
    #include <iostream>
    
    using namespace std;
    
    float tempConvert(float);
    void pause(void);
    
    
    int main(int argc, char* argv[])
    {
    	pause(); 	 
    	return 0;
    }
    
    void pause()
    {
    	cout<<endl<<"In pause()...";
    	cin.get();   
    }
    
    float tempConvert(float tempFarenheit)
    {
    	float tempCel;
    	tempCel=((tempFarenheit-32)*5)/9;
    	return tempCel;
    }
    But not in this case:

    Code:
    #include <iostream>
    
    using namespace std;
    
    float tempConvert(float);
    void pause(void);
    
    
    int main(int argc, char* argv[])
    {
    	float tempFer;
    	cout<<"Enter the temperature in Fahrenheit: ";
    	cin>>tempFer;
    	cout<<endl<<"The temperature in Celcius is: "<<tempConvert(tempFer)<<" degrees.";
    	pause(); 	 
    	return 0;
    }
    
    void pause()
    {
    	cout<<endl<<"In pause()...";
    	cin.get();   
    }
    
    float tempConvert(float tempFarenheit)
    {
    	float tempCel;
    	tempCel=((tempFarenheit-32)*5)/9;
    	return tempCel;
    }
    What happens is that it will wait for the user to enter something in the first code segment I posted, but in the second one it will display "In pause()..." but won't wait for the user to input something.

  2. #2
    Deleted Account
    Join Date
    Feb 2005
    Location
    Vienna, Austria
    Posts
    1
    Hello.

    I'm new to C++ too. I didn't find a solution to your problem, but I think it has something to do with the puffer which is used for the input. I tried to clean it but it still didn't stop at that point.

    Code:
    cin.clear();
    cin.ignore(cin.rdbuf()->in_avail());
    A small solution would be to use two cin.get(), that worked fine. But I guess that's not the best solution for that.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    cin >> tempFer;
    This leaves a newline ('\n' - Enter keypress) in the cin buffer.
    cin.ignore();
    This clears the buffer.
    If there is something in the buffer cin.get() just gets it from there and doesn't stop for user input.

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    2
    Quote Originally Posted by anon
    cin >> tempFer;
    This leaves a newline ('\n' - Enter keypress) in the cin buffer.
    cin.ignore();
    This clears the buffer.
    If there is something in the buffer cin.get() just gets it from there and doesn't stop for user input.
    Ah, sweet man. Thanks, I didn't realize that it would do that, although I knew that it would still have something in the buffer, lol. Thanks for the replies guys.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    cin.ignore();
    This clears the buffer.
    That is not true. It reads and discards the next character.

    You might test with:
    Code:
    cin>>tempFer;
    cin.ignore();
    Then enter say, 25.5
    It probably would work, since 25.5 is read to tempFer, then the newline is discarded.
    Now try entering 25.5a
    This time, 25.5 is still read to tempFer, but the 'a' is discarded.

    One solution is to #include <limits>
    then write:
    Code:
    std::cin >> tempFer;
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    This discards as many characers as the stream can hold, or up to the first newline encountered.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is wrong in this simple code
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 07:10 AM
  2. what is wrong with this code please
    By korbitz in forum Windows Programming
    Replies: 3
    Last Post: 03-05-2004, 10:11 AM
  3. I cant find what is wrong with this code
    By senegene in forum C Programming
    Replies: 1
    Last Post: 11-12-2002, 06:32 PM
  4. Anyone see what is wrong with this code?
    By Wise1 in forum C Programming
    Replies: 2
    Last Post: 02-13-2002, 02:01 PM
  5. very simple code, please check to see whats wrong
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 12:51 AM