I very recently learned how to program at all, and I'm starting in c++.
I started with a helloworld type program, and it worked fine.
I made a program that repeats what you input, and that worked fine as well.
I made a program that is a basic calculator. It worked fine.
I did all this using Borland 5.5
I found a much better compiler, MinGW
I uninstall borland, delete my .exes and all the files except for the .cpps, and I compile them all again using MinGW.
The helloworld and repeater worked fine, exactly as before.
The problem is that the calculator closes immediately after the calculation (the final output)
I compared the repeater:
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
|
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
char word[80];
cout << "Type a line of text.\n";
cin.get(word, 79);
cout << "Did you type:\n";
cout << word << "?" << endl;
cin.ignore();
cout << "Press enter to end program.";
cin.ignore();
return 0;
}
|
With the calculator:
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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
//float is used for numbers with possible decimals, int is used for any number
float num1;
float num2;
int num3;
float num4;
cout << "Enter a number." << endl;
cin >> num1;
cout << "Enter a second number." << endl;
cin >> num2;
cout << "Choose an operation" << endl;
cout << "1 = add, 2 = subtract, 3 = multiply, 4 = divide" << endl;
cin >> num3;
if (num3 > 4 || num3 < 1)
{
cout << "You have entered an invalid operation." << endl;
cout << "Press enter to end the program. If you would like to try again, follow the instructions next time!";
cin.ignore();
return 0;
}
else
{
if (num3 == 1)
{
num4 = num1 + num2;
cout << "Your answer is: " << num4 << endl;
}
else if (num3 == 2)
{
num4 = num1 - num2;
cout << "Your answer is: " << num4 << endl;
}
else if (num3 == 3)
{
num4 = num1 * num2;
cout << "Your answer is: " << num4 << endl;
}
else if (num3 == 4)
{
num4 = num1 / num2;
cout << "Your answer is: " << num4 << endl;
}
}
cout << "Press enter to end the program.";
cin.ignore();
return 0;
}
|
And I can't seem to find the problem.
In place of the "cin.ignore();" in both of them I had "getchar();" but I tried changing it. Still, same problem.
I know the problem lies in that, which is essentially a pause before the end of the program.
I have searched the forums and google and other programming sites, and I haven't found anything that worked.
I just want a simple pause script that will let me see the output before the prompt closes.
I've been searching for two hours now, any help is greatly appreciated.