I have just started programming in c++ (yesterday), my background is in HTML, CSS and Javascript.
i'm on chapter 5: controlling program flow (c++ for dummies) currently creating a break in a "for loop".

Anyway'z my problem is, when i build and run the program all action's i want the program to take are executed fine but after there executed the damn "system("PAUSE")
return 0;"

dosent work it just executes the program then displays: -
"sh: line 1: PAUSE: command not found
breakDemo has exited with status 0."

also im writing my programs in Mac OS X xCode dose this have anything to do with my issue as the author of my book is demonstrating in dev-c++ for WIN systems.

any help you could give would be much appreciated, and thanks for your time.

Recommended Answers

All 16 Replies

Yes it is because your OS is different. See what system(<command>) call does is, it invokes the Operating System shell API. Command PAUSE exists in Windows shell. So replace PAUSE with an equivalent MAC call and you will be fine.

cheers,
aj.wh.ca

Soo.. i need to be looking for a Mac OS X C++ resource site?

Not necessarily. You could just remove this line and run the program from a command shell. Or use a standard method of pausing such as cin.get.

ok i see either run the program in terminal (its our version of DOS i guess well UNIX command line shell) or simply replace "PAUSE" with "cin.get." so theres no other sytax required?

yeah thanks alot man i really appriciate your help(who ever posted) on this thread
HOLLA...

>so theres no other sytax required?
You may need to clear the stream of leftover junk:

#include <iostream>
#include <limits>

using namespace std;

int main()
{
  // Your program here

  cin.ignore ( numeric_limits<streamsize>::max(), '\n' );
  cin.get();
}

Or alternatively:

#include <iostream>

using namespace std;

int main()
{
  // Your program here

  cin.ignore ( cin.rdbuf()->in_avail() );
  cin.get();
}

Or if you think you can get away with it:

#include <iostream>
#include <limits>

using namespace std;

int main()
{
  // Your program here

  cin.sync();
  cin.get();
}

;)

But be warned, each of these has subtle differences that really shouldn't matter until you know enough to care about them.

I must confess i only started programming the other day would you mind just going through a little of it plz?
so lets look at the first code only whats happening cin.ignore onwards i get every thing else just not the rest thanks

I'm a bit pressed for time right now, so consider this a placeholder for a thorough explanation of streams and how ignore and sync work. :) Hopefully later this evening.

ok thanks alot man

P.s. im in the UK (Manchester) and its 11:30pm here so ill prob be asleep when you reply.

ok i tryed out the first oone but i seem to be having a whale of a time getting the if statement to work any suggestions:-

//BreakDemo - input a series of number.
//			  continue to accumulat the sum
//			  of these numbers untill the user
//			  enter a negative number
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

int main(int nNumerofArgs, char* pzsArgs[])
{
  // input the loop count
  int accumulator = 0;
  cout  << "This program sums values enterd"
		<< "by the user\n";
  cout  << "Terminate the loop by entering "
		<< "a negative number\n";
	   
// loop "forever"
for(;;)
{
	// fetch another number
	int value = 0;
	cout	<< "Enter next number: ";
	cin		>> value;
	
	// if it's negative...
	if (value < o)
	{
		// ...then exit
		break;
	}
	
	// ...Otherwise add the number to the
	// accumulator
	accumulator = accumulator + value;
}

// now that we've exited the loop
// output the accumulated result
cout	<< "\nThe total is "
		<< accumulator
		<< "\n";
	
  cin.ignore ( numeric_limits<streamsize>::max(), '\n' );
  cin.get();
}

thanks for any help

>if (value < o)
You may need to change your font. The letter o is not the same as the integer 0.

>Hopefully later this evening.
Okay, a stream is basically just a bunch of characters in sequence. For example:

atest

When you open the stream, you can see 'a'. However, to get to any of the other characters on the stream, you have to extract all of the characters in front of them. So to get to 's', we would have to extract 'a', 't', and 'e'. A stream is filled with characters from an outside source such as the keyboard.

When it comes to basic input in C++, you need to remember only two things:

1) Input in C++ is line oriented
2) Anything that doesn't read an entire line may leave characters on the stream

