What am I doing wrong?

I am trying to make a nested for loop to convert celsius to fahrenheit but overtime i run the code i lose the previous column. Any advices? Thanks


#include <iostream>
using namespace std;

#include <iomanip>
#include <cmath>

int celciustofarenheit (int);
int farenheittocelsius (int);
int celsius;
int column;

int main() {
cout << "Celsius" << setw (10) <<"Farenheit" << setw(10);
cout << "Celsius" << setw(10) << "Farenheit" << setw(10);
cout << "Celsius" << setw(10) << "Farenheit" << setw(10);
cout << "Celsius" << setw(10) << "Farenheit" << setw(10) << endl;


for (celsius = 0; celsius <=24; celsius ++)
{
cout << setw(7);
cout << celsius;
cout << setw(10);
cout << celciustofarenheit (celsius)<< endl;

for (celsius = 25; celsius <=50; celsius ++)
{
cout << setw(27);
cout << celsius;

cout << setw(10);
cout << celciustofarenheit (celsius) << endl;

}

}

return 0;
}

int celciustofarenheit (int)
{
return (( celsius * 9/5) + 32);
}


Celsius Farenheit Celsius Farenheit
0 32
25 77
26 78
27 80
28 82
29 84
30 86
31 87
32 89
33 91
34 93
35 95
36 96
37 98
38 100
39 102
40 104
41 105
42 107
43 109
44 111
45 113
46 114
47 116
48 118
49 120
50 122
Last edited on
closed account (48T7M4Gy)
Write pseudocode

for each item
print 4 then go to next line
change variable for second for loop
That's kinda what I did and unfortunately is not working
Also, I would recommend to use floats not ints.
Last edited on
Will floats allow me to use different initial values for the same variable? Or should I just change the variable for the following loops?
what type of output you want?
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
#include <iostream>
using namespace std;

#include <iomanip>

float celciustofarenheit(float c);
float farenheittocelsius(float f);
float celsius;
int column;

int main() {
	cout << "Celsius" << setw(10) << "Farenheit" << endl;

	for (celsius = 0; celsius <= 24; celsius++) {
		cout << setw(7);
		cout << celsius;
		cout << setw(10);
		cout << celciustofarenheit(celsius) << endl;

		for (float fareh = 25; fareh <= 50; fareh++) {
			cout << setw(27) << fareh;
			cout << setw(10) << celciustofarenheit(fareh) << endl;
		}

	}

	return 0;
}

float celciustofarenheit(float c) {
	return ((celsius * 9 / 5) + 32);
}
I want to arrange Celsius (0-100) and it's conversion to Fahrenheit in columns of 25 outputs
please provide sample for output
Celsius Fahrenheit Celsius Fahrenheit Celsius Fahrenheit Celsius Fahrenheit
      0         32      25         77      50        122      75        167
      1         33      26         78      51        123      76        168
      2         35      27         80      52        125      77        170
      3         37      28         82      53        127      78        172
      4         39      29         84      54        129      79        174
      5         41      30         86      55        131      80        176
      6         42      31         87      56        132      81        177
      7         44      32         89      57        134      82        179
      8         46      33         91      58        136      83        181
      9         48      34         93      59        138      84        183
     10         50      35         95      60        140      85        185
     11         51      36         96      61        141      86        186
     12         53      37         98      62        143      87        188
     13         55      38        100      63        145      88        190
     14         57      39        102      64        147      89        192
     15         59      40        104      65        149      90        194
     16         60      41        105      66        150      91        195
     17         62      42        107      67        152      92        197
     18         64      43        109      68        154      93        199
     19         66      44        111      69        156      94        201
     20         68      45        113      70        158      95        203
     21         69      46        114      71        159      96        204
     22         71      47        116      72        161      97        206
     23         73      48        118      73        163      98        208
     24         75      49        120      74        165      99        210
                                                             100        212[output]
[/output]
Last edited on
closed account (48T7M4Gy)
Hint:
1
2
3
4
5
6
7
for ( int i = 0; i < 25; i++ )
{
   for (int j = 0; j < 4; j++)
      cout << i + (j * 25 );

   cout << endl;
}


Plus free giveaway for heading:
1
2
3
4
for (int i = 0; i < 4; i++)
   cout << "Celsius" << setw(10) << "Farenheit" << setw(10);

cout << endl;
Last edited on
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
#include <iostream>
using namespace std;

#include <iomanip>
#include <cmath>

float celciustofarenheit(float);
float farenheittocelsius(float);
float celsius = 0;
int column;
int row;

