CONverting kilograms

Hi, I am a beginner, I have an assignment to convert kilograms to pounds and kilometers to miles.
I have created a code that works, however I don;t think it is neat. Can anybody help please?

I tried to make the answers in different lines but it did not work for all.


// kilogram1.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>

int main()
{
std::cout << "Hello, I am Ghadeer !\n";

int x; // x is the kilometer
std::cin >> x;
std::cout << x * 0.621371; // this is to convert kilometer to miles


int y; // y is the kilograms
std::cin >> y;
std::cout << y * 2.2 ; // this is to convert kilogrms to pounds

std::cout << " Thank you for using this program!";
return 0;

}
Add std::cout << std::endl; when you want the output to start on the next line.
Last edited on
Thank you , it works.
I am not so satisfied about the code even though it works. What do you think?
I think its probably your second or third program, and you have nothing to worry about. I see good habits, you used std:: which is already better than 95% of new coders. I see some comments to help understand it, which is good as well.
Variable names help twice over, once to make it easier to read, and they eliminate comments.
consider
int y; // y is the kilograms
int kg; //self explanatory... no comment needed here?

It would be better using code tags, <> on the side bar editor. Try to do that going forward.

Lets give it a rewrite for fun. I added some prompts to tell user what to do, named variables as to what they are doing, added constants instead of 'magic numbers' that have unclear meaning.

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

// kilogram1.cpp : This file contains the 'main' function. Program execution
//begins and ends there.

#include <iostream>

int main()
{
      constexpr double km2mi  = 0.621371;
      constexpr double kg2lbs = 2.204623;  //same # of decimals for consistency
      std::cout << "Hello, I am Ghadeer !\n";

      int km; 
      std::cout << "Please input a value in km to convert to miles>";
      std::cin >> km; 
      std::cout << km * km2mi; // this is to convert kilometer to miles

      int kg;
      std::cout << "Please input a value in kg to convert to pounds>"; 
      std::cin >> kg;
      std::cout << kg * kg2lbs ; // this is to convert kilogrms to pounds

      std::cout << " Thank you for using this program!";
      return 0;
}


see how you can follow without excess comments? Its clear that
std::cout << kg * kg2lbs ; // this is to convert kilogrms to pounds
you are printing pounts from kilos here, because the variables tell you that, making the comment silly.

Even though I changed a little, I still think you did well for an early program. The changes are just stuff to think about for next time.
Last edited on
Thank you so much!
Actually , this is my first program!
I am fairly new to coding but after reading this... what are the advantages to including std:: rather than just cout and cin?
there are a bunch of posts on it here and on the web, but it boils down to this:

namespace std has gotten too big. Because it is so large, large projects began to have issues where local variables and functions were colliding with standard ones and causing bugs and issues. So the practice went from having the global using statement to not having it, and instead using the namespace.

personally I prefer a small header that pulls in common stuff, eg
using std::cout;
using std::cin;
... etc

because the std:: is visual clutter to me that I strongly dislike.
Its best practice to do one of these 2 ideas, though, and not the 'using namespace std' line, because of accidental name collisions.
I'll look into it some more but thanks for the response! That helps.
Hello JoeyB1901,

The most recent post that is worth reading. "Using namespace std" http://www.cplusplus.com/forum/beginner/258335/

Andy
Topic archived. No new replies allowed.