exit(1) not working on unix

closed account (S3TkoG1T)
Hello!

So I am trying to use exit(1) but every time I compile it through g++ I get an error that exit(1) "wasn't declared in the scope". Help me understand why, because I included, #include <cstdlib> for exit(1) to go through & be declared, for the purpose of terminating the program. This is all done on unix using SSH on my MAC.

Here's what I am working with:


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

  1 #include <iostream>
  2 #include <fstream>
  3 #include <cstdlib>
  4 #include <cstring>
  5 using namespace std;
  6 
  7 
  8 int main () {
  9         
 10         ifstream infile("input.txt");
 11         if (!infile) {
 12                 cerr << "Error opening input.txt" << endl;
 13                 exit (1);
 14         }
 15         
 16         int numItems;
 17         infile >> numItems;
 18         
 19         for(int i = 0; i < numItems; i++) {
 20                 int num;
 21                 infile >> num; 
 22                 cout << num << endl;
 23         
 24         }
 25         
 26         
 27         infile.close();
 28 
 29 
 30 }
Try including <stdlib.h> instead of <cstdlib>. Maybe this helps.
closed account (S3TkoG1T)
it did!! THANK YOU SOOOOO MUCH!
You need to #include <cstdlib> and use std::exit because it's in the std namespace.
@L B:He did (see line 5).
I know this problem. I'm developing software for lots of C++/Posix systems since about 25 years. And sometimes (often ?) those <cxyz> headers representing former <xyz.h> headers don't work properly. As a result I decided to avoid using <cxyz> headers.
Ah, yeah. In MSVC they're not even in the std namespace despite having code as if that were intended. At least clang++ and MinGW are standard-conforming, which is what I use.
Topic archived. No new replies allowed.