Sams teach yourself C++ 7th edition

hey everyone, i recently bought the book sams teach yourself C++ and i seem to be having some problems with it. for example this code should output 2 48 14 but instead it outputs 2 4814. (i use microsoft visual c++ 2010 express. also the book doesnt explain why i use std so id like to know that as well.

#include <iostream>
int main()
{
int x = 8;
int y = 6;
std::cout << std::endl
std::cout << x - y << " " << x * y << x + y;
std::cout << std::endl;
return 0;
}
Look closely at the following line and you will find your answer:

std::cout << x - y << " " << x * y << x + y;
#include <iostream>

int main()
{
std::cout << "Hello World";
system("pause");
return 0;

}

#include <iostream>

using namespace std;

int main()
{
cout << "Hello World";
system("pause");
return 0;

}

2 4814 = because you dont have " " after x*y

std::cout << x - y << " " << x * y << x + y
Last edited on
thanks i guess this book isnt very good it doesnt tell me to do that in the program O_o
Okay, the reason it is messed up is because you need to do
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

int main()
{
      int x = 8;
      int y = 6;
      std::cout << std::endl
      std::cout << x - y << " " << x * y << " " << x + y;
      std::cout << std::endl;
      return 0;
}


The std:: just tells the compiler that you are using the cout object in the standard (std) namespace. Everything in the C++ standard library is in the std namespace. A way around it is to put one of two things.
Either using namespace std; under the #include preprocessor directives or call each thing you are using out of the namespace by doing :

1
2
3
using std::cin;      using std::cout;
using std::endl;    using std::vector;
// etc. 


tim039 wrote:
thanks i guess this book isnt very good it doesnt tell me to do that in the program O_o

Yeah. Any Sams Teach Yourself books are frowned upon. You should get Accelerated C++ or just read the tutorials from this site http://www.cplusplus.com/doc/tutorial/ (has a PDF format).
Last edited on by closed account z6A9GNh0
Topic archived. No new replies allowed.