Hello all.
Well, I'm still quite new at programming and utilizing self-study at home with Prata's C++ Primer Plus 6E, and on chapter 3, exercise 1. Well, I'll leave the code. The header comments give the directions, and my closing comments largely cover my thoughts on the exercise.
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
|
// 1HeightConversion.cpp -- convert inches to feet and inches.
// Inputs inches in integer, uses underscore for input, and
// utilizes a symbolic constant conversion factor.
#include <iostream>
int main()
{
std::cout << "Please enter your height in inches:__\b\b";
int inches;
std::cin >> inches;
std::cin.get();
// Conversion for feet.
const int feet = inches / 12;
// Modulus for leftover inches.
const int loinches = inches % 12;
std::cout << "Your height is " << feet << " feet, " << loinches << " inches.";
std::cin.get();
return 0;
}
// The constants could have just as easily been replaced by simple integers.
// It feels as though I've either done this wrong despite the code working properly,
// am incorrectly utilizing const, and this entire exercise could and probably would
// have been done without using const at all.
|
So, I'm trying not to get into bad habits, which is why you don't see namespaces (I read about that) and attempts are always made to use logical naming conventions, and spaced out for readability. I've done a bit of homework here, and the code does what it's supposed to do - no question there. I'm just concerned I'm doing something that could get me in trouble later, and I'm also thinking that, despite it being an exercise, declaring and initializing those two constants arbitrary and didn't serve much educational purpose.
I need people that know about this more than myself. Originally, I thought, "I'll just make the constant the entire conversion in one line, output the text where needed. It should accept integers and cout commands, right?" I was wrong, and if that's even possible it's well beyond my level.