need help with break statement or return statement. Dev C++ temp converter

Hi, I am new to the forum and relatively new to programming.

In my intro to computer programming class we were assigned a project to create a simple temperature converter so here's mine.

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
// Project2Tempconverter.cpp

//*************
//program: Project 2 - Temperature conversion
//Name: Robert Weaver
//Date: 3/21/2013
//description: Write a program that converts (C)to(F) and (F) to (C) with choices. 
//************
#include <cstdlib>
#include <iostream>  // automatic inclusion of headers
#include <windows.h> // allows the use of the cleaer screen feature
#include <iomanip>   // allows use of setprecision


using namespace std;
                     // declare variables below here 
short int choice;    // <--this statement initiates choices 
char redo;           // <--this statement reinitiates the loop 

float celsiusTemp;  
float fahrenheitTemp;

int main()
						 { 
    while (redo=3) // here I initiate the while loop
                         {
                     
    				 cout << setw(40) << "MENU\n\n";    	
        			 cout << "Please execute a command number: [1],[2], or [3].\n\n";
           		     cout << "[1] if you would like to convert Fahrenheit(F) to Celsius(C).\n";
      			     cout << "[2] if you would like to convert Celsius(C) to Fahrenheit(F).\n"; 
        			 cout << "[3] if you would like to close the program." << endl;
        			 cout << "\nCommand number: ";
        			 cin  >> choice;
        			 cout << "\n";
        			 system("cls");
     
	 	  if (choice==1){ // here I initiate an if/else format within the while loop 
        			 cout << "\nEnter Fahrenheit temperature: ";
           			 cin  >> fahrenheitTemp;
          			 celsiusTemp=((fahrenheitTemp-32)*5/9);
        			 cout << "\nThe Celsius temperature is: " << setprecision (4) << celsiusTemp << endl;
                        }
     
	 else if (choice==2){ 
	 			     cout << "\nEnter Celsius temperature to convert: ";
                   	 cin  >> celsiusTemp;
                   	 fahrenheitTemp=(celsiusTemp*9/5)+32;
                   	 cout << "\nThe Fahrenheit temperature is: " << setprecision (4) << fahrenheitTemp << endl;
         			 cin.get();
     					}
       while (choice==3){ // here I use another while loop to be able to exit the program with a return 0; statement 
     				cout << "Press any key to close the program." << endl;
     				return 0;
     					}
     
	 				 cout << "\nenter [0] to do another conversion: ";
      				 cin >> redo;
      				 system("cls");
     				   
						}
     				    
                        }
     


My question is, how do I break the infinite loop that happens when anything besides a numeric value is entered?

I would like something along the lines of cout << "That input is not valid; please return to the menu [0]."; to appear when anything besides numeric input is entered.

Secondly, is there something besides return 0; that I can do to close the program out?

p.s. I could use some help with developing pseudo code also.

as far as everything else is concerned I am very pleased with my program since it is my first.

thanks for any cin >>
Last edited on
I would most likely do a switch statement for "invalid input".

This is how I would set it up:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
switch(choice)
{
       case 1:
           //insert code here
           break;

       case 2:
          //insert code here
          break;

       case 3:
          //insert code to exit here
          return;        //return statement to end program

      default:
         //invalid input try again
         //ask for input again
         continue;     //to allow the switch statement to start over again
}


Not 100% sure if this the exact way to set it up, but this is a method I would approach to solve that problem.
1
2
3
4
5
6
7
8
9
10
#include <ctype.h>

if(isdigit(variable))
{
  //valid input
}
else
{
  //invalid input
}


~ just google isdigit or isalpha, you can know more about it. isdigit checks whether the input is a decimal digit character.
continue is for loops, and will not do what you think for the switch.

A better way is to enclose the switch in a while loop controlled with a bool:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
bool Quit = false;

while (!Quit){

     switch(choice){

      ......
      case 3: //user wants to quit
           Quit = true; //execution continues after the while
            break;       //could use return or exit function here
  
      default:

     }
}


@weaver126

You also have global variables - which is bad. Put them in main() and send them to whichever function needs them as references.

When doing math with floating point numbers, explicitly specify them as such, so they don't do integer division - like this:

celsiusTemp=((fahrenheitTemp-32.0f)*5.0f/9.0f);

Personally, I always use doubles - if you specify them as 32.0 say they will default to double. The problem with floats is they only have 7 significant figures of accuracy. This is fine for this assignment, but it doesn't take much for it to be a problem.

Lines 28 - 33 could be a ShowMenu function.