int main() {
	cout << "Celsius" << setw(10) << "Farenheit" << setw(10);
	cout << "Celsius" << setw(10) << "Farenheit" << setw(10);
	cout << "Celsius" << setw(10) << "Farenheit" << setw(10);
	cout << "Celsius" << setw(10) << "Farenheit" << setw(10) << endl;

	for (row = 0; row <=25; ++row) {
		for (column = 0; column <=3; ++column) {
			if (column == 0) {
				cout << setw(7);
				cout << celsius;
				cout << setw(10);
				cout << celciustofarenheit(celsius);
				++celsius;
			}
			if (column == 1) {
				cout << setw(10);
				cout << celsius;
				cout << setw(10);
				cout << celciustofarenheit(celsius);
				++celsius;
			}
			if (column == 2) {
				cout << setw(10);
				cout << celsius;
				cout << setw(10);
				cout << celciustofarenheit(celsius);
				++celsius;
			}
			if (column == 3) {
				cout << setw(17);
				cout << celsius;
				cout << setw(10);
				cout << celciustofarenheit(celsius) << endl;
				++celsius;
			}

		}

	}

	return 0;
}

float celciustofarenheit(float) {
	return ((celsius * 9 / 5) + 32);
}


This code is not optimized
Last edited on
closed account (E0p9LyTq)
Add a format flag to left-justify, tweak some of the setw()s, and you get a pretty output:

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
#include <iostream>
#include <iomanip>
#include <cmath>

float celciustofarenheit(float);

float celsius = 0.0;
int column;
int row;

int main()
{
   std::cout << "Celsius" << std::setw(10) << "Farenheit" << std::setw(10);
   std::cout << "Celsius" << std::setw(10) << "Farenheit" << std::setw(10);
   std::cout << "Celsius" << std::setw(10) << "Farenheit" << std::setw(10);
   std::cout << "Celsius" << std::setw(10) << "Farenheit" << std::setw(10) << std::endl;
   std::cout.setf(std::cout.left);

   for (row = 0; row <= 25; ++row)
   {
      for (column = 0; column <= 3; ++column)
      {
         if (column == 0)
         {
            std::cout << std::setw(8);
            std::cout << celsius;
            std::cout << std::setw(12);
            std::cout << celciustofarenheit(celsius);
            ++celsius;
         }
         if (column == 1)
         {
            std::cout << std::setw(8);
            std::cout << celsius;
            std::cout << std::setw(12);
            std::cout << celciustofarenheit(celsius);
            ++celsius;
         }
         if (column == 2)
         {
            std::cout << std::setw(8);
            std::cout << celsius;
            std::cout << std::setw(12);
            std::cout << celciustofarenheit(celsius);
            ++celsius;
         }
         if (column == 3)
         {
            std::cout << std::setw(8);
            std::cout << celsius;
            std::cout << std::setw(12);
            std::cout << celciustofarenheit(celsius) << std::endl;
            ++celsius;
         }

      }

   }

   return 0;
}

float celciustofarenheit(float)
{
   return ((celsius * 9.0 / 5.0) + 32.0);
}


Sample output:

Celsius Farenheit   Celsius Farenheit   Celsius Farenheit   Celsius Farenheit
0       32          1       33.8        2       35.6        3       37.4

4       39.2        5       41          6       42.8        7       44.6

8       46.4        9       48.2        10      50          11      51.8

12      53.6        13      55.4        14      57.2        15      59

16      60.8        17      62.6        18      64.4        19      66.2

20      68          21      69.8        22      71.6        23      73.4

24      75.2        25      77          26      78.8        27      80.6

28      82.4        29      84.2        30      86          31      87.8

32      89.6        33      91.4        34      93.2        35      95

36      96.8        37      98.6        38      100.4       39      102.2

40      104         41      105.8       42      107.6       43      109.4

44      111.2       45      113         46      114.8       47      116.6

48      118.4       49      120.2       50      122         51      123.8

52      125.6       53      127.4       54      129.2       55      131

56      132.8       57      134.6       58      136.4       59      138.2

60      140         61      141.8       62      143.6       63      145.4

64      147.2       65      149         66      150.8       67      152.6

68      154.4       69      156.2       70      158         71      159.8

72      161.6       73      163.4       74      165.2       75      167

76      168.8       77      170.6       78      172.4       79      174.2

80      176         81      177.8       82      179.6       83      181.4

84      183.2       85      185         86      186.8       87      188.6

88      190.4       89      192.2       90      194         91      195.8

92      197.6       93      199.4       94      201.2       95      203

96      204.8       97      206.6       98      208.4       99      210.2

