You are here:

C++/compilation error


There is an error when compiling:

I've run the program and it points to system("pause"); and the error is "expected constructor, destructor, or type conversion before '(' token". What's wrong this time?

#include <ctype.h>
#include <iostream>
#include <string>
using namespace std;

int main()
  string gender ;

  // accept string from user
  std::cin >> gender ;

  // first convert all chars to lower case
  for( size_t i=0 ; i < gender.size() ; ++i )
      gender[i] = tolower( gender[i] ) ;

  bool ok = true ;

  // an empty string is not ok
  if( gender.empty() ) ok = false ;

  else if( gender.size() == 1U )
      // a single char was entered, must be either 'f' or 'm'
      if( ( gender[0] != 'm' ) && ( gender[0] != 'f' ) ) ok = false ;

  // more than one char entered, must be either "male" or "female"
  else if( ( gender != "male" ) && ( gender != "female" ) ) ok = false ;

  if( !ok )
  cout << "ERROR! gender is incorrect\n" ;

   return EXIT_SUCCESS;

the system() function is declared in <cstdlib> or <stdlib.h> in C.
you need to #include the header.

system("pause") represents a vulnerability (an unsafe programming practice) which can be exploited by an attacker. a malicious attacker could replace the "pause" program on the machine with a program that does some kind of damage, and use it to cause trouble. spoofing a legitimate and harmless program is a common practice among malware.

in addition, it also has the drawback that the argument string is not portable across different implementations. your code may work on windows, but may do something completely different (or not work at all)  on unix.

a portable and less risky way is to write:

   std::cin >> std::ws ; // throw away white spaces remaining in the input buffer
   std::cin.get() ; // wait for the user to enter something  


All Answers

Answers by Expert:

Ask Experts




my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++11. i would not answer questions about gui and web programming.


about 15 years or so

post graduate engineer

©2016 All rights reserved.