Thread: Re: [Dev-C++] Dev-cpp-users Digest, Vol 21, Issue 2
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
From: Mayank J. N. <ma...@in...> - 2008-02-13 11:59:08
|
Dear There is no problem with the bool data type. In the below code comparison operator has been used instead of assignment operator for changing the value of output variable. So in the below code value of output variable is never changed to false and during the conditional checking in the if statement it get its value as true. > ------------------------------ > > Message: 2 > Date: Tue, 12 Feb 2008 00:03:54 -0500 > From: Shawn <tre...@gm...> > Subject: Re: [Dev-C++] bool data type > To: ll...@cd... > Cc: dev...@li... > Message-ID: <47B...@gm...> > Content-Type: text/plain; charset="iso-8859-1" > > I'm working with C++ though, my book says that the bool data type is a > built-in type consisting of just two values, the constants true and false. > > I have the program compiling without error now, however, it still prints > the last statement even though I didn't enter valid data. > > int main() > { > > //Declare and initialize variables > float weight = 0.0; > float distance = 0.0; > float shippingRate = 0.0; > float shippingCharges = 0.0; > * bool output = true;* > > > //Intro > cout<<endl; > cout<<"Thank you for shipping with The Fast Freight Company!!!\n\n"; > > > //Prompt user for the weight of their package > cout<<"Please enter the weight of your pacakge(in grams): "; > cin>>weight; > cout<<endl; > if (weight <= 0 || weight > 20000) > {cout<<"Please enter either a positive number or a weight of 20 > kilograms or less for your package.\t\t"; > *output == false;*} //use = sign here **instead of ==** > > //Prompt user for distance the package will travel > cout<<"Please enter the distance the package will travel(in miles): "; > cin>>distance; > cout<<endl; > if (distance < 10 || distance > 3000) > {cout<<"We don't ship to distances of less then 10 miles or more > then 3000 miles.\t\t"; > * *output == false;*} // use = sign here instead of ==* > > > //Evaluate weight input > if (weight <= 2000) > shippingRate = 1.10; > else if (weight > 2000 && weight <= 6000) > shippingRate = 2.20; > else if (weight > 6000 && weight <= 10000) > shippingRate = 3.70; > else > shippingRate = 4.80; > > > //Evaluate distance input > if (distance >= 10 && distance <= 500) > distance = 500; > else if (distance >= 501 && distance <=1000) > distance = 1000; > else if (distance >= 1001 && distance <= 1500) > distance = 1500; > else if (distance >= 1501 && distance <= 2000) > distance = 2000; > else if (distance >= 2001 && distance <= 2500) > distance = 2500; > else > distance = 3000; > > > //Perform the calculations > shippingCharges = shippingRate * (distance/500); > > //Evaluate if output is needed > //if (weight > 0 && weight <= 20000 && distance >= 10 && distance <= 3000) > *if (output == true) * > //Output shipping charges > {cout<<fixed<<showpoint; > cout<<"The total shipping charges for your package is > $"<<setprecision(2)<<shippingCharges<<endl;} > > return 0; > } > > When I enter 0 for both or just one prompt, the program still displays > both the total shipping charges as $6.60 and the input validation message. > > When I uncomment out the *if (weight > 0 && weight <= 20000 && distance > >= 10 && distance <= 3000) *line, it works just fine so I have to be > using bool wrong somehow. > > Lloyd wrote: > >> //C does not have bool data type >> enum >> { >> FALSE=0, >> TRUE=1 >> }; >> >> typedef int bool; >> >> int main() >> { >> bool a=TRUE; >> } >> >> >> On Mon, 2008-02-11 at 18:36 -0500, Shawn wrote: >> >> >>> Thanks, but the compiler is still saying 'TRUE' undeclared (first use >>> this function) and points me to the line where I initialize it to TRUE >>> at the beginning of my program. >>> >>> bool output = TRUE; >>> >>> I thought that as soon as you call bool, the compiler knows it's >>> either going to be true or false? >>> >>> pepone.onrez wrote: >>> >>> >>>> dev...@li... >>>> >>>> On Feb 12, 2008 12:09 AM, Shawn <tre...@gm...> wrote:> >>>> >>>> >>>>> but then what would I put just before the total shipping charges >>>>> >>>>> >>>> line so it wouldn't print? >>>> if(output == TRUE){ >>>> //output is TRUE to what you whan >>>> }else{ >>>> >>>> >>>> } >>>> >>>> ------------------------------------------------------------------------- >>>> This SF.net email is sponsored by: Microsoft >>>> Defy all challenges. Microsoft(R) Visual Studio 2008. >>>> http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/ >>>> _______________________________________________ >>>> Dev-cpp-users mailing list >>>> Dev...@li... >>>> TO UNSUBSCRIBE: >>>> http://www23.brinkster.com/noicys/devcpp/ub.htm >>>> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users >>>> >>>> >>>> ____________________________________________________________________ >>>> >>>> ------------------------------------------------------------------------- >>>> This SF.net email is sponsored by: Microsoft >>>> Defy all challenges. Microsoft(R) Visual Studio 2008. >>>> http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/ >>>> >>>> ____________________________________________________________________ >>>> >>>> _______________________________________________ >>>> Dev-cpp-users mailing list >>>> Dev...@li... >>>> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm >>>> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users >>>> >>>> >>>> >>> ------------------------------------------------------------------------- >>> This SF.net email is sponsored by: Microsoft >>> Defy all challenges. Microsoft(R) Visual Studio 2008. >>> http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/ >>> _______________________________________________ Dev-cpp-users mailing list Dev...@li... TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm https://lists.sourceforge.net/lists/listinfo/dev-cpp-users >>> >>> >> ______________________________________ >> Scanned and protected by Email scanner >> >> ------------------------------------------------------------------------- >> This SF.net email is sponsored by: Microsoft >> Defy all challenges. Microsoft(R) Visual Studio 2008. >> http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/ >> _______________________________________________ >> Dev-cpp-users mailing list >> Dev...@li... >> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm >> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users >> >> >> -- _____________________________________________________________ Mayank Jain Nawal NIKSUN. ma...@in... http://www.niksun.com Tel: +91 124 231 6012 SCO 16 Sector 14 Mob: +91 981 839 0836 Gurgaon Haryana 122001 India _____________________________________________________________ |
From: Shawn <tre...@gm...> - 2008-02-13 14:09:46
|
Thanks everyone! :) Mayank Jain Nawal wrote: > Dear > > There is no problem with the bool data type. In the below code > comparison operator has been used instead of assignment operator for > changing the value of output variable. So in the below code value of > output variable is never changed to false and during the conditional > checking in the if statement it get its value as true. > > > >> ------------------------------ >> >> Message: 2 >> Date: Tue, 12 Feb 2008 00:03:54 -0500 >> From: Shawn <tre...@gm...> >> Subject: Re: [Dev-C++] bool data type >> To: ll...@cd... >> Cc: dev...@li... >> Message-ID: <47B...@gm...> >> Content-Type: text/plain; charset="iso-8859-1" >> >> I'm working with C++ though, my book says that the bool data type is >> a built-in type consisting of just two values, the constants true and >> false. >> I have the program compiling without error now, however, it still >> prints the last statement even though I didn't enter valid data. >> int main() >> { >> >> //Declare and initialize variables >> float weight = 0.0; >> float distance = 0.0; >> float shippingRate = 0.0; >> float shippingCharges = 0.0; >> * bool output = true;* >> >> >> //Intro >> cout<<endl; >> cout<<"Thank you for shipping with The Fast Freight Company!!!\n\n"; >> >> >> //Prompt user for the weight of their package >> cout<<"Please enter the weight of your pacakge(in grams): "; >> cin>>weight; >> cout<<endl; >> if (weight <= 0 || weight > 20000) >> {cout<<"Please enter either a positive number or a weight of 20 >> kilograms or less for your package.\t\t"; >> *output == false;*} //use = sign here **instead of ==** >> >> //Prompt user for distance the package will travel >> cout<<"Please enter the distance the package will travel(in miles): "; >> cin>>distance; >> cout<<endl; >> if (distance < 10 || distance > 3000) >> {cout<<"We don't ship to distances of less then 10 miles or more >> then 3000 miles.\t\t"; >> * *output == false;*} // use = sign here instead of ==* >> >> >> //Evaluate weight input >> if (weight <= 2000) >> shippingRate = 1.10; >> else if (weight > 2000 && weight <= 6000) >> shippingRate = 2.20; >> else if (weight > 6000 && weight <= 10000) >> shippingRate = 3.70; >> else >> shippingRate = 4.80; >> >> //Evaluate distance input >> if (distance >= 10 && distance <= 500) >> distance = 500; >> else if (distance >= 501 && distance <=1000) >> distance = 1000; >> else if (distance >= 1001 && distance <= 1500) >> distance = 1500; >> else if (distance >= 1501 && distance <= 2000) >> distance = 2000; >> else if (distance >= 2001 && distance <= 2500) >> distance = 2500; >> else >> distance = 3000; >> //Perform the calculations shippingCharges = >> shippingRate * (distance/500); >> //Evaluate if output is needed >> //if (weight > 0 && weight <= 20000 && distance >= 10 && distance <= >> 3000) >> *if (output == true) * >> //Output shipping charges >> {cout<<fixed<<showpoint; >> cout<<"The total shipping charges for your package is >> $"<<setprecision(2)<<shippingCharges<<endl;} >> >> return 0; >> } >> >> When I enter 0 for both or just one prompt, the program still >> displays both the total shipping charges as $6.60 and the input >> validation message. >> When I uncomment out the *if (weight > 0 && weight <= 20000 && >> distance >= 10 && distance <= 3000) *line, it works just fine so I >> have to be using bool wrong somehow. >> >> Lloyd wrote: >> >>> //C does not have bool data type >>> enum >>> { >>> FALSE=0, >>> TRUE=1 >>> }; >>> >>> typedef int bool; >>> >>> int main() >>> { >>> bool a=TRUE; >>> } >>> >>> >>> On Mon, 2008-02-11 at 18:36 -0500, Shawn wrote: >>> >>>> Thanks, but the compiler is still saying 'TRUE' undeclared (first use >>>> this function) and points me to the line where I initialize it to TRUE >>>> at the beginning of my program. >>>> bool output = TRUE; >>>> >>>> I thought that as soon as you call bool, the compiler knows it's >>>> either going to be true or false? >>>> pepone.onrez wrote: >>>>> dev...@li... >>>>> >>>>> On Feb 12, 2008 12:09 AM, Shawn <tre...@gm...> wrote:> >>>>> >>>>>> but then what would I put just before the total shipping charges >>>>>> >>>>> line so it wouldn't print? >>>>> if(output == TRUE){ >>>>> //output is TRUE to what you whan >>>>> }else{ >>>>> >>>>> >>>>> } >>>>> >>>>> ------------------------------------------------------------------------- >>>>> >>>>> This SF.net email is sponsored by: Microsoft >>>>> Defy all challenges. Microsoft(R) Visual Studio 2008. >>>>> http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/ >>>>> _______________________________________________ >>>>> Dev-cpp-users mailing list >>>>> Dev...@li... >>>>> TO UNSUBSCRIBE: >>>>> http://www23.brinkster.com/noicys/devcpp/ub.htm >>>>> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users >>>>> >>>>> >>>>> ____________________________________________________________________ >>>>> >>>>> ------------------------------------------------------------------------- >>>>> >>>>> This SF.net email is sponsored by: Microsoft >>>>> Defy all challenges. Microsoft(R) Visual Studio 2008. >>>>> http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/ >>>>> >>>>> ____________________________________________________________________ >>>>> >>>>> _______________________________________________ >>>>> Dev-cpp-users mailing list >>>>> Dev...@li... >>>>> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm >>>>> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users >>>>> >>>> ------------------------------------------------------------------------- >>>> >>>> This SF.net email is sponsored by: Microsoft >>>> Defy all challenges. Microsoft(R) Visual Studio 2008. >>>> http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/ >>>> _______________________________________________ Dev-cpp-users >>>> mailing list Dev...@li... TO UNSUBSCRIBE: >>>> http://www23.brinkster.com/noicys/devcpp/ub.htm >>>> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users >>>> >>> ______________________________________ >>> Scanned and protected by Email scanner >>> >>> ------------------------------------------------------------------------- >>> >>> This SF.net email is sponsored by: Microsoft >>> Defy all challenges. Microsoft(R) Visual Studio 2008. >>> http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/ >>> _______________________________________________ >>> Dev-cpp-users mailing list >>> Dev...@li... >>> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm >>> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users >>> >>> > > |