C++ beginner

I am extremely new to this language and need help with a project. Where exactly do I start when I am asked to read in 2 integers and output both their sum and product. I am also using a linux system and using the terminal in a mac. Thanks!

Last edited on
What have you tried?
#include <iostream>
using namespace std;

int main()
{

This is just the beginning first lines I'm not sure how to proceed.
Last edited on
Do you know how to declare variables?
Hello, do not worry it is actually easy thing to do

#include <iostream>
using namespace std;
int main ()
{
int a, b; (this is called declaration of varables)

cout<<"Enter any 2 integers \n"; (" " will display in the sceen; \n for new line)
cin>>a>>b;
cout<<"the sum of integers you have entered is equal to"<<(a+b)<<endl;
cout<<"the product of the integers you have entered is equal to"<<(a*b)<<endl;

return 0;


}


endl stands for end the line and start the next from new one;
*** some compilers may need to system ("pause")


GOOD LUCK
Last edited on
@jer311 & ratbek

Please read:

http://www.cplusplus.com/forum/beginner/1/
Last edited on
First, please always use code tags. Edit your post, select all the code, then press the <> button on the right under the format menu.

Also, in your IDE make the indenting spaces (4 spaces say) not tabs, it will display better on this page, because tabs get translated to 8 spaces here.

If you do this, your code should look like this:


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
26
27
28
29
30
31
32
33
34
35
36
37
38


#include <iostream>
// other system includes here

// your own header files #included here

using namespace std;  // don't do this

// put function declarations here
// try to list them in the same order as they are called in main()

int main ()
{

// don't  declare more than 1 variable per line
int a, b; // (this is called declaration of varables)
// use meaningful names
// The specification said ints, but
// double gives more flexibility - can do division for example
// you can leave it as ints if you want

double FirstNumber = 0.0; // always initialise variables with something preferably here at declaration
double SecondNumber = 0.0; // Zero might not always be a good choice however

// put spaces around operators - don't jam everything together
// use // for comments

std::cout << "Enter any 2 numbers \n"; // (" " will display in the sceen; \n for new line)
std::cin >> a >> b;
std::cout << "the sum of numbers you have entered is equal to" << (a+b) << std::endl;
std::cout<<"the product of the numbers you have entered is equal to"<<(a*b)<<std::endl;

return 0;
} // end of main

// put function definitions here
// try to list them in the same order as they are called in main() 


Although this might seem trivial now, the code for getting the input, doing the sum, and the product could be in functions of their own. If you added a lot of functions later this makes more sense.

The using namespace std; thing is bad, because it brings in the whole std namespace, polluting the global namespace, which can cause naming conflicts for variables and functions. Did you know that std::distance, std::left and std::right exist in the std namespace? If you had a function or variable with that name and had using namespace std; , then you will have big problems.

I realise you are a brand new beginner, just trying to introduce some good habits early on - and give some ideas for the future. Hope all goes well - and Good Luck !! :+)
Thanks guys its all extremely helpful and it works perfectly! Much thanks!
Topic archived. No new replies allowed.