100     212         101     213.8       102     215.6       103     217.4
Your code kinda works. Instead of increasing from top to bottom it increases from left to right. I think i can tweak it to get the output i want.

Thank You!!!!!!!


Celsius Farenheit   Celsius Farenheit   Celsius Farenheit   Celsius Farenheit
      0        33         1        35         2        37         3        39
      4        41         5        42         6        44         7        46
      8        48         9        50        10        51        11        53
     12        55        13        57        14        59        15        60
     16        62        17        64        18        66        19        68
     20        69        21        71        22        73        23        75
     24        77        25        78        26        80        27        82
     28        84        29        86        30        87        31        89
     32        91        33        93        34        95        35        96
     36        98        37       100        38       102        39       104
     40       105        41       107        42       109        43       111
     44       113        45       114        46       116        47       118
     48       120        49       122        50       123        51       125
     52       127        53       129        54       131        55       132
     56       134        57       136        58       138        59       140
     60       141        61       143        62       145        63       147
     64       149        65       150        66       152        67       154
     68       156        69       158        70       159        71       161
     72       163        73       165        74       167        75       168
     76       170        77       172        78       174        79       176
     80       177        81       179        82       181        83       183
     84       185        85       186        86       188        87       190
     88       192        89       194        90       195        91       197
     92       199        93       201        94       203        95       204
     96       206        97       208        98       210        99       212
    100       213       101       215       102       217       103       219
Last edited on
ric717 Please let me know is this the type of output you needed

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
#include <iostream>
#include <iomanip>
#include <cmath>

float celciustofarenheit(float);

float celsius = 0.0;
int column;
float column1;
int row;

int main() {
	std::cout << "Celsius" << std::setw(10) << "Farenheit" << std::setw(10);
	std::cout << "Celsius" << std::setw(10) << "Farenheit" << std::setw(10);
	std::cout << "Celsius" << std::setw(10) << "Farenheit" << std::setw(10);
	std::cout << "Celsius" << std::setw(10) << "Farenheit" << std::setw(10)
			<< std::endl;
	std::cout.setf(std::cout.left);
	//for loop for column 1
	for (column1 = 0; column1 <= 25; ++column1) {
		for (column = 0; column <= 3; ++column) {
			std::cout << std::setw(8);
			if (column == 0) {
				std::cout << column1;
				std::cout << std::setw(12);
				std::cout << celciustofarenheit(column1);
			}
			if (column == 1) {
				std::cout << column1 + 26;
				std::cout << std::setw(12);
				std::cout << celciustofarenheit(column1 + 26);
			}
			if (column == 2) {
				std::cout << column1 + 52;
				std::cout << std::setw(12);
				std::cout << celciustofarenheit(column1 + 52);
			}
			if (column == 3) {
				std::cout << column1 + 78;
				std::cout << std::setw(12);
				std::cout << celciustofarenheit(column1 + 78) << std::endl;
			}
		}
	}
	return 0;
}

float celciustofarenheit(float celsius)
{
	return ((celsius * 9.0 / 5.0) + 32.0);
}


Celsius Farenheit   Celsius Farenheit   Celsius Farenheit   Celsius Farenheit
0       32          25      77          50      122         75      167         
1       33.8        26      78.8        51      123.8       76      168.8       
2       35.6        27      80.6        52      125.6       77      170.6       
3       37.4        28      82.4        53      127.4       78      172.4       
4       39.2        29      84.2        54      129.2       79      174.2       
5       41          30      86          55      131         80      176         
6       42.8        31      87.8        56      132.8       81      177.8       
7       44.6        32      89.6        57      134.6       82      179.6       
8       46.4        33      91.4        58      136.4       83      181.4       
9       48.2        34      93.2        59      138.2       84      183.2       
10      50          35      95          60      140         85      185         
11      51.8        36      96.8        61      141.8       86      186.8       
12      53.6        37      98.6        62      143.6       87      188.6       
13      55.4        38      100.4       63      145.4       88      190.4       
14      57.2        39      102.2       64      147.2       89      192.2       
15      59          40      104         65      149         90      194         
16      60.8        41      105.8       66      150.8       91      195.8       
17      62.6        42      107.6       67      152.6       92      197.6       
18      64.4        43      109.4       68      154.4       93      199.4       
19      66.2        44      111.2       69      156.2       94      201.2       
20      68          45      113         70      158         95      203         
21      69.8        46      114.8       71      159.8       96      204.8       
22      71.6        47      116.6       72      161.6       97      206.6       
23      73.4        48      118.4       73      163.4       98      208.4       
24      75.2        49      120.2       74      165.2       99      210.2
Last edited on
Topic archived. No new replies allowed.