2

I have a simple program, and it works fine, but the system("CLS"); and system("pause"); statements have red IntelliSense lines underneath them. When I move my cursor over them it says Error "system" is ambiguous. What is causing that?

Here is my code:

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
  int choice = 0;
  const double PI = 3.14159;
  double sideSquare = 0.0;
  double radius = 0.0;
  double base = 0.0;
  double height = 0.0;

  cout << "This program calculates areas of 3 different objects." << endl;
  cout << "1.) Square" << endl;
  cout << "2.) Circle" << endl;
  cout << "3.) Right Triangle" << endl;
  cout << "4.) Terminate Program" << endl << endl;

  cout << "Please [Enter] your object of choice: ";
  cin >> choice;
  system("CLS"); // The problem is here...

  switch(choice)
  {
   case 1: 
    cout << "Please [Enter] the length of the side of the square: ";
    cin >> sideSquare;
    cout << "The area is: " << pow(sideSquare, 2) << endl;
    break;

   case 2: 
    cout << "Please [Enter] the radius of the circle: ";
    cin >> radius;
    cout << "The area is: " << PI * pow(radius, 2) << endl;
    break;

    case 3:
    cout << "Please [Enter] the base of the triangle: ";
    cin >> base;
    cout << endl << "Now [Enter] the height of the triangle: ";
    cin >> height;
    cout << "The area is: " << (base * height) / 2 << endl;
    break;

  default:
    cout << "Please [Enter] a valid selection next time." << endl;
    return 0;
  }
  system("pause"); // ... and here.
  return 0;
}
Rakib
  • 7,435
  • 7
  • 29
  • 45
0000
  • 677
  • 2
  • 8
  • 20
  • 2
    I don't see any red lines. Are you using an IDE that is showing red lines? If so, which IDE? – Greg Hewgill May 28 '14 at 00:20
  • microsoft visual studio 2010 – 0000 May 28 '14 at 00:26
  • 3
    I'm a little surprised it said `system` is ambiguous; it should have said that it's undeclared. Also, consider whether clearing the screen is really necessary. If I'm running your program, I might have existing output on my screen, and you should leave it alone unless you have a very good reason to clear it. – Keith Thompson May 28 '14 at 00:34

1 Answers1

12

You need to #include <cstdlib>

Source: http://en.cppreference.com/w/cpp/utility/program/system

Also, try to avoid system, it's dangerous. To pause the program when it's finished, put a breakpoint on the } at the end of main. There isn't a standard way to clear the screen unfortunately.

For future reference, the red squiggles are intellisense errors, which are shown by a different front-end than the one that actually compiles the code, so the red squiggles are sometimes wrong, especially with complex templates. In most cases, including this one, it's correct though.

Mooing Duck
  • 64,318
  • 19
  • 100
  • 158
  • 3
    Also try `std::system`; it's common for C++ implementations to have C-library functions injected into global namespace as well as `std`, so conceivably the IDE thinks `::system` and `std::system` are different – M.M May 28 '14 at 00:25
  • 2
    Alternatively, console programs will pause automatically when they terminate in Visual Studio if you "start without debugging" (CTRL+F5) as long as you use the `/SUBSYSTEM:CONSOLE` linker option. See [this answer](http://stackoverflow.com/a/1152873/1227469), and [possibly this one](http://stackoverflow.com/a/23288778/1227469). Having the program itself pause is usually the wrong solution, if all you want to do is view the output after termination, since released console programs normally terminate inside an open console, and therefore don't need to pause. – JBentley May 28 '14 at 00:34
  • @JBentley: Wierd, I thought they were subsystem by default, but your links imply otherwise. – Mooing Duck May 28 '14 at 00:36