return 0; problem

this prgm convert F to C or C to F.

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
73
74
#include <iostream>
#include <iostream>
#include <iomanip>
using namespace std;
double calcFahrenheit();
double calcCelsius();
int main()
{
double choice,choice1,choice2;
	do 
	{
        cout << "1. Convert F to C" << endl;
        cout << "2. Convert C to F" << endl;
		cout << "What is your choice?" << endl;
        cin >> choice;
	}
	while (choice == 1 && choice == 2);
	{
		if (choice == 2)
		{
		choice1 = calcFahrenheit();
		cout << choice1 << endl;
		}
		if (choice == 1)
		{
		choice2 = calcCelsius();
		cout << choice2 << endl;
		}
	}
	if (choice != 1 && choice != 2)
	cout <<"Invalid Input"<< endl;
	{ 
		system("Pause");
		return 0;
	}
  system("Pause");
  return 0;
}
double calcCelsius()
{
double start_temp,end_temp,temp_incr;
		cout << "Starting Temperature: ";
		cin >> start_temp;
		cout << "Ending Temperature: ";
		cin >> end_temp;
		cout << "Increasement by: ";
		cin >> temp_incr;
		cout << "Fahrenheit    Celsius"
		"\n---------------------"<<endl;
		for (double F=start_temp, C=(5.0/9.0)*(F-32.0); F<=end_temp; F=F+temp_incr , C=(5.0/9.0)*(F-32.0)) 
			{
			cout.precision(2);
			cout <<setw(8) <<fixed<< F << setw(12) << C <<"\n";
			}
		return 0;	
}
double calcFahrenheit()
{
double start_temp,end_temp,temp_incr;
		cout << "Starting Temperature: ";
		cin >> start_temp;
		cout << "Ending Temperature: ";
		cin >> end_temp;
		cout << "Increasement by: ";
		cin >> temp_incr;
		cout << "Celsius    Fahrenheit"
		"\n---------------------"<<endl;
		for (double C=start_temp, F=(9.0/5.0)*(C)+32.0; C <=end_temp; C=C +temp_incr , F=(9.0/5.0)*(C)+32.0) 
			{
			cout.precision(2);
			cout <<setw(8) <<fixed<< C << setw(12) << F <<"\n";
			}
		return 0; 
}


but in the output, there is this strange 0.00 that show up
i.e.
Celsius    Fahrenheit
---------------------
   0.00       32.00
  15.00       59.00
  30.00       86.00
0.00

in line 55 and 73 i use return 0; which should end that call fuction. here is the thing i dont get. if the call function successfully end, then why 0.00 show up? could anyone offer me an explanation or a way to fix so that 0.00 wont show up? thx
Last edited on
closed account (D80DSL3A)
Try eliminating lines 22 and 27.
I'm surprised that line 17 ever lets you into the while loop. Did you mean to use || instead of && ?
If it were written like this while ( (choice == 1) && (choice == 2) ); then surely the loop would never be entered.
Bad indentation:

1
2
3
4
5
6
7
8
9
do 
	{
        cout << "1. Convert F to C" << endl;
        cout << "2. Convert C to F" << endl;
		cout << "What is your choice?" << endl;
        cin >> choice;
	} //this is a do while loop
	while (choice == 1 && choice == 2);
	{ //random scope entry 

closed account (D80DSL3A)
I see that now, thanks.
1. Line 17, why not have; while (choice != 1 && choice != 2); this would then eliminate the need for lines 30-35. If you wanted to warn user re; input you could put
if (choice != 1 && choice != 2) {cout<<"Invalid input, try again!\n";}
before line 17, ie; before the } while (choice != 1 && choice !== 2);

2. Line 36, change system ("pause") to cin.ignore().get();
(A safer replacement!)

3. Line 18 and 29 not sure why curly braces here??

4. Youve got #include <iostream> twice at top of code??

5. The 0.00 is occurring because of the for loop line 50. Why is a for-loop
being used for this calculation?
Last edited on
Topic archived. No new replies allowed.