Needing to take variables and do simple math

So far this is what i have. I have to take a persons name and age in earth years and convert it to Martian years.

These are the Requirements the code must meet----

Your program must also meet these requirements:
Asks the user for their name and displays it back out in a welcome message. Must use getline() function to accommodate for spaces in the name.
Include comments with your name and date
Uses comments to explain the code
Uses a constant value for the Martian year of 1.88
The Martian result is displayed out to exactly 2 decimal places.
All variables and constants must be defined and initialized at the beginning of the main() function.
Be sure to check your math with various test data. For example:
20 years = 10.64 Martian years
22 years = 11.70 Martian years
30 years = 15.96 Martian years



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  #include <cstdlib>
#include <iostream>
# include <string>

using namespace std;

int main(int argc, char *argv[])
{
    cout << "Calculate Your Martian Age!" << endl;
    
    cout << "Enter your name to get startred." << endl;
    string FirstLine;
    getline(cin, FirstLine);
    
    cout << "How old are you in Earth Years?" << endl; 
    string SecLine;
    getline(cin, SecLine);
    
    system("PAUSE");
    return EXIT_SUCCESS;
you should in put your age as an integer not a string. Then you can do math.
How can i do that. I hate sounding stupid but its just hard to grasp all of this.
There are several different data types.

You are using std::string for both of your inputs.

For your name(line 12-13) it is perfectly fine. But when you are dealing with integers you should input integers. So you should change line 16 to int SecLine;. Though I would suggest you use descriptive variable names instead of FirstLine and SecLine. Maybe try name and age?

Here is how you would change it to an int

1
2
3
int age;
cin >> age; //input the integer
//std::getline is only used for strings 


You can now do math with the integer

try this

 
cout << age * 2 << endl;


That will output your age times 2

to reassign a value to your age try the = operator.

There are also compound operators += , *= , /= , -= , %=
Say for example we have a += 2; This is the same as a = a + 2;
Compound operators add to the existing value.

http://www.cplusplus.com/doc/tutorial/operators/
http://www.learncpp.com/ --Check out chapter 2 and 3
Thank you so much.
Topic archived. No new replies allowed.