Translate

Storing Data For C++ Variables

If all you can do is print a message, C++ would not be useful. The point is usually to get new data from somewhere such as end-user input and then do something interesting with it. The operation requires a variable, this is a location where you can put the data.
You can think of variables as a magic box that holds the values​​. As a result of the program, can read, write, or modify the values ​​as needed. Next example uses a variable named ctemp and ftemp to hold Celsius and Fahrenheit values​​, respectively. How value can be put into a variable?

One way is through the console input. In C++, you can enter values ​​using cin object, representing the (quite rightly) console input. With cin, you use operator display data stream flowing to the right (>>)

This is what happens in response to this statement. (The process is actually a bit more complicated, but do not worry about that for now.)


  1. Program delaying and waiting for the user to enter the data.
  2. The user types a number and press ENTER.
  3. The number is accepted and placed in ctemp variable (in this case).
  4. The continued processing.


So, if you think about it, a lot happens in response to this statement:
cin >> ctemp;

But before you can use a variable in C++, you must declare. This is an absolute rule, and that makes C++ is different from Basic, careless in this regard and does not require a declaration. (But generations Basic programmers have hit their heads against their terminals when they find mistakes cropping as a result of the negligence of the variable base) is important enough to justify repetition, so I'll make the cardinal rule:
In C++, you must declare a variable before using it.

To declare a variable, you must first know what type of data to be used. This is an important concept in C++ as in most other languages​​.

About Data Types

Thus, the variable is something that you can think of as a magic box where you can put the information-or, rather, the data. But what kind of data? All the data on the computer numerical eventually, but is set to be one of three basic formats: integer, floating-point, and text strings. There are some differences between the formats floating-point and integer. But the main rule is simple:
If you need to store numbers with fractional portions, using floating-point variables, if not, use an integer.

The main floating-point data type in C + + is a double. This may seem like an odd name, it stands There is also a type-precision (float), but its use is rare "double-precision floating point". When you need the ability to maintain a fractional portion, you will get better results and fewer error messages if you keep doubling. A double declaration has the following syntax. Notice that the statement ends with a semicolon (;), like most types of reports.

double variable_name;
You can also use a double declaration to create a series of variables:
double variable_name1, variable_name2, ...;
For example, this statement creates a double variable named aFloat:
double aFloat;
This statement creates a variable of type double.

The next statement declares four double variables named b, c, d, and amount:
double b, c, d, amount;

The effect of this statement is equivalent to the following:
double b;
double c;
double d;
double amount;
The result of these declarations is to create four variables of type double.

Every time I go to Canada, I had to convert Celsius to Fahrenheit temperature in my head. If I had a handheld computer, it would be nice to tell it to do this conversion for me, computers are good at that sort of thing. The following conversion formula. Asterisk (*), when used to combine two values​​, means "multiply."

Fahrenheit = (Celsius * 1.8) + 32


Now, a useful program will take any value input for Celsius and then convert
it. This requires the use of some new features:
◗ Getting user input
◗ Storing the value input in a variable
Here is the complete program. Open a new source file, enter the code, and
save it as convert.cpp. Then compile and run.


  1. #include <iostream>
  2. using namespace std;
  3. int main() {
  4. double ctemp, ftemp;
  5. cout << "Input Celsius temp: ";
  6. cin >> ctemp;
  7. ftemp = (ctemp * 1.8) + 32;
  8. cout << "Fahrenheit temp is: " << ftemp;
  9. system("PAUSE");
  10. return 0;
  11. }


The program is easier to follow when you add a comment-that the C + + are denoted by double slashes (/ /). Comments are ignored by the compiler (they did not have any effect on the behavior of the program), but they are useful to humans. Here is a commented version:


  1. #include <iostream>
  2. using namespace std;
  3. int main() {
  4. double ctemp; // Celsius temperature
  5. double ftemp; // Fahrenheit temperature
  6. // Get value of ctemp (Celsius temp).
  7. cout << "Input Celsius temp: ";
  8. cin >> ctemp;
  9. // Calculate ftemp (Fahrenheit temp) and output.
  10. ftemp = (ctemp * 1.8) + 32;
  11. cout << "Fahrenheit temp is: " << ftemp << endl;
  12. return 0;
  13. }


It commented version, although it is easier for humans to read, needs more work to get into. While following the examples in this book, you can always omit comments or choose to add them later. Remember this cardinal rule for comments:
C++ code beginning with double slashes (//) is a comment and is ignored by the
C++ compiler to the end of the line.

Now let's try for another example:

1. Write a program that asks the user to enter two numbers, obtain two numbers from the user and print the sum, product, difference, and intelligence of the two numbers. The result will be like as follow:


Answer:


  1. #include <iostream>

  2. using namespace std;

  3. int main()
  4.  {
  5.  int num1, num2; // declare variables

  6.  cout << "Enter two of integers: "<<endl; // prompt user
  7.  cin >> num1 >> num2; // read values from keyboard

  8.  // output the results
  9.  cout << "The sum is " << num1 + num2
  10.  << "\nThe product is " << num1 * num2
  11.  << "\nThe difference is " << num1 - num2
  12.  << "\nThe quotient is " << num1 / num2 << endl;


  13.  return 0; // indicate successful termination
  14.  }




2. Write a program that print the numbers 1 to 4 on the same line with each pair of adjacent numbers separated by one space.
Write the program using the following methods:
a) Using one output statement with one stream insertion operator.
b) Using one output statement with four stream insertion operators.
c) Using four output statements.

The result will as this follow:


Answer:
  1. #include <iostream>

  2. using namespace std;

  3. int main ()
  4.  {
  5.  // Part A
  6.  cout << "1 2 3 4\n";

  7.  // Part B
  8.  cout << "1 " << "2 " << "3 " << "4\n";

  9.  // Part C
  10.  cout << "1 ";
  11.  cout << "2 ";
  12.  cout << "3 ";
  13.  cout << "4" << endl;

  14.  return 0;
  15. }

No comments:

Post a Comment