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.

Featured Replies in this Discussion

>so theres no other sytax required? You may need to clear the stream of leftover junk: ... Or alternatively: ... Or if you think you can get away with it: ... ;) But be warned, each of these has subtle differences that really shouldn't matter until you know enough to care about them.

>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: ... 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…

... My code is standard conforming, so I can think of two reasons: You're not using a modern compiler. 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.

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.

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

>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:

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:

You're not using a modern compiler.

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.

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.