By "reading an entire line", I mean reading all characters up to and including the '\n' character, which signals the end of a line. Two common ways of getting input will not read an entire line. The first way is single character input with cin.get():

cout<< cin.get() <<endl;

If the user types "atest" then cout will print 'a', and "test" will be left on the stream. A subsequent call to cin.get() will immediately read 't' instead of waiting for more input, as it would if the stream were empty.

The second way is any input using cin's >> operator. Think about it, if the stream contained "12345atest" and you told cin to take an integer:

int x;
cin>> x;

Then it will stop at the first non-integer character, which would be 'a'. Therefore, "atest" would be left on the stream.

Now, an input stream defines the ignore() member function that basically reads characters from the stream and discards them until a limit is reached, either by reading N characters, or reading a certain character:

void ignore ( int n, char delim = EOF )
{
  char ch;

  while ( --n >= 0 && ( ch = get() ) != delim )
    ;
}

So let's look at the first solution:

cin.ignore ( numeric_limits<streamsize>::max(), '\n' );

numeric_limits<streamsize> is a template that defines a max() function. The max() function returns the total number of characters a stream can hold. So ignore will read at least as many characters as a full stream has. The second argument defines the newline character as a delimiter, so ignore will read at least as many characters as a full stream has, or until a newline character is read. Remember, because input in C++ is line oriented, this effectively discards all of the characters in the stream because there is unlikely to be characters beyond a newline on an interactive stream like cin.

That's the simplest of the solutions to understand. The second is:

cin.ignore ( cin.rdbuf()->in_avail() );

Every stream has a buffer for efficiency reasons, and in C++ that buffer knows how many characters it has. Put simply, the stream knows how many characters are on the stream. You can find out that tidbit of information by calling the in_avail() member function of the stream buffer, which can be obtained by calling the rdbuf() member function of the stream.

Because the second argument to ignore has a default value, you can omit it in the function call and it will default to the end-of-file. The call basically says to discard all existing characters on the stream.

The third solution is the shortest, but also the most difficult to understand:

cin.sync();

Without getting into the nitty-gritty details of the C++ Standard language, and reasons why this solution is debatable (though I have yet to see it fail to work properly), sync will remove all characters from the stream.

ok so i relativly understand the first solution(you telling me how to make the program stop and read certain results from user input correct?)

like i said i really am new to this

ok i understand what a function is(a mini program that executes certain task for you within a code.)
but what is a stream? god this book I've gots useless this is so hard i've come from a HTML, CSS and Javascript backgroung so im trying to understand how this language is passing data around(well just how it works)
like with HTML i have a head(command for the browser) and body(visual application/static site/dynamic depending on the codeing)

ok my heads fried, i know i can do this i just need to be able to visualise things

In Narue's examples try deleting line 10. This worked for me. I too am just learning c++. I have no prior experience in programming. I find things that work and go with it. I don't understand what having or not having line 10 does except for the fact that without it my program runs without any errors. I've read cin is a "cousin" of cout and that they are used quite a bit. hope this helps.

I don't understand what having or not having line 10 does except for the fact that without it my program runs without any errors.

My code is standard conforming, so I can think of two reasons:

  1. You're not using a modern compiler.
  2. The "errors" you're getting aren't really errors. Rather, you're experiencing the extra blocking read from cin.ignore() when the stream is empty. This can seem like an error if you're not expecting it.
Member Avatar for v3ga

In Narue's examples try deleting line 10. This worked for me. I too am just learning c++. I have no prior experience in programming. I find things that work and go with it. I don't understand what having or not having line 10 does except for the fact that without it my program runs without any errors. I've read cin is a "cousin" of cout and that they are used quite a bit. hope this helps.

It was a 6 year old thread may not be much help ryt?

It was a 6 year old thread may not be much help ryt?

You'll find that older threads have a tendency to show up near the top of the search hits on Google. That's one reason why we don't automatically close threads after a certain age.

Member Avatar for v3ga

You'll find that older threads have a tendency to show up near the top of the search hits on Google. That's one reason why we don't automatically close threads after a certain age.

I had replied to TheGoldenMaster since he seemed to be thinking that he was helping the OP

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.