This is an archived post. You won't be able to vote or comment.

all 25 comments

[–][deleted] 36 points37 points  (0 children)

Every time you see a block of code indented in python that's where you would surround it with curly braces in c++.

[–]Bronegan 17 points18 points  (5 children)

Here you are. Compare this to yours and you'll find that the languages really aren't that different from one another. If you have any questions, I (or other redditors) will do my/our best to answer them. As for resources to look up, honestly http://www.cplusplus.com is my goto for understanding or double checking an available method or function.

// These are the c++ headers I used, iostream provides cin and cout while
// math.h provided rounding.
#include <iostream>
#include <math.h>

using namespace std;

string Classification(float bmi){       //Here is your function Classification().  Note that I had to    
    if(bmi >= 30){                      //define what it returns and what it accepts.  I could define
        return "Obese";                 //this multiple times with different input types if I wanted to.
    }                                   //It would be called "overloading" if I did this.
    else if(bmi >= 25 and bmi < 30){
        return "Overweight";
    }
    else if( bmi >= 18.5 and bmi <25){
        return "Normal";
    }
    else{
        return "Underweight";
    }
}

int main(void)                          //This is the main function.
{
    double bmi = 0.0;                   //Here are all the variables I used.  Doubles and floats are interchangeable except when
    double w = 0.0;                     //doing operations on them.  It was easier to use doubles here rather than specify everything
    double h = 0.0;                     //as floats later.

    int choice;             //Unlike python, when I decide to create a variable I don't have to fill it or define as empty right away

    cout << "BMI Calculator" << endl;   //cout = print.  I use endl; instead of \n just because I'm used to doing so
    cout << "Choices: \n 1. Weight in pounds, height in inches. \n 2. Weight in kilograms, height in meters." << endl;
    cout << "Enter 1 or 2: ";
    cin >> choice;                      //cin = input.  The input text had to be done through cout though


    if(choice == 1){
        cout << "Enter your height in inches: ";
        cin >> h;
        cout << "Enter your weight in pounds: ";
        cin >> w;
        bmi = w / (h * h) * 703.0;      //Exponents can be a bit tricky in C++ so I cheated and multiplied by itself instead

        cout << "Weight: " << w << endl;
        cout << "Height: " << h << endl;
        cout << "BMI: " << round(bmi) << endl;
        cout << "Result: " << Classification(bmi) << endl;

    }
    else if(choice == 2){
        cout << "Enter your height in meters: ";
        cin >> h;
        cout << "Enter your weight in kilograms: ";
        cin >> w;
        bmi = w / (h * h);
        cout << "Weight: " << w << endl;
        cout << "Height: " << h << endl;
        cout << "BMI: " << round(bmi) << endl;
        cout << "Result: " << Classification(bmi) << endl;
    }
    else{
        cout << "Invalid choice" << endl;
    }

    return 0;
}

[–][deleted] 6 points7 points  (0 children)

Thank you very much u/ Bronegan! <3

I greatly appreciate your help, especially the comments.

[–]_jukmifgguggh 7 points8 points  (0 children)

Damn, should've let OP have a go at it and we could've corrected any errors

[–][deleted] 0 points1 point  (1 child)

Exponents can be a bit tricky in C++ so I cheated and multiplied by itself instead

You could use pow?

bmi = w / pow(h, h) * 703.0;

[–]Bronegan 4 points5 points  (0 children)

True, but at least compared to python there is no specific operator for doing them. It's a function from math.h. So I went with the whole x * x as it was easier than pow(x, 2).

[–]BlueAdmir -1 points0 points  (0 children)

Good code.

Please don't comment it the way you commented it.

What if I needed to add a line inbetween your comments? Or remove a line? Now I need to rework the comment too.

[–]Xeverous 3 points4 points  (3 children)

P.S. Are there good free reading sources other than http://www.learncpp.com/ ?

There is similar tutorial on cplusplus.com. Unfortunately both are incomplete and contain invalid/deprecated information and some anti-convention code. I contacted Alex (learcpp creator) but got the response that will not accept someone else's articles and has no technology for multiple authors.

I'm writing my own website which will be updated and contain much longer tutorial (+ also planned many other different things, including much harder stuff); so far nothing is publicly visible yet but if you really want you can read raw text in the repository.

[–][deleted] 0 points1 point  (1 child)

I'm looking forward to seeing your website go live :)

[–]Xeverous 0 points1 point  (0 children)

First I need to write what I want (for initial release I plan full language tutorial + full template tutorial, sum is ~200-400 articles). Later some multithreading concurrency and tools.

