Good morning all,

So i have started teaching myself C++ in preparation for taking software engineering at university next year, after many hours i have managed to put a simple program together, Im just asking if someone can tell me on in the right track to understanding what each function does if i break it down,

#include <iostream>
using namespace std;

So the iostream allows for keyboard input and the namespace is where code is stored?

cout << "Enter a real number: ";
	cin >> x;

	cout << "Enter a real number: ";
	cin >> y;

So the user is inputting a number to replace the x and the y with a value,

float x = 0.0;
float y = 0.0;

This is setting the integer value to 0, allowing the user to input a number to replace the 0, and i am using float because of the decimal point

float a = x;
	float b = x;
	float c = x;
	float d = x;
	float e = x;

This is storing the input from the console window? not 100% sure on what its doing...

a += y;
	b -= y;
	c *= y;
	d /= y;

And i have no idea what this section does...

cout << "x += y " << a << endl;
	cout << "x -= y " << b << endl;
	cout << "x *= y " << c << endl;
	cout << "x /= y " << d << endl;
	cout << "x %= y " << e << endl;

And this just outputs the information into the console window, Im pretty down with console output ;)

As i said above, just looking for clarification that i have my mind on the right sort of track and am not embedding wrong information into my head.

Thanks heaps in advance

-Privoxy

Edit:

The full code in view

#include <iostream>
using namespace std;

int main()

{
	float x = 0.0;
	float y = 0.0;

	cout << "Enter a real number: ";
	cin >> x;

	cout << "Enter a real number: ";
	cin >> y;

	float a = x;
	float b = x;
	float c = x;
	float d = x;
	float e = x;
	
	a += y;
	b -= y;
	c *= y;
	d /= y;

	cout << "x += y " << a << endl;
	cout << "x -= y " << b << endl;
	cout << "x *= y " << c << endl;
	cout << "x /= y " << d << endl;
	cout << "x %= y " << e << endl;

}

Edited 7 Years Ago by Privoxy: n/a

Welcome to the site. See comments in red below.

#include <iostream>
using namespace std;

So the iostream allows for keyboard input and the namespace is where code is stored?

cout << "Enter a real number: ";
	cin >> x;

	cout << "Enter a real number: ";
	cin >> y;

So the user is inputting a number to replace the x and the y with a value,

// it's not really replacing x and y, it's storing it in memory locations represented by the variable names x and y

float x = 0.0;
float y = 0.0;

This is setting the integer value to 0, allowing the user to input a number to replace the 0, and i am using float because of the decimal point

// integers are whole numbers. this is declaring a variable (allocating memory at a certain location) which is represented by the variable name y and putting the value 0.0 in that memory location

float a = x;
	float b = x;
	float c = x;
	float d = x;
	float e = x;

This is storing the input from the console window? not 100% sure on what its doing...

// this declares more variables (named a, b, c, d, e), allocates their memory and stores the value held in the memory location represented by the variable name x in their location as well.

a += y;
	b -= y;
	c *= y;
	d /= y;

And i have no idea what this section does...

// these lines say: take the value stored in the variable a, add the value stored in y to it and store the result in the variable a. The same goes for the subtract, multiplication and divide operators with their respective variables used for the operators.

cout << "x += y " << a << endl;
	cout << "x -= y " << b << endl;
	cout << "x *= y " << c << endl;
	cout << "x /= y " << d << endl;
	cout << "x %= y " << e << endl;

And this just outputs the information into the console window, Im pretty down with console output ;)

Edited 7 Years Ago by donaldw: n/a

Oh that makes more scene, Thank you for taking the time to explain it to me,

Highly appreciate it

No problem. You know there's a system in place on the board to show your appreciation, right :D

