• 15
• 15
• 11
• 9
• 10

# First assigned/complete project, check please.

This topic is 3700 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Well, I'm teaching myself C++ via some old class chapters/exercises that a good guy gave me. It's bean going well, I love programming so far even though I can't do much with it yet. this is my first project that the book assigned and I want to make sure that everything I used and how I used it was correct, Here's what it asked for and my source code is down there too. **Write a program that converts Miles to Yards, Yards to Feet, and Feet to Inches. You are expected to perform the same conversions twice with two sets of variables. (one set of int, and one set for float point variables)**
[Source]
#include <iostream>

int main()
{
long int IntMiles = 10, IntYards = IntMiles * 1760; // Miles to Yards
long int IntFeet = IntYards / 3; // Yards to Feet
long int IntInches = IntFeet / 12; // Feet To Inches

float FloatMiles = 13.14, FloatYards = FloatMiles * 1760; // Miles to Yards
float FloatFeet = FloatYards / 3; // Yards to Feet
float FloatInches = FloatFeet / 12; // Feet to Inches

//First set, int conversions
std::cout << "**First set of project, int conversions**" << std::endl << std::endl;

std::cout << "Miles: " << IntMiles << "\nConverted to yards: " //miles to yards
<< IntYards << std::endl << std::endl;

std::cout << "Yards: " << IntYards << "\nConverted to feet: " //yards to feet
<< IntFeet << std::endl << std::endl;

std::cout << "Feet: " << IntFeet << "\nConverted to inches: " //feet to inches
<< IntInches << std::endl << std::endl;

//Second set, Float point conversions
std::cout << "**Second set, Float Point Conversions**" << std::endl << std::endl;

std::cout << "Miles: " << FloatMiles << "\nConverted to yards: " // miles to yards
<< FloatYards << std::endl << std:: endl;

std::cout << "Yards: " << FloatYards << "\nConverted to feet: " // yards to feet
<< FloatFeet << std::endl << std::endl;

std::cout << "Feet: " << FloatFeet << "\nConverted to inches: " //feet to inches
<< FloatInches << std::endl << std::endl;

std::cout << "Press <ENTER> to exit window";

std::cin.get(); //Stops console screen from flashing by
return 0; //not exactly sure but i use it alot
}
[/Source]
[/source]

##### Share on other sites
Enclose your code in 'source' tags, like this:

[source]

...code goes here...

[/source]

:)

##### Share on other sites
Looks pretty good.

1. First of all, kudos for not dumping the entirety of the C++ standard library into the global namespace - that's something we don't see very often :)

2. You don't need to write 'long int' - you can just write 'long' (the 'int' is implied).

3. Many style guides recommend that multiple variables not be declared/initialized in the same statement (instead, put each in its own statement, on its own line).

4. There are no hard and fast rules regarding naming conventions. However, it's somewhat uncommon to begin variable names with an upper-case letter (starting with an upper-case letter is more often reserved for class/struct names and/or function names).

5. The 'return 0' at the end of the main() function returns an int (as specified by the function's return type) to the calling environment; it is often used to signal to the calling environment whether everything went ok ('0', typically), or something went wrong (a non-zero value signifying an error of some sort). As it happens, if you always intend to return '0' from main(), you can just leave the return statement out (the 'main' function is special in that it returns 0 implicitly in the absence of a return statement).

An obvious next step would be to create functions to perform the various conversions, but I imagine you'll get to that in future exercises...