Inches to feet problem

Asks user height in Inches,
output should look like. Your height is 6'2".

(assuming user entered 75)


This is what i got so far, but its not working since its a double value instead of integar can someone write a program for me. which is similar to this if possible


// Hasnain Attarwala
// Lab1 problem final
// Page NA
// Inches to Feet problem

#include <iostream>

using namespace std;

int main ()
{
// declare variables
float htEntered;
float htHolder;
float htFeet;
int htInches;
float conversionRate;




// assign
conversionRate = 0.0833333333f;

// Ask height from user
cout << "Enter height in Inches ";
cin >> htEntered;

htHolder = htEntered * conversionRate; // inches to feet formula



htFeet = floor (htHolder);
//Typecasting

int htEnteredInt = htEntered;

htInches = (int) htEnteredInt % 0.0833333333;

// Print
cout << "The height in feet is " << htFeet << "\' " << htInches << "\" " << endl;




return 0;
}

// Output
It seems to me, your program was a little overkill in variables. Also, 75" is equal to 6' 3". Anyway, here is your revamped program.

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
// Inches To Feet.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>

using namespace std;

void Convert(int inch)
{
	int feet, inches;
	inches = inch %12;
	feet = inch/12;
	cout << "\n\t\tThe height in feet is " << feet << "\'" << inches << "\" " << endl;
}

int main ()
{
int htInches;
// Ask height from user
cout << "\n\n\t\tEnter height in Inches ";
cin >> htInches;

Convert( htInches);
cout << "\n\n\n\t\t";
return 0;
}


Last edited on
What does this do

"\n\n\n\t\t";

Just had one day of coding class, super excited want to learn everything
'\n' is newline, Does the same as 'endl'. The '\t' is a 'tab' command. It, puts spaces on the line, so the characters printed will not be right next to the left edge of the screen. They are not really necessary, but I like centering the text a little on screen
Thanks
It would print 3 line breaks and 2 tabs, which makes no sense...

Try something 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
#include <iostream> //so we can get input and give output
using namespace std; //so we don't have to prefix all are cin's and cout's with "std::"

int main() { //our entree point
	int total; //total inches
	cout << "How many inches: "; //Print out a message
	                             // so the user knows that he/she is supposed to enter something.
	cin >> total; //Ask for input.
	if( !cin.good() ) { //Check the validity of the input.
	 //The user did not enter an int, or some other error occurred while trying to read from cin.
		cerr << "Whoops, that's not supposed to happen..." << endl; //Print out an error message.
		return 1; //Return from main which ends the program.
		          // (Note: returning a number other then 0 from main
		          // signifies that the program did not run correctly.)
	}
	int feet = total / 12; //Remember that an int divided by an int (ie. "total / 12") will return an int,
	                       // if the quotient normally wouldn't be an integer
	                       // then it will truncate the quotient.
	                       // (Ex. 27 / 12 == 2 because 27 and 12 are both ints)
	int inches = total % 12; //Since integer division only returns the quotient with no remainder,
	                         // we use the modulus operator (pronounced "mod") to get the remainder.
	                         // (Ex. 27 % 12 == 3 because 27 / 12 in math is 2 and 3/12)
	                         // Note: we can also find the remainder by subtracting feet times 12
	                         // from the total. (Ie. total % 12 == total - feet * 12 == total - total / 12 * 12)
	                         // This may seem counter-intuitive,
	                         // but remember that total / 12 will have a trucated quotient.
	cout << feet << '\'' << inches << '"' << endl; //Print out our new feet and inches variables,
	                                               // along with two characters for notation.
	return 0; //We end our program, returning 0 (or signifying that it ran correctly.)
}

Please let me know if you have any questions.
Last edited on
mathhead...

The reason for the 3 linefeeds and 2 tabs, was so that the text, "Press any key to continue..." that is shown at the end of the program, would not be directly under and to the far left, of the text above. It would be centered along along with the other text above it. I like neatness, that's all.
I compiled your code, and entered 78.7 as inches. The program still gave the feet and inches, even though I did not enter an integer. I did not get the error message as expected.