>So the iostream allows for keyboard input
At the risk of giving you too much information, there are several stages that your code goes through from source to executable. Here are the stages (bird's eye view):

  1. Source file: This is the stuff you write, exactly as you write it.
  2. Translation unit: This is the result of running the preprocessor to completion (merging headers into the source file, expanding macros, etc...). The preprocessor handles this step, obviously.
  3. Object file: Processing the translation unit produces machine code that isn't necessarily executable, but can be combined with other object files to create an executable. The compiler handles this step.
  4. Executable file: One or more object files are linked together such that external references (one translation unit using stuff from another) are resolved. The linker handles this step.

<iostream> is what C++ calls a header (not a header file, because headers need not be stored on the file system). Headers are used to supply declarations for code that's defined in a separate object file. These declarations are necessary for the compilation step to complete successfully. Without them, the compiler will complain that you're using unknown names and fail long before you get to the linker.

The <iostream> header contains declarations for the standard stream objects you'll be using at first (cin, cout, cerr, clog), and their wide counterparts (wcin, wcout, wcerr, wclog). Those objects are the interface to the standard streams. Note that I didn't mention a keyboard yet. cin and wcin work with the standard input stream by default, which often points to the standard console, which interfaces with the keyboard driver, which gets keypresses from the keyboard. But cin does not read from the keyboard, it reads from the standard input stream. Granted it's a subtle difference, but an important one if you want to avoid being confused when you see cin being redirected to a file.

>and the namespace is where code is stored?
I dislike how books and tutorials start you off with a using directive, because it hides what's actually happening and creates confusion.

All of the names in the standard library are in a namespace called "std" (as in standard, not gonorrhea). Namespaces are a way of categorizing and separating names such that they don't get jumbled and confused. Think of it like having two boxes: your stuff and my stuff. We both might have exactly the same stuff, but we can tell which is your stuff and which is my stuff because it's in separate boxes. Namespaces work the same way with names:

#include <iostream>

namespace my_stuff {
  int xbox;
  int pc;
  int soda;
}

namespace your_stuff {
  int xbox;
  int pc;
  int soda;
}

int main()
{
  if (my_stuff::xbox > your_stuff::xbox)
    std::cout<<"w00t!\n";
}

All of that only makes sense if you understand that the names are extended to include the namespace. There isn't anything in the above program called xbox , just as there isn't any stuff that belongs to either you or me not in one of the boxes. So to get your xbox, you look in the box holding your stuff and search for an xbox. Likewise, to get the xbox name from the your_stuff namespace, first you look in the namespace, then search for the name. The way this is resolved is with the double colon: <namespace>::<name> , or your_stuff::xbox .

The directive using namespace std is a convenience feature that saves you the trouble of always typing the namespace and the double colon. It basically says "automatically look in this namespace if you don't find the name". Here's an example where your_stuff is opened up like that, but my_stuff isn't:

#include <iostream>

namespace my_stuff {
  int xbox;
  int pc;
  int soda;
}

namespace your_stuff {
  int xbox;
  int pc;
  int soda;
}

int main()
{
  using namespace your_stuff;

  if (my_stuff::xbox > xbox)
    std::cout<<"w00t!\n";
}

Now an unadorned xbox is assumed to be from the your_stuff namespace.

>So the user is inputting a number to replace the x and the y with a value
x and y are storage locations for a value. It would be more correct to say that the values stored by x and y are being overwritten by the values being read from the standard input stream.

>This is setting the integer value to 0, allowing the user to input a
>number to replace the 0, and i am using float because of the decimal point

There's no integer involved. You're initializing a variable of type double to the literal value 0.0, which also has a type of double.

>This is storing the input from the console window? not 100% sure on what its doing...
You wrote it, you should understand what it's doing. Anyway, you're initializing a, b, c, d, and e (variables of type double), with a copy of the value stored in x.

>And i have no idea what this section does...
Those can be viewed as compound operators. a += b is equivalent to a = a + b . Using the latter expression as a guide, we can say that the value of b is added to the value of a, and the result of that addition overwrites the old value of a. +, -, *, and / are the basic arithmetic operators for addition, subtraction, multiplication, and division, respectively. As long as you know what the assignment operator does as well as those, the "compound" operators are easy to decipher.

>And this just outputs the information into the console
>window, Im pretty down with console output

Close enough. Like cin, cout may default to the console window, but it's more accurate to say that it points to the standard output stream and the standard output stream happens to go to a console window.

This article has been dead for over six months. Start a new discussion instead.