TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: CHRIS DOWNS
from: MICHAEL RATHBURN
date: 1997-10-25 16:52:00
subject: Re: Help Please

Hi Chris Downs, hope you are having a nice day
22-Oct-97 06:49:10, Chris Downs wrote to Michael Rathburn
          Subject: Re: Help Please
 MR>> Please if you have time please look at this code and tell me what
 MR>> is going wrong all it is supposed to do is get the user to enter
 MR>> the quoted char,int and float check they have input the correct
 MR>> data then output the data but the float check cocks up please
 MR>> tell me why.
 CD> FASCINATING!!
 CD> First off, there were some problems with the logic of the program.
 CD> You didn't use braces around some of the else deals so that
 CD> program control didn't flow as I suppose you expected.  But that's
 CD> not what was so interesting.
 MR>> float pi;
 MR>> cout > pi; if(pi <
 MR>> 3.142  || pi > 3.142 ) { cout << "\n\tWrong Formula"; continue; }
 CD> When I ran this thing, I would type in 3.142 and the program
 CD> thinks that pi is either less than 3.142 or greater than 3.142.
This is what I didn't understand I changed it to cover a range and it
worked ill paste in the revised code
---->
snip><
//*******************************************************************
//*                chap2x3.cpp
//*                Programmer: Michael Rathburn
//*      Date 24/10/97
//*                Version 2
//*******************************************************************
#include 
#include 
#include 
#include 
char ch;
int number;
float pi;
void main()
{
        clrscr();
        while (1)
        {
                cout << "\n\tPlease enter the letter A   ";
                cin  >> ch;
                if (ch != 'A')                   //if not "A" print error
                {
                cout << "\n\tWrong Letter";
                continue;
                }
                else
                cout << "\n\tPlease enter 21  ";
                cin  >> number;
                if (number != 21)                //if not 21 print error
                {
                cout << "\n\tWrong Integer";
                continue;
                }
                else
                cout << "\n\tPlease enter pi ie:-3.142  ";
                cin >>pi;
                if( pi > 3.143  || pi < 3.141 )  //use a range to to test the 
float
                {
                cout << "\n\tWrong Formula";
                continue;
                }
                else
                cout << "\n\tYou entered the letter " << ch;
                cout << "\n\tYou entered the number " << number;
                cout << setiosflags(ios::fixed)
                          << setiosflags(ios::showpoint)    //show decimal 
point
                          << setprecision(3)                //3 points after 
the decimal
                          << setw(5);                       // set the field 
width to five
                cout << "\n\tYou Entered pi " << pi;
                break;
        }
                cout <<"\n\n\n\n\t\tGoodbye!";
}
*/
 CD> There are two ways to make it work right.  One is to declare pi to
 CD> be a double.  The other is to cast your numeric constants to
 CD> floats.
 CD> So the following will (consistently?) work
 CD> if (pi  (float)3.142) { ... }
 CD> I would love to hear a good explanation as to why 3.142(double) !=
 CD> 3.142(float)
 CD> -!-þ Blue Wave/QWK v2.12 þ
 CD> ---þ Blue Wave/QWK v2.12 þ
 -=> Yours sincerely, Michael Rathburn <=-
--- Terminate 5.00 UnReg
---------------
* Origin: Terminate IS the final terminal! (2:250/124.19)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.