Inches to centimeters loop

So I'm supposed to write a program that prompts a user for a two numbers within 36 inches of each other.

Use a for loop as a timer.

An outer Do While Loop that will ask the User if they wish to run the program again.
An inner For Loop structure that will:
a. Initialize the For Loop counter to the beginning value entered by the User
b. Update the counter by incrementing it by six
c. Test the counter by comparing it to the ending value entered by the User
d. The body of the For Loop will use the value of the counter as the inches to convert to centimeters.

The code below is the current version of what I have. My problem is that it doesn't do the conversion and is an infinite loop.

Any help would be appreciated. Thanks!

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
 /************************** Compiler Directives **********************/

#include <cstdlib>

#include <iomanip>

#include <iostream>
using namespace std;



/**************************** main Function **************************/

int main()

{



//Local variables
int lowNum;
int highNum;
int counter;
int inches;
float centimeters;
char runAgain;

cout << "Enter you low number of inches "; //Ask user for a beginning number
	cin >> lowNum;
	
	cout << "Enter an high number of inches that is within 36 inches of your beginning number "; //Ask user for a ending number
	cin >> highNum;
	
	
	
for (lowNum = counter;
	 lowNum < highNum;
	 lowNum + 6)

{
(centimeters = (inches * 2.54));

cout << setw(5) << "Inches " <<inches <<endl;
cout << setw(15) << "Centimeters"  << centimeters  <<endl;

while (highNum > lowNum)
{
counter = lowNum * 2.54;//inches to centimeters conversion
cout << "\n" << setw(5) << 
lowNum << setw(21) << fixed << showpoint << setprecision(2) << 
counter;
highNum <= lowNum + 36;
}

}
do 

{
	cout << "Enter a beginning number of inches "; //Ask user for a beginning number
	cin >> lowNum;
	
	cout << "Enter an ending number of inches that is within 36 inches of your beginning number "; //Ask user for a ending number
	cin >> highNum;	
}

while (runAgain == 'y' || runAgain == 'Y');//program runs again if y, Y entered
{
	cout << "Enter a beginning number of inches "; //Ask user for a beginning number
	cin >> lowNum;
	
	cout << "Enter an ending number of inches that is within 36 inches of your beginning number "; //Ask user for a ending number
	cin >> highNum;	
}
(highNum > lowNum + 36);

Last edited on
Hello BIGB185,

I think you missed the closing code tag.

This might be useful:

PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

It makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



One of the first things I noticed is in the for loop. It should end with "lowNum += 6)" Otherwise "lowNum" would never change and you would be processing the same number each time through an endless for loop.

Until I straighten out your code it is hard to tell what is what.

Hope that helps for now,

Andy
Thanks, Andy. Sorry about that.
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>
#include <iomanip>

int main()
{
    // An outer Do While Loop that will ask the User if they wish to run the program again.
    char again ;
    do
    {
        // prompt user for two numbers within 36 inches of each other.
        const int MAX_DIFF = 36 ;

        std::cout << "\nenter two lengths which do not differ by more than " << MAX_DIFF << " inches.\n" ;

        int lower ;
        std::cout << "lower length: " ;
        std::cin >> lower ;

        int higher ;
        std::cout << "higher length: " ;
        std::cin >> higher ;

        if( lower <= 0 )
           std::cout << "error: lengths must be positive.\n" ;

        else if( higher <= lower )
            std::cout << "error: the higher length must be more than the lower length.\n" ;

        else if( (higher-lower) > MAX_DIFF )
            std::cout << "error: the two lengths are not within " << MAX_DIFF << " inches of each other\n" ;

        else // valid input
        {
            // An inner For Loop structure
            const int INCREMENT = 6 ; // Update the counter by incrementing it by six

            // print the header
            std::cout << "\n\n"
                      << std::setw(8) << "inches" << std::setw(15) << "centimeters" << '\n'
                      << std::setw(8) << "------" << std::setw(15) << "-----------" << '\n' ;

            for( int inches = lower ; // Initialize the For Loop counter to the beginning value
                 inches <= higher ; // Test the counter by comparing it to the ending value
                 inches += INCREMENT // Update the counter by incrementing it by six
               )
            {
                // use the value of the counter as the inches to convert to centimeters
                const double MULTIPLIER = 2.54 ;
                const double centimeters = inches * MULTIPLIER ;
                std::cout << std::fixed << std::setprecision(2) \
                          << std::setw(8) << inches << std::setw(15) << centimeters << '\n' ;
            } // end for loop
        } // end else valid input

        std::cout << "\n\nrun the program again (y/n)? " ;
        std::cin >> again ;
    }
    while( again == 'Y' || again == 'y' ) ; // end outer do while loop
}
Hello BIGB185,

No worries.

As I read the instructions you program should look something like:

do
{
cin >> lowNum;
cin >> highNum;

for (counter = lowNum; counter < highNum; counter += 6)

prompt to continue
} while (std::toupper(runAgain == 'Y'); // <--- "toupper" needs header file "cctype".


This is just a quick thought for now. What you have is usable. It just needs rearranged in the correct order.

I see you fixed the code tags. Now the proper indenting would help.
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
/************************** Compiler Directives **********************/

#include <cstdlib>
#include <iomanip>
#include <iostream>

using namespace std;

/**************************** main Function **************************/

int main()
{
	//Local variables. This msy work, but it is usually a good idea to initialize the variables.
	int lowNum{};
	int highNum{};
	int counter{};
	int inches{};
	float centimeters{};  // <--- "double" would be a better choice.
	char runAgain{ 'Y' };  // <--- Or could be 'N'.

	cout << "Enter you low number of inches "; //Ask user for a beginning number
	cin >> lowNum;

	cout << "Enter an high number of inches that is within 36 inches of your beginning number "; //Ask user for a ending number
	cin >> highNum;



	for (lowNum = counter; lowNum < highNum; lowNum + 6)
	{
		(centimeters = (inches * 2.54));

		cout << setw(5) << "Inches " << inches << endl;
		cout << setw(15) << "Centimeters" << centimeters << endl;

		while (highNum > lowNum)
		{
			counter = lowNum * 2.54;//inches to centimeters conversion
			cout << "\n" << setw(5) <<
				lowNum << setw(21) << fixed << showpoint << setprecision(2) <<
				counter;
			highNum <= lowNum + 36;
		}

	}

	do

	{
		cout << "Enter a beginning number of inches "; //Ask user for a beginning number
		cin >> lowNum;

		cout << "Enter an ending number of inches that is within 36 inches of your beginning number "; //Ask user for a ending number
		cin >> highNum;
	}

	while (runAgain == 'y' || runAgain == 'Y');//program runs again if y, Y entered
	{
		cout << "Enter a beginning number of inches "; //Ask user for a beginning number
		cin >> lowNum;

		cout << "Enter an ending number of inches that is within 36 inches of your beginning number "; //Ask user for a ending number
		cin >> highNum;
	}

	(highNum > lowNum + 36);

	return 0;
}


I need to go over the instructions again to make sure I have it correct before I can rework your program.

Hope that helps,

Andy
Thanks, Andy. I'm just a little confused on the order then I guess.
Last edited on
So I've been playing with the code and this is the current version. Unfortunately, it is only computing the beginning number and not incrementing by six. It's also not asking if the user would like to run the program again and it doesn't seem to care if the user enters a number that is greater than 36 inches than the beginning number.


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
/************************** Compiler Directives **********************/

#include <cstdlib>

#include <iomanip>

#include <iostream>
using namespace std;



/**************************** main Function **************************/

int main ()

{

//No Local constants
float counter = 0; //While Loop counter
char runAgain = 'Y'; //ask to run program again

//Local variables
int beginNum; //beginning number of inches
int endNum; //end number of inches
int inches;
float centimeters;
/*************** Begin main Function Executables *****************/

for (counter = beginNum;
	 counter < endNum;
	 counter += 6);

do 

{
	cout << "Enter a beginning number of inches "; //Ask user for a beginning number
	cin >> beginNum;
	
	cout << "Enter an ending number of inches that is within 36 inches of your beginning number "; //Ask user for a ending number
	cin >> endNum;	
}

while (endNum < beginNum && endNum >= beginNum + 36);
{
	counter = beginNum * 2.54;//inches to centimeters conversion
	
	cout << "\n" << setw(8) << "Inches" << setw(21) << "Centimeters" <<"\n" << "**********" << setw(20) << "*************";//conversion table
	cout << "\n" << setw(5) << beginNum << setw(21) << fixed << showpoint << setprecision(2) << 
	counter;//information on conversion table
}





while (runAgain == 'y' || runAgain == 'Y');//program runs again if y, Y entered
{

	cout << "Enter your beginning number: ";//asks user to input the beginning number
	cin >> beginNum;

	cout << "Enter an ending number of inches that is within 36 inches of your beginning number "; //Ask user for a ending number
	cin >> endNum;
}



return 0;
}// end main 
Last edited on
My apologies to JLBorges, I didn't see your post before posting again. Your program works. I had to edit it just a touch to fit within what we've done in class so far but I think or at least I hope, I've got it figured out.

Thank you once again, gentleman.
I do have a question though. Does the "const int INCREMENT = 6 ;" bit of code essentially do the job that the "for loop" was supposed to do? Wouldn't that essentially negate the use of the "for loop" as a timer in this instance?

Ben
> Does the "const int INCREMENT = 6 ;" bit of code essentially do the job that the "for loop" was supposed to do?

No. It just declares a constant value which specifies the amount of the increment.

This is the for loop construct; the bolded part increments the loop counter by INCREMENT (6):
1
2
3
4
for( int inches = lower ; // Initialize the For Loop counter to the beginning value
     inches <= higher ; // Test the counter by comparing it to the ending value
     inches += INCREMENT // Update the counter by incrementing it by six
   )
Thank you, JLBorges.
Topic archived. No new replies allowed.