I keep getting an error message when running my code at the end. I think it has something to do with line 26, but I'm not sure how to correct it. the error is
sh: PAUSE: command not found

My code is attached.

Thanks for help in advance.

#include<iostream> 
#include<cmath>

using namespace std;

int CheckWeight(int weight, string Planet);

int main()
{
	int weight,res;
	string planetName;
	
	do{
		cout << "Please enter your weight of your object to the nearest whole number." <<endl;
		cin >> weight;}
	while(cin.good()==false);
	cout << " " << endl;
	do{ 
		cout << "Please enter a planet name from the following list." << endl;
		cout << "Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune" << endl;
		cin >> planetName;
		cout << " " << endl;
		// Call function to check weight
		res = CheckWeight(weight, planetName);
	}while(res==1);
	system("PAUSE");
	return 0;
}// end main()

// Function for calculating weight

int CheckWeight(int weight, string Planet)
{
	int Result=0;
	if(Planet== "Mercury"){
		cout << "Your weight on Mercury is " << (weight*0.4155)<< endl;
	}
	else if(Planet== "Venus"){
		cout << "Your weight on Venus is " << (weight*0.8975)<< endl;
	}
	else if(Planet== "Earth"){
		cout << "Your weight on Earth is " << (weight*1.0)<< endl;
	}
	else if(Planet== "Mars"){
		cout << "Your weight on Mars is " << (weight*0.3507)<< endl;
	}
	else if(Planet== "Jupiter"){
		cout << "Your weight on Jupiter is " << (weight*2.5374)<< endl;
	}
	else if(Planet== "Saturn"){
		cout << "Your weight on Saturn is " << (weight*1.0677)<< endl;
	}
	else if(Planet== "Uranus"){
		cout << "Your weight on Uranus is " << (weight*0.8947)<< endl;
	}
	else if(Planet== "Neptune"){
		cout << "Your weight on Neptune is " << (weight*1.1794)<< endl;
	}
	else{
		cout << "You entered a wrong planet name. Please try again " << endl;
		cout << "Please try again " << endl;
		Result=1;
	}
	return Result;

system("PAUSE");

This instructs your programme to ask the shell it is running inside to execute the command PAUSE, just as if you had typed PAUSE into a command line yourself. Your shell has no such command, and hence returns as error message. As I recall, this command commonly exists in various Windows shells, but is not present in bash, csh, tcsh, fish and a number of other shells that are commonly used in *nix operating systems.

The solution is to step away from this hideous OS-dependent hack and do something actually within your programme if you want to pause the programme.

Edited 5 Years Ago by Moschops: n/a

You have to include windows.h, then system("pause") or system("PAUSE") will work fine. Here is a simple code I wrote to display the fact:

#include <windows.h>
int main()
{
    system("PAUSE");
    return 0;
}

Also although it is generally best to avoid OS specificity and keep C++ platform independant, sometimes it is more powerful to just use the system. (for example the horrible [in my opinion] language of VB is highly OS dependant on windows, yet it is an extremely powerful language that allows you to move windows through each other, and do hardware specific tasks. Though I still recommend using a platform independant method, or at least using a header check like #ifdef _WIN32 which checks if the system is windows 32 bit.

Edited 5 Years Ago by Labdabeta: Footnote

The system function actually lives in cstdlib; the problem is not that he doesn't include system, as if that were the case, his code would simply not compile. The problem is not that system is unrecognised by the compiler; the problem is that his shell does not recognise the command PAUSE.

Also although it is generally best to avoid OS specificity and keep C++ platform independant, sometimes it is more powerful to just use the system. (for example the horrible [in my opinion] language of VB is highly OS dependant on windows, yet it is an extremely powerful language that allows you to move windows through each other, and do hardware specific tasks.

Excuse me? VB isn't "highly OS dependant on windows," is was written completely dependent on Windows. Can you imagine having to write all the code necessary to create a form or a button from scratch so VB can be OS independent? Let alone the interrupt structure necessary to run the components! Very bad illustration.


@determine: See this

Edited 5 Years Ago by WaltP: n/a

I understand WaltP, VB is nearly completely for Windows (I actually wrote that, but changed it when I remembered that a friend of mine was able to get a simple program to work on his Linux computer) I should probably have used a better example, I am sorry *looks down in embarrassment*
Anyways, I was taught pseudocode a while ago for language and platform independant pauses, this syntax being:

Create a variable and set it to a value that is hard to enter (I usually use '\a' the alarm value)
While that variable is that value, ask the user to input that variable with no prompt.

The good thing about this is that it works even if input times out (like if you are using SDL_Event and SDL_PollEvent instead of SDL_WaitEvent)

Basic point is, the website that WaltP linked to seems to be good as far as I can tell, and if you need to pause in another circumstance or language than try a looping input.

>>You have to include windows.h,

No you don't. including windows.h will make a simple console program carray around a lot of useless and unnecessary bagage. Just include cstdlib (as previously mentioned) or stdlib.h.

>>You have to include windows.h,

No you don't. including windows.h will make a simple console program carray around a lot of useless and unnecessary bagage. Just include cstdlib (as previously mentioned) or stdlib.h.

But can I do that on a OS with a Mac? I'm trying it now and it is not working.

EDIT: I just ended up deleting the line.

Edited 5 Years Ago by determine: n/a

This article has been dead for over six months. Start a new discussion instead.