whitenite1
First of all that "Press [the] any key..." text comes from your IDE, not the program.

Now: the reason your "78.7" input got treaty as 78 and didn't error out is because of the nature of the cin >> ... operation. Even though the statement doesn't begin to parse the entered text until you hit enter, this doesn't mean it reads the whole line (ie. all of the text you typed.) In the case of string or char*, the read will stop parsing at the first white space (ignoring leading white space.) To show this, try the following test program:
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <string>
using namespace std;

int main() {
   string a, b, c;
   cin >> a >> b >> c;
   cout << "a is \"" << a << "\"\nb is \"" << b << "\"\nand c is \"" << c << "\"\n";
   return 0;
}
Run this program and and enter the text
Hello, world! My name is whitenite1.
You should get back
a is "Hello,"
b is "world!"
and c is "My"
As you see, not only are only the first three words gather from the stream, but you only need to hit enter one time. Now in the case of my cin >> ...//into an int the parsing stops at the first non-number (although it will allow a leading minus ('-') sign.) So it stops at your '.' character and just parses "78" which stores the int 78 into total. The remaining ".7" and linebreak are left in the stream.

If you wanted a more thorough error check you could parse the whole line with the function
getline( cin, ... ); //as opposed to cin >> ... included in the <string> header file. Then you could manually parse the whole line to check if it is indeed an integer and nothing else (where the "..." is a C++ string.)
@mathhead200

My response of August 29th was mainly in regard to your statement
It would print 3 line breaks and 2 tabs, which makes no sense...
which, IMO, is false. It did make sense, and I was explaining why it did.
I knew that the "Press any key.. " text comes from the IDE, but it still is printed. As I stated before -
The reason for the 3 linefeeds and 2 tabs, was so that the text, "Press any key to continue..." that is shown at the end of the program, would not be directly under and to the far left, of the text above. It would be centered along along with the other text above it. I like neatness, that's all.

And the reason I mentioned my '78.7' input, was because in your comment after (!cin.good()), you mentioned
//The user did not enter an int, or some other error occurred while trying to read from cin.
, so I entered a non-integer number. I guess you didn't mean an actual non-integer number, but a non-numerical input. I did see though, that by entering text, I did get the error message.
Thanks for the test program explanation. It was helpful..
whitenite1 wrote:
My response of August 29th was mainly in regard to your statement
"It would print 3 line breaks and 2 tabs, which makes no sense..." (mathhead200)
which, IMO, is false. It did make sense, and I was explaining why it did.
I knew that the "Press any key.. " text comes from the IDE, but it still is printed.

Fine, but it bad to do stuff like that in a "real" program. Users are not going to run your program through an IDE, and not all developers use an IDE. For one, I compile and run from a command prompt or shell. If I were to run your program in a Linux shell, it would cause the
%Mathhead200>
following the program's execution to appear two tabs into the middle of my screen. Please don't take offense. When I said to Hasnain Attarwala that it made no sense, I guess I was just trying to convey that he need not emulate that in his own code. I was also typing my response before you responded, so I hadn't read your post at 2:54pm yet...

As for your other remark, you may have "entered a non-integer", but you also entered an integer prefixed onto it. You have to think like a computer. So it wasn't that the comment was wrong, it was incomplete. I never guaranteed that the user stopped typing after they entered their int. And again, you can't really make that kind of check when dealing with that default >> operator, or at the very least it would be overly unnecessary to limit your self to that one method of gaining input for an application that needs to preform that kind of check.

I'm glad the test program was helpful. :)
Last edited on
@mathhead200

Thank you for giving me your reasoning. I haven't compiled from the command prompt, though I have run programs from it. I do notice that the program closes rapidly right after finishing. I will have re-think how I end my programs, so that doesn't happen anymore.

whitenite1
The closing rapidly isn't a problem if the program is being run from a text prompt, as console programs should be. However, if you want the console the stay open after it's started from a GUI or an IDE with a "Press Any Key..."-type message, there is a nice sticky thread on the beginner's forum of this site you can read through.
Topic archived. No new replies allowed.