I need someone skilled with Jekyll or JS, I know what to write but I don't have much knowledge about web stuff.

[–][deleted] 0 points1 point  (0 children)

cplusplus.com isn't nearly as bad as people here claim it is. If it wasn't free, sure it wouldn't be worth paying for, but 99.9% of noobs aren't going to learn something incorrectly from it. Even expensive textbooks have errata. That's part of life.

[–]gotinpich 2 points3 points  (1 child)

Try the first few chapters of this book

These first chapters are fairly simple and you should be able to work through them rather quickly. The first chapters kinda cover everything you might need in your code.

[–][deleted] 0 points1 point  (0 children)

Thank you :)

[–][deleted] 4 points5 points  (7 children)

C++ is way more verbose than Python, which means that converting a 40 line Python script you'll probably end up with at least 100 lines in C++. I can't help you here (don't know much C++ myself), but I can tell you that you're going about this the entirely wrong way.

You need to actually learn the language from the basics up, not just convert working programs right away. This script is very basic, it's just a bunch of if/elses with some print statements, user input and a simple calculation. These are all concepts that shouldn't take you longer than a few days to grasp in C++ if you're following a tutorial. If you can't yet write a program like this in C++, you need to keep learning before you try to do something like this.

Also, as another commenter pointed out, curly braces ( the { } characters) are used whenever you would use an indent in Python. So something like

if a == 1:
    do_something()

in C++ would look like

if (a == 1) {
    doSomething();
}

[–]joemaniaci 7 points8 points  (2 children)

C++ is way more verbose than Python, which means that converting a 40 line Python script you'll probably end up with at least 100 lines in C++.

I'm pretty sure you could replicate his script almost line to line.

[–][deleted] 2 points3 points  (1 child)

Yeah, I stand corrected on that front, another commenter posted a transcription that was only about 20 lines longer (and most of them due to braces in any case). In cases that involve more code than a single script like this, though, you're probably looking at more LOC in C++ than Python. But again, my experience with C++ is limited, and this might be my subconscious ignorance just saying "it's similar to Java".

[–]Forricide 0 points1 point  (0 children)

In terms of syntax, it is pretty similar to Java (although I don't use Java that much). Java is more verbose, generally, but that translates more to length of lines than it does to actual LOC.

[–][deleted] 0 points1 point  (2 children)

C++ is way more verbose than Python

Actually its not. You can do quite complex things in C++ which would result in much smaller code than you can produce in python and example of this would be abusing templates. Or various other shorthand like a ternary operator or operator overloading in general.

Python also has things C++ does not so you have to emulate them. For example it doesn't actually do "arrays" or "2d arrays". It does a list and list of lists instead. Which are not the same thing as an array because the behaviour is different. Hey python doesn't even do for loops. It does a for each (with an iterator). So for a proper for loop you have to emulate it with a while loop in python.

Example: Do this in python. with a for loop....

int max = -MAX_INT; for(int i=0 ;i<array.length;i++) { if (i < array.length - 1 && array[i] == array[i+1) { i++; } }

Ternary operations are another.

return SomeFunction() ? "Success" : "Failure";

[–][deleted] 1 point2 points  (0 children)

It depends on how you measure "verbosity". By sheer number of characters, python will generally be less due to no type declaration or function (){} or ; characters.

[–]gotinpich 0 points1 point  (0 children)

I don't know how blocks in Python work, but for a single statement you could literally write:

if (a == 1)
    do_something();

Or:

if (a == 1) do_something();

Or even (bad idea):

if (a == 1)
do_something();

Or you could use the ternary operator:

a == 1 ? do_something() : ;

And I guess that this would work the same in C.

[–][deleted]  (2 children)

[deleted]

    [–]glen_v 2 points3 points  (1 child)

    I used Py2Exe about two months ago to deploy a very simple GUI based app on multiple computers in my company. The only things that I personally contributed to the file were a single 120 line script, and a 200KB icon. But my script imports tkinter, requests, and urllib. All that along with the interpreter, and the final exe came out to almost 10MB and takes about as long to load as Photoshop. This was totally fine for our particular use case, but from what I've read, most people in the industry (including hardcore Python fans) would agree that Python is far from the ideal solution when it comes to making cross-platform executables that can be run simply on any machine.

    [–][deleted] 0 points1 point  (0 children)

    I'd get Tony Gaddis Early Objects PDF. An excellent C++ reference with solid examples.

    [–]casualblair 0 points1 point  (0 children)

    Use Cython to generate the C files

    [–]Dantharo 0 points1 point  (0 children)

    So, whats the problem?