Help Reqired

Hi,

I was doing a simple program for practice and getting an erroneous result for option 2. Can anybody remove the bug:

# include <iostream>
using namespace std;

class Tempurature
{

float c, f;
double con;
int choice;

public:
void TakeInput (void);
void Convert (void);
void DisplayResult (void);
};



void Tempurature :: TakeInput (void)
{

cout << "What to covert:\n" << "Celcius to Ferenhite\n" << "Ferenhite to Celcius\n" << "Enter your choice 1 or 2:\n";
cin >> choice;

if (choice == 1)
{

cout << "Enter the Celcius Value:\n";
cin >> c;
}
else
{

cout << "Enter the Ferenhite Value:\n";
cin>> f;
}
// return choice;
}

void Tempurature :: Convert (void)
{
if (choice == 1)
{

con = (c* (9/5)) + 32;
}
else
{

con = (f- 32)* (5/9);
}

}

void Tempurature :: DisplayResult (void)
{
if (choice == 1)

cout << c << " "<< "Degree Celcius =" << con << " " << "Degree Ferenhite\n";

else

cout << f << " " << " Degree Ferenhite =" << con << " " << "Degree Celcius\n";
}

int main ()
{

Tempurature dc;
dc.TakeInput();
dc.Convert();
dc.DisplayResult();
return 0;
}

Results:

What to covert:
Celcius to Ferenhite
Ferenhite to Celcius
Enter your choice 1 or 2:
2
Enter the Ferenhite Value:
203.94
203.94 Degree Ferenhite =0 Degree Celcius

--------------------------------
Process exited after 23.17 seconds with return value 0
Press any key to continue . . .

What to covert:
Celcius to Ferenhite
Ferenhite to Celcius
Enter your choice 1 or 2:
1
Enter the Celcius Value:
203
203 Degree Celcius =235 Degree Ferenhite

--------------------------------
Process exited after 5.753 seconds with return value 0
Press any key to continue . . .


Thanks.
Last edited on
Read this -> http://www.cplusplus.com/articles/jEywvCM9/
Format your post so we can read it better.

> con = (c* (9/5)) + 32;
> con = (f- 32)* (5/9);
Watch out for integer division giving you zero.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# include <iostream>
using namespace std;

class Tempurature
{
    float c, f;
    double con;
    int choice;
    
public:
    int TakeInput (void);
    void Convert (int); // <--
    void DisplayResult (void);
};

int Tempurature :: TakeInput (void)
{
    
    cout
    << "What to covert:\n" << "Celcius to Ferenhite\n"
    << "Ferenhite to Celcius\n" << "Enter your choice 1 or 2:\n";
    cin >> choice;
    
    if (choice == 1)
    {
        cout << "Enter the Celcius Value:\n";
        cin >> c;
    }
    else
    {
        
        cout << "Enter the Ferenhite Value:\n";
        cin>> f;
    }
     return choice; // <--
}

void Tempurature :: Convert (int choice)
{
    if (choice == 1)
    {
        
        con =  c * 9/5. + 32; // <--
    }
    else
    {
        
        con = (f - 32.)* 5/9; // <--
    }
    
}

void Tempurature :: DisplayResult (void)
{
    if (choice == 1)
        
        cout << c << " "<< "Degree Celcius =" << con << " " << "Degree Ferenhite\n";
    
    else
        
        cout << f << " " << " Degree Ferenhite =" << con << " " << "Degree Celcius\n";
}

int main ()
{
    
    Tempurature dc;
    int selection = dc.TakeInput(); // <--
    dc.Convert(selection);
    dc.DisplayResult();
    return 0;
}


What to covert:
Celcius to Ferenhite
Ferenhite to Celcius
Enter your choice 1 or 2:
1
Enter the Celcius Value:
100
100 Degree Celcius =212 Degree Ferenhite
Program ended with exit code: 0


Messrs Daniel Fahrenheit and Anders Celsius would appreciate you spelling their names correctly too :)
Last edited on
Hi,

I resolved it with a bracket ()

void Temperature:: Convert (void)
{
if (choice == 1)
{

con = ((c* 9) /5 )+ 32;
}

else
{

con =(((f- 32)* 5) / 9);
}
}

Fahrenheit

Thanks everyone.

SR
Last edited on
1
2
3
    int TakeInput (void); //sets this->choice
    void Convert (int); //uses choice passed as parameter
    void DisplayResult (void); //uses this->choice 
kind of confusing

consider this
1
2
3
4
5
6
7
8
9
10
11
12
13
class Temperature{
public:
   TakeInput(unit)
   DisplayResult(unit)
private
   double Convert(unit)
   double value_in_kelvin
};


Temperature t;
t.TakeInput('Celcius')
t.DisplayResult('Fahrenheit')
Last edited on
Topic archived. No new replies allowed.