PDA

View Full Version : cin.readLine(); //not being executed. Why?



Landslyde
25th May 2012, 02:41
I'm new to both C++ and Qt. I'm reading an online book (http://www.ics.com/files/designpatterns/book/index.html) and am at Example 1.8. My code is exactly as theirs, but I cannot get to cin.readLine(); I can enter my sentence with whitespaces and hit <enter> a 100 times, but I never get to the next line of code. Can someone tell me why? Here is my code:

//#include <QtCore/QCoreApplication>
#include <QTextIStream>
#include <QString>
//#include <QFile>

QTextStream cout(stdout);
QTextStream cin(stdin);


int main()
{
//QCoreApplication a(argc, argv);
QString s1("This "), s2("is a "), s3("string");
s1 += s2; //concatenation
QString s4 = s1 + s3;
cout << s4 << endl;
cout << "The length of that string is " << s4.length() << endl;
cout << "Enter a sentence with whitespaces: " << endl;

// The following three lines of code never execute.

s2 = cin.readLine();
cout << "Here is your sentence: \n" << s2 << endl;
cout << "The length of your sentence is: " << s2.length() << endl;


return 0;
//return a.exec();
}

ChrisW67
25th May 2012, 05:22
I seriously doubt their example has commented out lines of code. Please use
... tags around code.

Anyway, that code compiles and runs correctly here. Are you running it on Windows and is a console window popping up to take your input?

Landslyde
25th May 2012, 05:38
ChrisW67: The commented-out lines have no affect on the program execution. However, I did go back and put the code in line-for-line. The same result occured. The program accepts my sentence, but it will not move past it to the next line of code: cin.readLine(); My OS is Linux Mint. The program output is in Qt Creator, in its own console below the code. This is the only example that has not ran as expected, and I cannot understand why.

ChrisW67
25th May 2012, 06:14
The "Application Output" area is, as the name implies, for output. It is not a terminal and is not connected to the program's stdin.

Go to Projects (Ctrl-5), Run Settings, and turn on Run in Terminal for your program.

Landslyde
25th May 2012, 07:17
Thanks for the insight, ChrisW67. I did select the "Run in Terminal" and then ran the program. My term pops up just fine, but that's all I get. A term window. Blank. Nothing. Oh well, everything else is working well. I was wondering though, isn't a terminal also for application input/output?
Also, how do I get my main window to open Maximized? As I recall in VB, there was a way to set this in the Properties window to have the app Maximized when it was launched. Do I have to hard-code this? Just wondering. Truth told, I think I'm fighting with this, making it more difficult than it should be. Has been so long for me though. I started programming back in '83. Had a pretty bad blow to the head though and was in a coma for some time. The very things that use to be second-nature are not there the way they use to be. I'm struggling and am not use to it at all. I do appreciate your help. For real. Thanks.

ChrisW67
25th May 2012, 08:44
Under Tools, Options..., Environment, General tab you will find the terminal command it is using to launch you program. Perhaps that is broken. It defaults to "xterm -e" on my system, which of course requires xterm to be present. The KDE equivalent "konsole -e" does not work in my environment.

Can you manually run it from a command prompt?

Landslyde
25th May 2012, 12:17
My General Environment terminal is set to: x-terminal-emulator -e

That should work, right? As for running it from the command prompt, I can get to the dir (the desktop build where the .exe is), but I don't know how to run it from there. Sorry, but I have done very little from the terminal. I can do the sudo get-apt (or apt-get).... But that's about it for me.

No big deal on this though. Everything else has driven smoothly, so I'm not sweating this. Sure appreciate your help though. I do have other questions with regards to file i/o, but I'll work through this book a little before I just ask for help. I like to learn. I taught myself how to program using a Timex Sinclair 1000 =) Cassette driven media storage, using your TV for the monitor through a TV/Game Atari switch on the back of the TV. I cut my teeth though on an Apple IIc. Most everything written back then was in BASIC, so I'd put a 5.25" floppy in and break into the programs to look at the code, see how they made it all work. Then I went to Pascal, Delphi, and about 12 other languages. I always wanted to code in C++, then the accident came. Heck, I doubt much memory of it would be left anyway. So here I am, using Qt4 to learn C++. I have a particular program I want to write, so there lies the challenge.

Thanks again for your help.