I was doing this program on Visual Studio 2005 where the program converts C to F and vise versa but after debugging it, it doesnt give the expected result. Could someone tell me what is wrong with my code???
#include %26lt;iostream%26gt;
#include %26lt;iomanip%26gt;
using namespace::std;
using std::fixed;
int main()
{
int conversionType = 0;
double temp = 0.0;
double result = 0.0;
cout %26lt;%26lt; "Enter -1 (F to C) or 2 (C to F): ";
cin %26gt;%26gt; conversionType;
cout %26lt;%26lt; "Enter temperature: ";
cin %26gt;%26gt; temp;
if (conversionType == '-1')
{
result = (temp - 32) * 5 / 9 ;
}
else if (conversionType == '2')
{
result = temp * 9/5 + 32;
} //end if
cout %26lt;%26lt; "Result: " %26lt;%26lt; result %26lt;%26lt; endl;
return 0;
} //end of main function
--------------------------------------...
C++ programming question?
I'm not a c++ programmer but if your converstionType is an integer, why are you comparing it to a string? (conversionType == '2').
Reply:what the hell? ? Report It
Reply:what the hell? ? Report It
Reply:You're comparing the integer "conversionType" to a character in each of your if statements. Remove the quotes.
Reply:You are not pausing the prompt so that it can show the output. Add one of these
system ("PAUSE");
cin.ignore();
cin.get();
above return 0.
I use Dev C++, so I use system ("PAUSE"). You use something else, so these commands may not work. If they don't, then this definitely will.
It will hold the session until the user presses a key and since there is no more code after it, it will close the window.
Also, remove the quote or else it will return 0 regardless of what you put in.
#include %26lt;iostream%26gt;
#include %26lt;iomanip%26gt;
using namespace::std;
using std::fixed;
int main()
{
int conversionType = 0;
double temp = 0.0;
double result = 0.0;
cout %26lt;%26lt; "Enter -1 (F to C) or 2 (C to F): ";
cin %26gt;%26gt; conversionType;
cout %26lt;%26lt; "Enter temperature: ";
cin %26gt;%26gt; temp;
if (conversionType == -1)
{
result = (temp - 32) * 5 / 9 ;
}
else if (conversionType == 2)
{
result = temp * 9/5 + 32;
} //end if
cout %26lt;%26lt; "Result: " %26lt;%26lt; result %26lt;%26lt; endl;
char t;
cout%26lt;%26lt;"Press a key to end the session";
cin %26gt;%26gt; t;
return 0;
} //end of main function
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment