issue within code please help

program will not run as intended and I can not figure out why, i have used the same format in a prior program and it ran perfectly. is there a syntax error I am missing?

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
// 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)
//************

#include <iostream> // automatic inclusion of headers
#include <windows.h> // allows the use of the cleaer screen feature 
#include <iomanip>

using namespace std;

// declare variables below here 
short int choice;  // this statement initiates choices 
char redo;    // this statement reinitiates the loop 

double celsiusTemp;  
double fahrenheitTemp;

int main()

						

	{ 
	   while (redo=3)// here I initiate the while loop
    				{ 
    				 cout << setw(40) << "MENU\n\n";    	
        			 cout << "Please execute a command number: \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.\n";
        			 cout << "\nCommand number: ";
        			 cin  >> choice;system("cls");
        			 cout << "\n" << endl;
        			 
    	   if (choice==1)
	 	 				{ // here I initiate a if/else format within the while loop 
        			 cout << "Enter Fahrenheit temperature: ";
           			 cin  >> fahrenheitTemp;
          			 celsiusTemp=((fahrenheitTemp-32.0f)*5.0f/9.0f);
        			 cout << "\nThe Celsius temperature is: " << setprecision (4) << celsiusTemp << endl;
                        }
                        
      else if (choice==2)
	 					{ 
	 			     cout << "Enter Celsius temperature to convert: ";
                   	 cin  >> celsiusTemp;
                   	 fahrenheitTemp=((celsiusTemp*9.0f/5.0f)+32.0f);
                   	 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 << "Thanks for using my program - Robert.\n";
					return 0;
     					}
     
	 				 cout << "\nEnter [0] to do another conversion: ";
      				 cin >> redo;
      				 system("cls");
      				}
	}
     
Line 29:
 
   while (redo=3)// here I initiate the while loop 

Missing an equal sign.

Edit:
Smac below me is more accurate.
Last edited on
My guess is that it goes into an infinite loop?

Take a look at line 19, line 19, line 19, line 19, line 29, line 19, line 29, line 19 and line 64
how would I fix that? putting another = in line 29 creates an infinite loop
I am not sure what problem you are having but this program works fine
I just ran it and does everything it is supposed too. What problem are you having? Also note if you add another = to the while loop it just does nothing
closed account (18hRX9L8)
Ignore this...
Last edited on
Three problems I see right now:

1) Your while statement on line 29 is supposed to have a Boolean expression in the parentheses. You are assigning 3 to redo, not testing if it is 3 or not. For your program, I think a do/while block is a better fit. This will run your program at least once and then test the condition after each loop.

2) If your intent was for testing redo on line 29, then you tried to use redo while it was not initialized.

3) You ask the user to enter 0 to do another conversion yet you stream the user input to a char variable. The ASCII value of '0' is not equal to the integer 0. Just declare redo as an int. The istream class already has a way to convert user input to a number instead of a letter.
how would I fix that? putting another = in line 29 creates an infinite loop


change line 19 to int redo(3). Then after it runs the first time, type 4 and it should exit. If you want to use char instead line 19 should be char redo('3') and line 29 should be while (redo=='3'). Run it again and type 4 when prompted
Last edited on
closed account (18hRX9L8)
Ignore this
Last edited on
daleth I am a very visual person is there anyway you could show me what needs to be done to correct the mistakes?

i changed line 29 yet still do not get the results
@weaver126

Check out this for an entirely different approach:

http://www.cplusplus.com/forum/general/98946/#msg532033


I always use a switch for anything that requires a choice by the user.

Hope all goes well :)
the program still does not run properly after I do those corrections smac89.
I basically said what Smac said.

Line 19:
 
  char redo = '3';

Starting from line 29:
1
2
3
4
do{
   /*Your conversion code...*/
    if(choice == 3){/*Exit program...*/}
}while(redo == '3');


On a side note, if you've learned about the switch/case block, you have the option of using that instead of a string of if/else if statements.
Last edited on
at first I was using a switch statement but I changed it to a while for the use of my loop
Mine has a switch inside a while loop, and is a much better solution.
theideasman heres my new code, can you help me correct the errors?
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
#include <iostream>
#include <iomanip>
#include <windows.h>

using namespace std;


 double rectangular (double l, double w, double h)  //here I declare how the program will calculate the volume of the fish tank. (l*w*h) 
{
  double rect_vol;
  rect_vol=l*w*h*0.00433;
  return (rect_vol);
}
double fishTank (double l1, double w1, double h1)  //here I declare how the fish tank will calcule tank volume from in to feet to gallons. 
{
	double rect_vol1;
	rect_vol1=l1*w1*h1*0.00433*12;
	return (rect_vol1);
}
	char redo;  // here I declare my variable
	char choice; //choice statement allows user a menu 
	double l,w,h;  // Important variables for conversion l = length w=width h=height
	double l1,w1,h1;
	double rec; // this variable is for cout of the rectangle 
	double rec1;
	bool Quit = false;
	char option;
	
int main()

while (!Quit){

    switch (option){
           case 'a':
          	cout<<"Enter length ";
			cin>>l;
			cout<<"Enter width ";
			cin>>w;
			cout<<"Enter height ";
			cin>>h;
			rec = rectangular (l,w,h);
			cout << "The volume of your fish tank, in gallons, is approximately: "<< setprecision (5) << rec << endl;	
           break;

           case 'b':
          cout<<"Enter length ";
										 cin>>l1;
										 cout<<"Enter width ";
										 cin>>w1;
										 cout<<"Enter height ";
										 cin>>h1;
										 rec1 = fishTank (l1,w1,h1);
										 cout << "The volume of your fish tank, in gallons, is approximately: "<< setprecision (5) << rec1 << endl;	
										cin.get();
           break;

           case 'q': //user wants to quit
                  Quit = true;
                 	cout << "Thanks for using my program - Robert.\n";
										return 0;
                   break;
      
           default: 
               //deal with bad input
    }

}
Do you have compile errors - then post them in full.

There are some style problems:

1. Don't use global variables
2. come with better names for your functions & variables
3. avoid repeating code especially for input
4. avoid unnecessary variables
5. use the tolower or toupper function to avoid some of the errors on input
6. have a ShowMenu function & call it from inside the switch
7. Get the menu option input from inside the switch
8. Think of something to put in the default clause

Have a go at all that.

Edit:

9. Prefer to call functions from switch case's rather than put code into the case.
Last edited on
0. Indent your code.
In Visual Studio highlight all your code, then go to Edit -> Advanced -> Format Selection. And for goodness sake remove all those tabs.
Right off the bat, your main function needs an opening and closing brace.
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
#include <iostream>
#include <iomanip>
#include <windows.h>

using namespace std;


double rectangular (double l, double w, double h)  //here I declare how the program will calculate the volume of the fish tank. (l*w*h) 
{
    double rect_vol;
    rect_vol=l*w*h*0.00433;
    return (rect_vol);
}
double fishTank (double l1, double w1, double h1)  //here I declare how the fish tank will calcule tank volume from in to feet to gallons. 
{
    double rect_vol1;
    rect_vol1=l1*w1*h1*0.00433*12;
    return (rect_vol1);
}
char redo;  // here I declare my variable
char choice; //choice statement allows user a menu 
double l,w,h;  // Important variables for conversion l = length w=width h=height
double l1,w1,h1;
double rec; // this variable is for cout of the rectangle 
double rec1;
bool Quit = false;
char option;

int main()

    while (!Quit){

        switch (option){
        case 'a':
            cout<<"Enter length ";
            cin>>l;
            cout<<"Enter width ";
            cin>>w;
            cout<<"Enter height ";
            cin>>h;
            rec = rectangular (l,w,h);
            cout << "The volume of your fish tank, in gallons, is approximately: "<< setprecision (5) << rec << endl;	
            break;

        case 'b':
            cout<<"Enter length ";
            cin>>l1;
            cout<<"Enter width ";
            cin>>w1;
            cout<<"Enter height ";
            cin>>h1;
            rec1 = fishTank (l1,w1,h1);
            cout << "The volume of your fish tank, in gallons, is approximately: "<< setprecision (5) << rec1 << endl;	
            cin.get();
            break;

        case 'q': //user wants to quit
            Quit = true;
            cout << "Thanks for using my program - Robert.\n";
            return 0;
            break;

        default: 
            //deal with bad input
        }

    }
Last edited on
Topic archived. No new replies allowed.