Data Unit Converter

My code Wont let me have more than 9 options if someone tries to choose 10 the program thinks 1 is the option and 0 is what it needs to convert. How can I get around this?

And to avoid to make another thread how do I multiply a number when it has an exponent. See Case 10
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
#include <iostream>

using namespace std;

int main()
{

		char option;
		double Bit,B,KB,MB,GB,TB,PB,EB,ZB,YB;

		for( ; ; ) {
			do {
				cout<<"Choose Option Then Press Enter (q to quit) \n\n"
					<<"0. Bits to Bytes\n"
					<<"1. Bytes to Kilobytes\n"
					<<"2. Kilobyte to Megabyte\n"
					<<"3. Megabyte to Gigabyte\n"
					<<"4. Gigabyte to Terrabyte\n"
					<<"5. Terrabyte to Petabyte\n"
					<<"6. Petabyte to Exabytes\n"
					<<"7. Exabytes to Zettabytes\n"
					<<"8. Zettabytes to Yottabytes\n"
					<<"\n9. Bits to Kilobytes\n"
					<<"10. Bits to Megabytes\n"
					<<"\nChoose Unit Conversion: ";
				cin>> option;
				cout<<'\n';
	
		} while(option < '0' || option > '10' && option != 'q');

			if(option == 'q') break;

			switch(option)
			{
			case '0' :
				cout<<"Enter Bits to be converted: ";
				cin>>Bit;
				cout<<"\n";
				B = Bit * 0.125;
				cout<<Bit<<" Bits = "<<B<<" Bytes\n";
				break;

			case '1' :
				cout<<"Enter Bytes to be converted: ";
				cin>>B;
				cout<<"\n";
				KB = B * 0.0009765625;
				cout<<B<<" Bytes = "<<KB<<" Kilobytes\n";
				break;

			case '2' :
				cout<<"Enter Kilobytes to Be converted: ";
					cin>>KB;
				cout<<"\n";
				MB = KB * 0.0009765625;
				cout<<KB<<" Kilobytes = "<<MB<<" Megabytes\n";
				break;

			case '3' :
				cout<<"Enter Megabytes to Be converted: ";
				cin>> MB;
				cout<<"\n";
				GB = MB * 0.0009765625;
				cout<<MB<<" Megabytes = "<<GB<<" Gigabytes\n";
				break;

			case '4' :
				cout<<"Enter Gigabytes to be converted: ";
				cin>> GB;
				cout<<"\n";
				TB = GB * 0.0009765625;
				cout<<GB<<" Gigabytes = "<<TB<<" Terrabytes\n";
				break;

			case '5' :
				cout<<"Enter Terrabytes to be converted: ";
				cin>> TB;
				cout<<"\n";
				PB = TB * 0.0009765625;
				cout<<TB<<" Terrabytes = "<<PB<<" Petabyte\n";
				break;

			case '6' :
				cout<<"Enter Petabytes to be converted: ";
				cin>> PB;
				cout<<"\n";
				EB = PB * 0.0009765625;
				cout<<PB<<" Petabytes = "<<EB<<" Exabytes\n";
				break;

			case '7' :
				cout<<"Enter Exabytes to be converted: ";
				cin>>EB;
				cout<<"\n";
				ZB = EB * 0.0009765625;
				cout<<EB<<" Exabytes = "<<ZB<<" Zettabytes\n";
				break;
				
			case '8' :
				cout<<"Enter Zettabytes to be converted: ";
				cin>>ZB;
				cout<<"\n";
				YB = ZB * 0.0009765625;
				cout<<ZB<<" Zettabytes = "<<YB<<" Yottabytes\n";
				break;

			case '9' :
				cout<<"Enter Bits to be converted: ";
				cin>>Bit;
				cout<<"\n";
				KB = Bit * 0.0001220703125;
				cout<<Bit<<" Bits = "<<KB<<" Kilobytes\n";
				break;

			case '10' :
				cout<<"Enter Bits to be converted: ";
				cin>>Bit;
				cout<<"\n";
				MB = Bit * 1.1920928955078125e-7;
				cout<<Bit<<" Bits = "<<MB<<" Megabytes\n";
				break;
			}
			cout<<"\n";
	}
}
Last edited on
You could change option to an int and remove the 's around each number.
when I change option to int none of the options work the menu just loops
You're doing this as a do ... while loop. When an option is chosen, it meets the criteria, so it starts that loop over again. You don't have any way to get out of that loop.

You might think about whether do ... while is really what you want to do. If it is (though I'm not sure why), then you might try moving the while statement to after the switch, so that the switch, and thus all the conversions, are part of the loop.

Also, why the for (;;) statement? What is that doing for you?

And I agree with Pabloist. You need to change the char option to an int, and deal with quitting in another way. char only takes in the first character, which when you enter '10' will be the '1'. char ignores the '0' and then that is what is left in the stream to be converted. Having 'option' be an int, where they can enter 99 to quit, and adding that to your switch would be one way to deal with it.
When you change to an int you need to change all of your case statements to int's.
case '1' is equivalent to case 0x30, you need to make it case 1 and so forth.
Ok Its working now thank you all who helped.
Last edited on
Topic archived. No new replies allowed.