line 35 could be combined into line 33.

With your indentation, line up the closing brace with the first character of the statement that initiated it. For example the brace on line 43 should line up with the i on line 38.

Finally, I like to break bad habits early, so try to avoid having using neamspace std;. This brings brings the entire STL into the global namespace, polluting it & causing potential for name clashes.

There are a couple things to do. You can put std:: before each std thing, or you can do this:

1
2
3
4
5
6
using std::cout;
using std::cin;
using std::endl;

using std::string;
using std::vector;


I tend to do a mixture, with the above code snippet for things used frequently, and use std:: for things that are not, such as std::find say.

Some guys use std:: regardless.

HTH
Last edited on
@GaGoKoYa

There is no need for this test, because of the default clause in the switch.

isdigit only works 1 char at a time.
@theideasman is there anyway you can show me within my code what you are saying to do with the bool and switch statement?

you seem to have far more knowledge that I do so I'm a little lost.

I changed the floats to doubles, which I actually had them at originally but I changed them to see if anything different would happen.

I combined lines 33 and 35. thanks for the info.

lines 28-33 show menu function I'm lost on, along with changing the global variables. If you could PM me and help the value would not be under appreciated.

or email if you prefer.

the program has already been submitted for grading but I would still like to know how to change these things for future reference.
knowledge is power.
Last edited on
@TheIdeasMan

Hmm, so basically you will use the switch to end the loop, right? What about if you have some restrictions where you can only use if else? ~ especially those school work.
This is basically what it would look like:

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
#include <cstdlib>
#include <iostream>
#include <windows.h>
#include <iomanip>

using namespace std;

short int choice;
char redo;

float celsiusTemp;
float fahrenheitTemp;

int main()
{
	while(1)							//forever loop
	{
		cout << setw(40) << "MENU\n\n";
		cout << "Please execute a command number: 1, 2, or 3.\n\n";
		cout << "1 if you would like to convert Fahrenheit(F) to Celsius(C).\n";
		cout << "2 if you would like to convert Celsius(C) to Fahrenheit(F).\n";
		cout << "3 if you would like to close the program." << endl;
		cout << "\nCommand number: ";
		cin >> choice;
		cout << "\n";
		system("cls");
		
		switch (choice)
		{
			case 1:								//F to C
				cout << "\nEnter Fahrenheit temperature: ";
           		cin  >> fahrenheitTemp;
          		celsiusTemp=((fahrenheitTemp-32)*5/9);
        		cout << "\nThe Celsius temperature is: " << setprecision (4) << celsiusTemp << endl;
				break;				//breaks out of switch back to while loop
				
			case 2:					//C to F
				cout << "\nEnter Celsius temperature to convert: ";
                cin  >> celsiusTemp;
                fahrenheitTemp=(celsiusTemp*9/5)+32;
                cout << "\nThe Fahrenheit temperature is: " << setprecision (4) << fahrenheitTemp << endl;
				break;
				
			case 3:							//End program
				cout << "Program ended." << endl;
				return 0;		//allows the program to end
				
			default:			//Invalid input
				cout << "Invalid input.  Try again: " << endl;
				break;									
		}
	}
}


Might have to move things around a little to make it look nice.
Last edited on
@weaver126

... can show me within my code what you are saying to do with the bool and switch statement?


That's what the code snippet with the switch inside the while loop does.

lines 28-33 show menu function


Just create a ShowMenu function that prints the menu options.

along with changing the global variables.


Just put them in main.

To use references, just place an & in the parameter list:

1
2
3
4
double FahrToCel(const double &InputTemp){

return (InputTemp-32.0)*5.0/9.0;
}


@GaGoKoYa

A switch is analogous to a series of if -else if - else statements, except that a switch can only use constant integral values known at compile time. So one can do the exact same thing with if else statements , if required.
@crimsonzero2

I don't like your infinite loop. My solution with the bool Quit variable is better. There are situations where infinite loops are appropriate, but this isn't one of them IMO.

You have global variables - which I am trying to advise the OP against.

You use integer values in your calculations, which I was also trying to advise the OP about. Did you test your program? - all the values will zero in the Fahrenheit to Celsius because of the integer division, and 9/5 is 1 in the other conversion.
@TIM

Thanks. I thought the question was about the input during the conversion (my bad) if it just for the menu, then switch is the way for it.
@TheIdeasMan

I just wrote what he had similar with the switch statement in it. I agree with what you say, just showing him where the switch statement would go.
Topic archived. No new replies allowed.