Python vs C++

I am wanting to know how to take 2 values and returns a string that says "the sum of (value1) and (value2) is (value1+value2). I am trying to work the program out in both languages but just can't seem to find the right code. I am a beginner so any help and or advice is greatly appreciated. Also having the code in both python and c++ would be great! Thanks.
In C++ you could write:
1
2
3
4
5
6
7
8
9
10
#include <iostream>
using namespace std;

int value = 1;
int value2 = 2;

int main() {
    cout << "The sum of " << value << " and " << value2 << " is: ";
    cout << value+value2;
}


I don't remember how to do it in Python but it's probably simpler.
Last edited on
I'm a beginner, myself, and I know nothing at all about Python, but let me take a stab at the C++ part of your question.

First, have the user choose a number for the first value. Something like:

1
2
3
std::cout << "Please enter your first number: ";
double value1;
std::cin >> value1;


Then, do something more than a little similar for the second number.

And follow it up with something along the lines of:

 
std::cout << "The sum of " << value1 << " and " << value2 << " is " << value1 + value2;


Seems pretty simple and straight forward to me. Maybe not "Hello, world!" simple, but not many pages in the textbook beyond that. Or do I misunderstand your question?
Python is quite a bit simpler:

1
2
3
4
a=5
b=6
c = "The sum of " + str(a) + " and " + str(b) + " is " + str(a+b)
print(c)


c++ is also relatively simple

1
2
3
4
5
auto a = 5;
auto b = 6;
stringstream c;
c << "The sum of " << a << " and " << b << " is " << a+b;
cout << c.str();


If you're starting out you may want to pick one and stick with it for a while, but don't be afraid of switching. Languages are just like human languages, they all express ideas in generally the same way and attempt to accomplish the same goals, the methods are just a bit different.
Thanks!
Topic archived. No new replies allowed.