I'm trying to make a simple tempreture conversion program. The program is to convert from centigrade to fahrenheit. I can't get the program to compile. Please help. The program is below. What corrections should I do to make the program work?
// This is a program used convert values from degrees centigrade to degrees fahrenheit.
#include %26lt;iostream%26gt;
using namespace std;
int main ()
{
     double centigrade;
     double fahrenheit;
     cout %26lt;%26lt; "This program is used to convert degrees centigrade into degrees fahrenheit" %26lt;%26lt; endl;
     cout %26lt;%26lt; "What is the value for centigrade? ";
     cin %26lt;%26lt; centigrade;
     fahrenheit = 9.0 / 5.0 * centigrade + 32;
     cout %26lt;%26lt; "The degrees fahrenheit is " %26lt;%26lt; fahrenheit %26lt;%26lt; endl;
     return 0;
}
Please Help. Could someone help me correct my simple C++ program?
There are two simple issues. The "%26lt;%26lt;" in cin should be "%26gt;%26gt;" and then also the order of operations needs to be corrected on calculating the fahrenheit. And it looks like you have the wrong calculation. It should be:
fahrenheit = ((9*centigrade)/5) + 32
Reply:try this one with some addition features such as; subtract, divide, multiply and convert
#include %26lt;iostream.h%26gt;
int main(void)
    {
    int n,a;
    double d,e;
    cout%26lt;%26lt;"\n1. Multiply\n";
    cout%26lt;%26lt;"2. Divide\n";
    cout%26lt;%26lt;"3. Add\n";
    cout%26lt;%26lt;"4. Subtract\n";
    cout%26lt;%26lt;"5. Celcius to Fahrenheit\n";
    cout%26lt;%26lt;"6. Fahrenheit to Celcius\n";
    cout%26lt;%26lt;"choose a number: ";
    cin %26gt;%26gt; a;
    switch (a)
        {
        case 1:
        cout%26lt;%26lt;"\nenter your first number: ";
        cin %26gt;%26gt; d;
        cout%26lt;%26lt;"enter your second number: ";
        cin %26gt;%26gt; e;
        cout %26lt;%26lt; "your answer is: " %26lt;%26lt;(d*e) %26lt;%26lt; "\n";
        break;
        case 2:
        cout%26lt;%26lt;"\nenter your first number: ";
        cin %26gt;%26gt; d;
        cout%26lt;%26lt;"enter your second number: ";
        cin %26gt;%26gt; e;
        cout %26lt;%26lt; "your answer is: " %26lt;%26lt;(d/e) %26lt;%26lt; "\n";
        break;
        case 3:
        cout%26lt;%26lt;"\nenter your first number: ";
        cin %26gt;%26gt; d;
        cout%26lt;%26lt;"enter your second number: ";
        cin %26gt;%26gt; e;
        cout %26lt;%26lt; "your answer is: " %26lt;%26lt;(d+e) %26lt;%26lt; "\n";
        break;
        case 4:
        cout%26lt;%26lt;"\nenter your first number: ";
        cin %26gt;%26gt; d;
        cout%26lt;%26lt;"enter your second number: ";
        cin %26gt;%26gt; e;
        cout %26lt;%26lt; "your answer is: " %26lt;%26lt;(d-e) %26lt;%26lt; "\n";
        break;
        case 5:
        cout%26lt;%26lt;"\nenter celcius: ";
        cin %26gt;%26gt; d;
        cout %26lt;%26lt; "your answer is: " %26lt;%26lt;(9.0*(d/5.0) + 32) %26lt;%26lt; "\n";
        break;
        case 6:
        cout%26lt;%26lt;"\nenter Fahrenheit: ";
        cin %26gt;%26gt; d;
        cout %26lt;%26lt; "your answer is: " %26lt;%26lt;(5.0/9.0*(d-32)) %26lt;%26lt; "\n";
        break;
        default:
        cout%26lt;%26lt;"please choose a number from 1-6:\n";
        return main();
    }
    cout %26lt;%26lt; "\n"%26lt;%26lt;"1. restart\n2. exit\n";
    cin %26gt;%26gt; n;
    switch(n)
        {
        case 1:
        return main();
        case 2:
        return 0;
    }
    return 0;
}
Reply:The problem is that you have the wrong operator for cin.  It should be "cin %26gt;%26gt; centigrade":
int main () {
 double centigrade;
 double fahrenheit;
 cout %26lt;%26lt; "TEST" %26lt;%26lt; endl;
 cout %26lt;%26lt; "This program is used to convert degrees centigrade into degrees fahrenheit" %26lt;%26lt; endl;
 cout %26lt;%26lt; "What is the value for centigrade? ";
 cin %26gt;%26gt; centigrade;
 fahrenheit = 9.0 / 5.0 * centigrade + 32;
 cout %26lt;%26lt; "The degrees fahrenheit is " %26lt;%26lt; fahrenheit %26lt;%26lt; endl;
 return 0;
} 
This is because cin is an input stream and cout is an output stream.  The %26gt;%26gt; operator is used to read data; the %26lt;%26lt; operator is used to write data.  For cin, you are reading input, so you need to use the %26gt;%26gt; operator.  The %26lt;%26lt; operator does not apply to an input stream, because it can only read.
Reply:Your cin needs a %26gt;%26gt; operator instead of a %26lt;%26lt; operator.
Also posting your compiler errors really help in fixing this.
covent garden
Subscribe to:
Post Comments (Atom)
 
No comments:
Post a Comment