What's wrong with this code?

This is a discussion on What's wrong with this code? within the C++ Programming forums, part of the General Programming Boards category; Sorry, I'm quite new to this, but could someone explain to me why the function pause() works perfectly in this ...

  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
    22,183
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 08: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, 01:51 AM

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