Problem

Hi guys,
I created this program for converting things (for now, only temperature) And the conversion part works well, but then, when I want to go back to the beginning, it doesn't work. Can someone find the problem?

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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <iostream>
using namespace std;

char a, b, c, d, e, f, g, h, howman = '\0';
int yesno, yesnoy;
float howmany;

int main () 
{
	while (a != 'e')
	{
	a = b = c = d = e = f = g = h = 'n';
	howmany = howman;
	yesno = howman;
	yesnoy = howman;

	cout << "What would you like to convert?\nType 't' for temperature or 'e' to exit.\n";
	char what;
	cin >> what;
	if (what == 'e') return 0;
	else if (what != 't' && what != 'e')
	{
		cout << "Sorry, but we can not convert what you asked for. All we can convert is temperature.\n\n";
	}
	else if (what == 't') 
	{
		while (b != 'e')
		{
		cout << "From what would you like to convert?\nType 'c' for degrees celcius, 'f' for degrees fahrenheit or 'e' to exit.\n";
		char whata;
		cin >> whata;
		if (whata == 'c') 
		{
			while (c != 'e')
			{
			cout << "To what would you like to convert?\nType 'f' for degrees fahrenheit, or type 'e' to exit.\n";
			char whatc;
			cin >> whatc;
			if (whatc == 'f')
			{
				while (h != 'e')
				{
				g = 'n';
				cout << "How many degrees celcius would you like to convert to degrees fahrenheit?\n";
				cin >> howmany;
				float result;
				result = howmany * 1.8 + 32;
				cout << howmany << " degrees celcius equals to " << result << " degrees fahrenheit.\n\nDo you want to convert something else?\nType 'y' for yes, 'n' for no or 'e' to exit.\n";
				cin >> yesno;
				if (yesno == 'n' || yesno == 'e') return 0;
				else if (yesno == 'y')
				{
					while (g != 'e')
					{
					cout << "Is the conversion you want to make still from celcius to fahrenheit?\nType 'y' for yes, 'n' for no or 'e' to exit.\n";
					cin >> yesnoy;
					if (yesnoy == 'n')
					{
						b = c = g = h = 'n';
					}
					else if (yesnoy == 'y') 
					{
						howmany = howman;
						yesno = howman;
						yesnoy = howman;
						g = 'e';
					}
					else if (yesnoy == 'e') 
					{
						return 0;
					}
					else 
					{
						cout << "Sorry, but we didn't understand your answer to the question. You should have answered either 'y' for yes, 'n' for no or 'e' to exit.\n\n";
					}
					}
				}
				}
			}
			else if (whatc == 'e') 
			{
				return 0;
			}
			else 
			{
				cout << "Sorry, but we can not convert from degrees celcius to what you asked for. All we can convert to from degrees fahrenheit is degrees celcius.\n\n";
			}
			}
		}
		else if (whata == 'f')
		{
			while (d != 'e')
			{
			cout << "To what would you like to convert?\nType 'c' for degrees celcius, or type 'e' to exit.\n";
			char whatb;
			cin >> whatb;
			if (whatb == 'c')
			{
				while (e != 'e')
				{
				f = 'n';
				cout << "How many degrees fahrenheit would you like to convert to degrees celcius?\n";
				cin >> howmany;
				float result;
				result = (howmany - 32) / 1.8;
				cout << howmany << " degrees farenheit equals to " << result << " degrees celcius.\nDo you want to convert something else?\nType 'y' for yes, 'n' for no or 'e' to exit.\n";
				cin >> yesno;
				if (yesno == 'n' || yesno == 'e') return 0;
				else if (yesno == 'y')
				{
					while (f != 'e')
					{
					cout << "Is the conversion you want to make still from fahrenheit to celcius?\nType 'y' for yes, 'n' for no or 'e' to exit.\n";
					cin >> yesnoy;
					if (yesnoy == 'n')
					{
						b = d = e = f = 'e';
					}
					else if (yesnoy == 'y') 
					{
						g = 'e';
					}
					else if (yesnoy == 'e')
					{
						return 0;
					}
					else
					{
						cout << "Sorry, but we didn't understand your answer to the question. You should have answered either 'y' for yes, 'n' for no or 'e' to exit.\n\n";
					}
					}
				}
				}
			}
			else if (whatb == 'e') 
			{
				return 0;
			}
			else 
			{
				cout << "Sorry, but we can not convert from degrees fahrenheit to what you asked for. All we can convert to from degrees fahrenheit is degrees celcius.\n\n";
			}
			}
		}
		else if (whata == 'e') return 0;
		else 
		{
			cout << "Sorry, but we can not convert from what you asked for. All we can convert from is degrees celcius or fahrenheit.\n\n";
		}
		}
	}
	}
}
closed account (j3A5Djzh)
Hi,
the bigest problem of this program is fact, that this program is crazy and completly wrong designed! Try use better names for variables. This names "a,b,c,d,e,f,g,h" is not good idea ;-) Use switch and functions! If you put all code in main() is totaly confusing and wrong.

