System pause error

So, I have done a basic calculator program for college, but when you execute the calculation it prints the answer and the program closes. I have tried system("pause") but it still doesn't change anything and I do have iostream and all the #includes needed for it.

This is my code


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
#include "stdafx.h"
#include <iostream>
#include <Windows.h>

using namespace std;

int main()
{
	char op;
	float num1, num2;

	cout << "Please enter operator (+ , - , * or /) ";
	cin >> op;

	cout << "Enter your two numbers you wish to calculate ";
	cin >> num1;
	cin >> num2;


	switch (op)
	{
	case '+':
		cout << num1 + num2;
		break;

	case '-':
		cout << num1 - num2;
		break;

	case '*':
		cout << num1*num2;
		break;


	case '/':
		cout << num1 / num2;
		break;

	default:
		// if the operator is other than specified it will give an error
		cout << "error, invalid operator please try again. ";
		break;


	}
	return 0;

}
Last edited on
It seems you are using Visual Studio so try ctrl+F5 to run it.
Yes I can run it and things but I don't want it to close immediately after it prints the calculation
Hello b487097,

Some quick research showed me that "stdio.h" was the header file used for the "system("pause");".

Your code above does not show the use of "system", so I am wondering if you are missing a header file or if your usage is wrong? A lot of programs I white include "Windows.h" and I have not had any problem using a system call.

Quite often I will include "conio.h" and use this code at the end of the program while working with the program and than remove it when I am finished.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//#define RELEASE  // First line in the file. Uncomment when testing is finished.

#ifndef RELEASE
#include <conio.h>
#endif

// Program here

#ifndef RELEASE
std::cout << "\n\n\n\n Press anykey to continue";
_getch();
#endif

return 0;
}  // End of main 


I use "_getch()" because my compiler complains about "getch()".

A better way to avoid using "system("pause");" would be to wrap lines 12 - 45 in a do/while loop with the while condition as } while (cont); with "cont" defined as bool cont{ true };. Then in the case statements add:

1
2
3
4
case 'q':
case 'Q':
    cont = false;
    break;


Adjust line 12 to add 'q' to quit nd you will not have to worry about a pause at the end of the program.

Hope that helps,

Andy
Some quick research showed me that "stdio.h" was the header file used for the
"system("pause");".

Maybe you should do better research?

http://www.cplusplus.com/reference/cstdlib/system/
Last edited on
@MikeyBoy,

Sorry. I did say it was quick not in-depth I was rushed at the time and did not go as far as I would normally do. Also I have written programs which include "Windows.h" and have had no problem using "system"

I will strive to do better next time.

Andy
Sorry. I did say it was quick not in-depth I was rushed at the time and did not go as far as I would normally do.

I'm kinda curious as to which source you found that told you that stdio.h - a C header file, not a C++ one - was the one you needed for system. Whatever source that was, I'd advise not trusting it in future, and find a more trustworthy source.

I have written programs which include "Windows.h" and have had no problem using "system"

You're aware that header files include other header files, right?
@MikeyBoy,

I do not remember where I did the search from. It was either Google or on this site. Either way I just checked the first link or two that came up. Where ever it was I felt that it was good information. I see now that I need to be more careful in the future.

Yes, I do know that a header file can include others. I actually wrote a header file that includes another that includes another. And before you say it I do know that "Windows.h" is something I should not be using, but it like "conio.h" they work with some of the functions I use,, but they are mostly for my use not everyone else. At least until I can find something better.

Thanks for the input. Well taken,

Andy
No problem. For reference information, cppreference.com is an excellent and reliable source, and the reference section on here is good too.
Topic archived. No new replies allowed.