A length converter by a noob

This is a Length Converter that I made (i am a 14 yr old noob) and I would like to know if there is anything I need to fix or I should change to make it better.
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
#include <iostream>

using namespace std;

int main()
{
	double f, m;			// Variable for Feet and Meters		

	char option;		       // Variable for menu option

	for( ; ; ) {	               //loop for menu
		do {
	cout<<"Choose an option (Press q to quit) \n";
		cout<<"1. Convert Feet to Meters \n";
		cout<<"2. Convert Meters to feet \n";
		cout<<"Choose option and press enter: ";
		cin>>option;

		cout<<'\n';

		} while( option < '1' || option > '2' && option != 'q');

		if(option == 'q') break;

		switch(option) {

		case '1' :                 //Option 1 convert Feet To Meters
			cout<<"\nEnter Feet to be converted \n";
			cout<<"\nFeet: ";
			cin>> f;
			m = f * 0.3048;
			cout<< f << " feet = " << m << " meters \n";
			break;
		
		case '2' :		//Option 2 convert meters to feet
			cout<<"\nEnter Meters to be converted \n";
			cout<<"\nMeters: ";
			cin>> m;
			f = m * 3.2808399;
			cout<< m << " meters = " << f << " feet\n";
			break;
		}
		
		cout<<"\n";			//space between menu loops

	}
	return 0;
}

Last edited on
cout<<"Choose an option (Press Q to quit) \n"; if(option == 'q') break;

Should keep things consistent - don't ask for a Q and take a q.
ok thank you :)
anyone else want to comment
Last edited on
Try to do it in using a single loop.
May I ask for clarification
Yes, sure...
Rather than using an infinite loop and then another loop within it and breaking it based on an if condition, use a single loop - get the user option as integer and switch on that integer. Exit the loop if the input does not match.
I am not too good at explaining like this... will post the code if u ask.

Ok I think I get it but im not so sure can you post an example please
Like this:

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
int main()
{
   double f, m = 0.0;
   int option = 0;

   do
   {
      cout<<"Choose an option (Press any other key to quit) \n"
          <<"1. Convert Feet to Meters \n"
          <<"2. Convert Meters to feet \n";

      cin>>option;

      switch (option)
      {
         case 1 : cout<<"\nEnter Feet to be converted \n";
                  cin>> f;
                  m = f * 0.3048;
                  cout<< f << " feet = " << m << " meters \n";
                  break;
         case 2 : cout<<"\nEnter Meters to be converted \n";
                  cin>> m;
                  f = m * 3.2808399;
                  cout<< m << " meters = " << f << " feet \n";
                  break;
      }
   }while (option == 1 || option == 2);

   return 0;
}

oops.. there's an error... use option as char only and compare against '1' and '2'
right after the: cin>>option; go right to a switch statement.
What?
oh shit... shoulda looked at your post mgupta.
Topic archived. No new replies allowed.