Sorry for my english and have a nice day!
Last edited on
Hi,
i completely agree with Bambanos and add that you shouldn't define variables in loops and an important thing you have to learn is how to use variables
for example see my code (i write it for u)

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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <iostream>
using namespace std;

int main ()
{
    bool run=true;
    char answer;
    char yesno;
    int degree , result;

    while (run==true)
    {
        cout << "What would you like to convert?\nType 't' for temperature or 'e' to exit.\n";
        cin >> answer;
        if (answer=='t')
        {
            cout << "\nFrom what would you like to convert?\nType 'c' for degrees celcius, 'f' for degrees fahrenheit or 'e' to exit.\n";
            cin >> answer;

            if (answer=='c')
            {
                cout << "\nTo what would you like to convert?\nType 'f' for degrees fahrenheit, or type 'e' to exit.\n";
                cin >> answer;

                if (answer=='f')
                {
                    cout << "\nHow many degrees celcius would you like to convert to degrees fahrenheit?\n";
                    cin >> degree;

                    result = degree * 1.8 + 32;
                    cout << endl <<degree << " degrees celcius equals to " << result << " degrees farenheit.\n";

                    cout << "\ndo you want to convert somthing else? (y) for yes (n) for no\n";
                    cin >> yesno;
                    if (yesno=='y')
                        continue;
                    else if (yesno=='n')
                        run=false;
                    else
                        cout << "Sorry, invalid input.\n\n";
                }
                else if (answer=='e')
                    run=false;
                else
                    cout << "Sorry, invalid input.\n\n";

            }
            else if (answer=='f')
            {
                cout << "\nTo what would you like to convert?\nType 'c' for degrees celcius, or type 'e' to exit.\n";
                cin >> answer;

                if (answer=='c')
                {
                    cout << "\nHow many degrees fahrenheit would you like to convert to degrees celcius?\n";
                    cin >> degree;

                    result = (degree - 32) / 1.8;
                    cout << endl << degree << " degrees farenheit equals to " << result << " degrees celcius.\n";

                    cout << "\ndo you want to convert somthing else? (y) for yes (n) for no\n";
                    cin >> yesno;
                    if (yesno=='y')
                        continue;
                    else if (yesno=='n')
                        run=false;
                    else
                        cout << "Sorry, invalid input.\n\n";
                }
                else if (answer=='e')
                    run=false;

                else
                    cout << "Sorry, invalid input.\n\n";
            }
            else if (answer=='e')
                run=false;

            else
                cout << "Sorry, invalid input.\n\n";
        }

        else if (answer=='e')
            run=false;

        else
            cout << "Sorry, invalid input.\n\n";
    }
    return 0;
}


it has less variables than yours but still do what you want. (i didn't use function because i don't know if u know about them or no)

you can modify this code with goto to make it exactly act like your code

i hope my reply was helpful for you
and sorry for my bad English
Topic archived. No new replies allowed.