Thread: Input Error

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    164

    Input Error

    Okay I made a prog its for temperature conversion its still not developed yet and Im having problems

    heres the code

    Code:
    /* Software Name = Temperature Converter Calculator...
    Made by Manzoor Ahmed.*/
    // Program that helps converting Temperature degrees.
    // Formulas obtained from wikipedia.org.
    
    #include <cstdlib>
    #include <cctype>
    #include <iostream>
    
    using namespace std;
    
    int CelFunc() ;
    
    void CnvrtTemp()
    {
        char Cnvrt_Temp_Option ;
    
    
    	     cout << "\n==================== Convert Temperature =======================" << endl ;
         do
          {
    	     cout << "\n\n[C] Celsius Converter" << endl ;
    	     cout << "[F] Fahrenheit Converter" << endl ;
    	     cout << "[K] Kelvin Converter" << endl ;
    	     cout << "[R] Rankine Converter" << endl ;
    	     cout << "[M] Back to Main Menu." << endl ;
    	
    
    
    	     cout << "\n(Characters enclosed by the square brackets are the options.)" << endl ;
    	
    	     cout << "\nEnter your option: " ;
    	     cin >> Cnvrt_Temp_Option ;
    
    	     if ( Cnvrt_Temp_Option == 67 || Cnvrt_Temp_Option == 99 ) // 67 == C      99 == c
    		    {
    		     CelFunc();
    		    }
    
    		    // 70 = F  and 102 = f
    	     if ( Cnvrt_Temp_Option == 70 || Cnvrt_Temp_Option == 102 )
    		    {
    		     cout << "FahrFunc()" << endl ;
    		    }
    	
    	     if ( Cnvrt_Temp_Option == 75 || Cnvrt_Temp_Option == 107 )
    		    {
    		     cout << "KelFunc()" << endl;
    		    }
    
    	     if ( Cnvrt_Temp_Option == 82 || Cnvrt_Temp_Option == 114 )
    		    {
    		     cout << "RankFunc()" << endl ;
    		    }
    	   } while (!( Cnvrt_Temp_Option == 77 || Cnvrt_Temp_Option == 109 )); // 77 == M and 109 == m
    }
    
    int main()
    {
              char Main_Menu_Option;
                                // program name output
              cout << "################################################################################";
              cout << "#                                                                              #";
              cout << "#                                                                              #";
              cout << "#                                                                              #";
              cout << "#                       Temperature Converter Calculator                       #";
              cout << "#                                                                              #";
              cout << "#                                                                              #";
              cout << "#                                                                              #";
              cout << "#                                                                              #";
              cout << "################################################################################\n\n";
    
    
              do
                {                      // Main Menu
    
              cout << "\n================================ Main Menu =====================================\n\n\n";
                         // Menu
              cout << "[C] Convert Temperature" << endl ;
              cout << "[H] Help" << endl ;
              cout << "[E] Exit" << endl ;
    
              cout << "\n(Characters enclosed by the square brackets are the options.)\n" << endl ;
    
    
    
                   cout << "\nEnter your option : ";
                   cin >> Main_Menu_Option ;
    
                   // Following I have used ASCII numbers to check the input from user
                   // whether the input is correct or not
                   // 67 = C,  99 = c, 72 = H, 104 = h, 69 = E, 101 = e .
                   if (!( Main_Menu_Option == 67 || Main_Menu_Option == 99 || Main_Menu_Option == 72 || Main_Menu_Option == 104 || Main_Menu_Option == 69 || Main_Menu_Option == 101 ))
                   {
                      cout << "\nWrong option entered\n";
                   }
                    //  67 == C,  99 == c, 72 == H, 104 == h, 69 == E, 101 == e .
    
    
    
                    if ( Main_Menu_Option == 67 || Main_Menu_Option == 99) // 67 == C , 99 == c.
                       {
                        CnvrtTemp();
                       }
    				
    				if ( Main_Menu_Option == 69 || Main_Menu_Option == 104 )
    					{
    						cout << "Help section is under construction.\n";
    					}	
    
    
               } while (!( Main_Menu_Option == 69 || Main_Menu_Option == 101 ));
              system ("pause");
              return 0;
    }
    
    int CelFunc()
    	{	char again;
    		cout << "\n=============================== Celsius Converter ==============================\n\n" ;		
    		
    		
    		do {
    		cout << "Enter °C (Celsius) degree to convert :";
    		long double cTemp2Conv; 
    		cin >> cTemp2Conv;
    		
    			if (isdigit(cTemp2Conv)) {
    				
    				
    				long double cFahrenheit = (cTemp2Conv * 1.8) + 32;
    				long double cKelvin = cTemp2Conv + 273.15;
    				long double cRankine = (cTemp2Conv + 273.15) * 1.8;
    				
    				
    				cout << "\nAnswer: " << cTemp2Conv << " Celsius in Fahnrenheit scale = " << cFahrenheit;
    				cout << "\nAnswer: " << cTemp2Conv << " Celsius in Kelvin scale = " << cKelvin; 
    				cout << "\nAnswer: " << cTemp2Conv << " Celsius in Rankine scale = " << cRankine;
    				cout << "\n\n";
    			}
    			else {
    				cout << "\nINVALID INPUT !";
    				cout << "\nPlease enter numerical values.\n";
    			}			
    			cout << "\nDo you want to convert temperature again ?";
    			cout << "\nEnter Y for yes, N for no.";
    			cout << "\nEntering wrong option other than Y/N will result in getting back to \nConvert Temprature menu.";
    			cout << "\n[y]/[n]:";
    			cin >> again;
    				
    		} while (again == 'y'|| again == 'Y');
    	}
    the problem is when you go to Celsius Converter and there input alphabets then the programs start looping infinitely

    I tried cin.good() and isdigit() for checking the input but both gave me same results

    any help is really appreciated

    thanks

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Surely you don't need 20+ digits precision in your temperature conversion? Using float or plain double should be perfectly sufficient, rather than using "long double".

    Second, you are doing a lot of checking for letters with numbers. Why not do
    Code:
    if (tolower(input) == 'c')  ...
    It save you writing a commend saying "99 = c", and using tolower means you only have ONE comparison.

    Code:
    if (isdigit(cTemp2Conv))
    Is incorrect use of isdigit(). the function isdigit() takes a char as input, not a floating point value, and returns "true" if the number is in the range '0'..'9' - which mens 48..57 in integer values - so it would be true when you enter 48..57.9 degrees, and false otherwise.

    if you want to check if the input is "ok", you can do:
    Code:
    if (cin >> cTemp2Conv) {
    ... // input is accepted
    ... 
    } else {
    ... // input not correct. 
    cin.clear();  // Clear the "input had errors flag"
    cin.ignore(100, '\n'); // remove any remaining unused input up to 100 chars ending with \n
    ... 
    }
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jan 2007
    Location
    Euless, TX
    Posts
    144
    I think you will find this much simpler if you just use the 'C', 'c', 'E', 'e', etc, rather than using the ASCII numerical equivalent. Also, check the return of 'isdigit()'. I input a valid numer (32) and it returns an error.

  4. #4
    Registered User
    Join Date
    Nov 2007
    Posts
    164
    hi thanks guys

    matsp: i didn't understand what you said

    can you please explain ??

    or show me an example of my code ??

    I don't see how you check the input with if (cin >> cTemp2Conv)

    where's the condition in the if statement ?
    Last edited by manzoor; 11-09-2007 at 08:57 AM.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Can you clarify which part you don't understand, and perhaps TRY to implement a correction along the lines of what I suggested, and then ask further questions (and show what you've done). I don't believe my comments were that unclear.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Nov 2007
    Posts
    164
    hmmm well I didn't see where you check the input of variable cTemp2Conv
    what this line does ??

    if (cin >> cTemp2Conv)


    what does tolower() do ?

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by manzoor View Post
    hmmm well I didn't see where you check the input of variable cTemp2Conv
    what this line does ??

    if (cin >> cTemp2Conv)


    what does tolower() do ?
    Code like this
    Code:
    int x;
    bool b = cin >> x;
    will produce a value in b that reflects the success of the "cin >> x" operaton. So if what do you think the following does:
    Code:
    int x;
    bool b = cin >> x;
    if (b) cout << "x = " << x << endl;
    else cout << "error" << endl;
    ??

    tolower() takes a char and produces the lower-case version of it. E.g. tolower('A') is 'a', tolower('p') is 'p'. In the usage I suggest, it avoids checking for both the upper and lower case of the same value, since it's already been converted to lower-case, so it CAN'T be upper case.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Nov 2007
    Posts
    164
    hmm ok i understand the tolower() func

    but still can't understand how you check the input

    can you add the code to my temperature converter and then explain it step-by-step

    im a beginner and don't understand things that much

    so please explain the code step-by-step and if you can the add the code of checking the input to my temperature converter then i'll be so kind to you

  9. #9
    Registered User
    Join Date
    Nov 2007
    Posts
    164
    *bump*

    Help Please I Need To Show This Program To My School I Have Exam Need Help Asap

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    See Rule 5
    Post your latest effort, and drop the "Initial Caps Style" and urgency claims.

    We can help you along, but you've got to show what you know and what you've been learning. We're not here just to pass exams on your behalf.
    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.

  11. #11
    Registered User
    Join Date
    Nov 2007
    Posts
    164
    I Said Help

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And we've told you to "try it, then post the code". As Salem says, we're not here to do the job for you.

    If anyone else thinks my statements above are unclearn, then please feel free to clarify [sometimes what one person thinks is clear, isn't really clear]

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I think it's "clearn" (), but I'll try to explain it in more detail.
    Code:
    if (cin >> cTemp2Conv)
    cin's >> operator returns a bool, just like this.
    Code:
    bool function() {
        /* ... */
    }
    This boolean value indicates whether the cin operation was a success or not. It returns true if it succeeded. So, you can do something like this.
    Code:
    int var;
    bool success;
    
    success = (cin >> var);
    if(success) {
        /* success */
    }
    else {
        /* failure */
    }
    if(success) is the same as if(success == true).

    But you can also forgo the "success" variable entirely and put the expression directly into an if statement.
    Code:
    int var;
    bool success;
    
    if(cin >> var) {
        /* success */
    }
    else {
        /* failure */
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  14. #14
    Registered User
    Join Date
    Nov 2007
    Posts
    164
    Code:
    int var;
    bool success;
    
    success = (cin >> var);
    if(success) {
        /*So you mean here I put my code if the input was correct 
    
         Right ?*/
    }
    else {
        /* And if the input was a alphabet instead of the number then show the error message here ? Right ? */
    }

  15. #15
    Registered User
    Join Date
    Nov 2007
    Posts
    164
    Code:
    /* Software Name = Temperature Converter Calculator...
    Made by Manzoor Ahmed.*/
    // Program that helps converting Temperature degrees.
    // Formulas obtained from wikipedia.org.
    
    #include <cstdlib>
    #include <cctype>
    #include <iostream>
    
    using namespace std;
    
    int CelFunc() ;
    
    void CnvrtTemp()
    {
        char Cnvrt_Temp_Option ;
    
    
    	     cout << "\n==================== Convert Temperature =======================" << endl ;
         do
          {
    	     cout << "\n\n[C] Celsius Converter" << endl ;
    	     cout << "[F] Fahrenheit Converter" << endl ;
    	     cout << "[K] Kelvin Converter" << endl ;
    	     cout << "[R] Rankine Converter" << endl ;
    	     cout << "[M] Back to Main Menu." << endl ;
    	
    
    
    	     cout << "\n(Characters enclosed by the square brackets are the options.)" << endl ;
    	
    	     cout << "\nEnter your option: " ;
    	     cin >> Cnvrt_Temp_Option ;
    		 cin.ignore();
    
    	     if ( Cnvrt_Temp_Option == 'C' || Cnvrt_Temp_Option == 'c' )
    		    {
    		     CelFunc();
    		    }
    		    
    	     if ( Cnvrt_Temp_Option == 'F' || Cnvrt_Temp_Option == 'f' )
    		    {
    		     cout << "FahrFunc()" << endl ;
    		    }
    	
    	     if ( Cnvrt_Temp_Option == 'K' || Cnvrt_Temp_Option == 'k' )
    		    {
    		     cout << "KelFunc()" << endl;
    		    }
    
    	     if ( Cnvrt_Temp_Option == 'R' || Cnvrt_Temp_Option == 'r' )
    		    {
    		     cout << "RankFunc()" << endl ;
    		    }
    	   } while (!( Cnvrt_Temp_Option == 'M' || Cnvrt_Temp_Option == 'm' ));
    }
    
    int main()
    {
              char Main_Menu_Option;
                                // program name output
              cout << "################################################################################";
              cout << "#                                                                              #";
              cout << "#                                                                              #";
              cout << "#                                                                              #";
              cout << "#                       Temperature Converter Calculator                       #";
              cout << "#                                                                              #";
              cout << "#                                                                              #";
              cout << "#                                                                              #";
              cout << "#                                                                              #";
              cout << "################################################################################\n\n";
    
    
              do
                {                      // Main Menu
    
              cout << "\n================================ Main Menu =====================================\n\n\n";
                         // Menu
              cout << "[C] Convert Temperature" << endl ;
              cout << "[H] Help" << endl ;
              cout << "[E] Exit" << endl ;
    
              cout << "\n(Characters enclosed by the square brackets are the options.)\n" << endl ;
    
    
    
                   cout << "\nEnter your option : ";
                   cin >> Main_Menu_Option ;
    
                 
                   if (!( Main_Menu_Option == 'C' || Main_Menu_Option == 'c' || Main_Menu_Option == 'H' || Main_Menu_Option == 'h' || Main_Menu_Option == 'E' || Main_Menu_Option == 'e' ))
                   {
                      cout << "\nWrong option entered\n";
                   }
    
    
    
                    if ( Main_Menu_Option == 67 || Main_Menu_Option == 99) // 67 == C , 99 == c.
                       {
                        CnvrtTemp();
                       }
    				
    				if ( Main_Menu_Option == 69 || Main_Menu_Option == 104 )
    					{
    						cout << "Help section is under construction.\n";
    					}	
    
    
               } while (!( Main_Menu_Option == 69 || Main_Menu_Option == 101 ));
              system ("pause");
              return 0;
    }
    
    int CelFunc()
    	{	char again;
    		cout << "\n=============================== Celsius Converter ==============================\n\n" ;		
    		
    		
    		do {
    		cout << "Enter &#176;C (Celsius) degree to convert :";
    		float cTemp2Conv;
    		cin >> cTemp2Conv;
    		cin.ignore();
    		
    			if (cin >> cTemp2Conv) {
    				
    				
    				double cFahrenheit = (cTemp2Conv * 1.8) + 32;
    				double cKelvin = cTemp2Conv + 273.15;
    				double cRankine = (cTemp2Conv + 273.15) * 1.8;
    				
    				
    				cout << "\nAnswer: " << cTemp2Conv << " Celsius in Fahnrenheit scale = " << cFahrenheit;
    				cout << "\nAnswer: " << cTemp2Conv << " Celsius in Kelvin scale = " << cKelvin; 
    				cout << "\nAnswer: " << cTemp2Conv << " Celsius in Rankine scale = " << cRankine;
    				cout << "\n\n";
    			}
    			else {
    				cout << "\nINVALID INPUT !";
    				cout << "\nPlease enter numerical values.\n";
    			}
    			cin.clear();
    			cin.ignore(100, '\n');
    			cout << "\nDo you want to convert temperature again ?";
    			cout << "\nEnter Y for yes, N for no.";
    			cout << "\nEntering wrong option other than Y/N will result in getting back to \nConvert Temprature menu.";
    			cout << "\n[y]/[n]:";
    			cin >> again;
    				
    		} while (again == 'y'|| again == 'Y');
    	}
    OK HERE'S the code

    But I still have one problem and it is, that now I have to enter two values for example

    Look at the output:

    Enter C (Celsius) degree to convert: 100
    100

    Answer: 100 Celsius in Fahrenheit is: 212....
    ....

    Blah blah see ??

    you got what i wanna say ?

    but when you enter an alphabet then it responds at the first input see the output

    OUTPUT:

    Enter C (Celsius) degree to convert: c

    INVALID INPUT !
    Please enter numerical values.

    Got it ?

    Hope you understand What i wanted to say

    Sorry for my English

    thanks matsp and dwk

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM