mIRC Home    About    Download    Register    News    Help

Print Thread
#156348 15/08/06 09:19 AM
Joined: Feb 2005
Posts: 342
R
Rand Offline OP
Fjord artisan
OP Offline
Fjord artisan
R
Joined: Feb 2005
Posts: 342
Was just curious if anyone has read the book "Thinking in C++?" I have a few questions I'd like to ask someone if they have.

While not entirely related to IRC or mIRC, the reason I'm attempting to learn C++ is so I can write a dll or two for use with mIRC.

#156349 15/08/06 10:35 AM
Joined: Dec 2002
Posts: 2,962
S
Hoopy frood
Offline
Hoopy frood
S
Joined: Dec 2002
Posts: 2,962
You might aswell just ask. It looks like the book and relevant source code is available online anyway so even if your questions are specific to it someone might answer even if they haven't read the book already.


Spelling mistakes, grammatical errors, and stupid comments are intentional.
#156350 15/08/06 12:12 PM
Joined: Feb 2005
Posts: 342
R
Rand Offline OP
Fjord artisan
OP Offline
Fjord artisan
R
Joined: Feb 2005
Posts: 342
Yep, Bruce Eckel allows good sites to mirror his book, for those that wish to download it.

My question is aimed towards would be novice C++ programmers, whom have read the first few chapters of Thinking in C++.

To the point:

At the end of Chapter 2, of the first book, there are exercises you can do.

One of these exercises has stumped me. Specifically:

# Display a file a line at a time, waiting for the user to press the “Enter” key after each line.

I would like to know how new readers/programmers(c++) managed to figure out how to do that one?

My first idea was to just use system("pause");, but then I realized that it wouldn't be practical, since linux doesn't actually have a pause program.

The reason this stumps me is that, in Chapter 2, it doesn't show you how to do this. I was able to do all of the exercises except this one because of that.

Basically, I wanted to ask someone how they did it, as a new programmer reading this book.

--

My "programming" skills are fairly limited. I started off with mIRC, and afterwords did a little perl (which I sometimes wish Khaled would add support for perl scripts!# but that's another discussion altogether.)

I had given "C++ for Dummies" a shot a while back, but most of the examples they gave me didn't work, so I ended up giving up on it before I got into the language. Just the other day I had decided to pick C++ back up again, after someone recommended "Thinking in C++"

Anyway, as an example of the code:

Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
    string line;
    ifstream in("C:/testing.txt");
    while (getline(in, line)) {
        cout << line << endl;
        //  Need a way other than system("pause");
        //  Must wait for "enter" to be pressed.
        // Editted code:
        cin.get();
    }
}


So yeah, I'm pretty new to this. Go me..

Edit:


Okay, I just found out that Bruce Eckel has an Annotated soultions Guide. The solutions for Chapter 2 are free, but you have to pay $12 to buy the ebook if you want what is in the latter chapters.

With this said, I have the answer to my question.

cin.get();

Link: http://mindview.net/Books/TICPP/Solutions/SampleChapter

Last edited by Rand; 15/08/06 12:54 PM.
#156351 15/08/06 01:25 PM
Joined: Jan 2003
Posts: 1,063
D
Hoopy frood
Offline
Hoopy frood
D
Joined: Jan 2003
Posts: 1,063
in case you want to make a console app, and you would like the standard 'pause' message ('press a key to continue...') you can use system("PAUSE"); (windows only)

--edit--

sorry, missed that one line ;-], I skipped the comments hehehe


If it ain't broken, don't fix it!
#156352 15/08/06 01:34 PM
Joined: Feb 2005
Posts: 342
R
Rand Offline OP
Fjord artisan
OP Offline
Fjord artisan
R
Joined: Feb 2005
Posts: 342
Right. As I mentioned in my previous post:

Quote:
My first idea was to just use system("pause");, but then I realized that it wouldn't be practical, since linux doesn't actually have a pause program.

#156353 15/08/06 02:33 PM
Joined: Jan 2003
Posts: 3,012
Hoopy frood
Offline
Hoopy frood
Joined: Jan 2003
Posts: 3,012
look in to getch i believe it is. It's beeen quite a while since I've coded in the command-line level, so I forget the library to include. Might be stdio, not sure though. Returns type char.


-KingTomato
#156354 15/08/06 04:55 PM
Joined: Mar 2004
Posts: 210
F
Fjord artisan
Offline
Fjord artisan
F
Joined: Mar 2004
Posts: 210
If it's not getch (I seem to recall it is, but it's been about 17 years since I did any real C work), there's some function to get a keypress. Just loop until it == 13.

#156355 15/08/06 06:07 PM
Joined: Feb 2004
Posts: 2,019
Hoopy frood
Offline
Hoopy frood
Joined: Feb 2004
Posts: 2,019
All you need to do is use cin.get() as that waits for the user to press enter. Rand has already figured this out btw, look at his post.


Gone.
#156356 16/08/06 01:44 PM
Joined: Feb 2005
Posts: 342
R
Rand Offline OP
Fjord artisan
OP Offline
Fjord artisan
R
Joined: Feb 2005
Posts: 342
Yes, I got it working fine and dandy.

Also, about getch(); I found this on google before I found cin.get();, however, I couldn't get getch() to work, I included cstdio and cstdlib, but it was a no go.

Secondly, I also found out that getch() is now deprecated in Visual Studio 2005. Meaning that sometime in the future, it'll no longer be supported. However, _getch(); is useable.

http://msdn2.microsoft.com/en-us/library/078sfkak.aspx

I think this might be a windows specific thing though. smirk Seeming as how I can't get it to work in Dev-C++, but works fine in MS VS C++.

So, with this..
I can insert these instead:

Code:
#include <conio.h>

....

while (getline(in, line)) {
    cout << line;
    while (_getch() != 13);
    cout << endl;
}


Mind you, I'm already done with that exercise, as mentioned in my original post. I just enjoy learning new stuff!#

Must write a .dll some day!@#


Link Copied to Clipboard