Hello Hary swe,
PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.
Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.
You can use the preview button at the bottom to see how it looks.
I found the second link to be the most help.
|
Your code that I changed because line numbers are different:
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 39 40 41 42 43 44 45 46 47
|
#include <iostream>
#include <limits> // <--- Added.
using namespace std; // <--- Changed. Moved here.
int main() // <--- Changed. "main" returns an "int".
{
double nr1, nr2; //skapar variabler, alltså tal 1 och tal 2//
cout << "write two numbers\n\n";
//Här får användaren skriva in sina siffror//
while (std::cout << "Enter first number: " && !(std::cin >> nr1))
{
std::cout << "\n Invalid Input! Must be a number.\n\n";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
}
std::cout << "Enter second number: ";
cin >> nr2;
//Själva uträkningarna//
double summa = nr1 + nr2;
cout << "\nAddition\n" << nr1 << " + " << nr2 << " = " << summa << "\n\n"; // <--- Changed.
double diff = nr1 - nr2;
cout << "Subtraktion" << endl << nr1 << "- " << nr2 << " =" << diff << "\n\n";
double produkt = nr1 * nr2;
cout << "Multiplikation" << endl << nr1 << "* " << nr2 << " =" << produkt << "\n\n";
double kvot = nr1 / nr2;
if (nr2 == 0) // Om siffra 2 är noll går det ju inte att dividera//
cout << "Division" << endl << "går inte att dividera med noll, starta om och skriv in en annan siffra";
// så därför får användaren starta om igen//
//Om det går att dividera så skrivs denna rad ut//
else
cout << "Division" << endl << nr1 << "/ " << nr2 << " =" << kvot << "\n\n";
//cout << endl << "press Enter to exit"; // <--- Kind of a duplicata as "system("pause")" has its own message.
std::cout << "\n\n";
system("pause"); // <--- Changed.
}
| |
Putting line 4 asa the first line may work, but it is not needed to cover the header files,. Just the code that follows.
As mentioned "main" returns an "int". You can return some different, but it will be converted to an "int".
These days a "double" is the preferred floating point type because it has more precision than a "float.
Starting at line 13 this is one option you could use. If you look at the while condition and loop you will come to understand it. If not let me know.
You will need to do the same for "nr2".
On line 26 I added a couple of spaces, which you will need to do for the others.
Starting in either the 2011 or 2014 standards the "\n", (new line character), has replaced the "endl". Also a "cout" followed by "cin" will flush the output buffer before the "cin" takes any input. This can be used to your advantage.
For line 36 you could change this to a while loop and allow the user to enter something greater than (0.0) before the division.
Also when you write
if (nr2 == 0)
my IDE considers the (0) to be an "int" Although it will work it is a type mismatch comparing a "double" to an "int".
Line 44 the comment should be enough explanation.
It is best to avoid using system anything in your program. This could make the program vulnerable to attack.
I have found this to be a good replacement for
system("pause");
.
1 2 3 4 5
|
// A fair C++ replacement for "system("pause")". Or a way to pause the program.
// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue: ";
std::cin.get();
| |
Andy