Qt reading from console and stop when Enter has pressed - c++

QTextStream cin(stdin);
QTextStream cout(stdout);
QString path;
cout << "Set directory to save configuration file: ";
cout.flush();
// path = cin.readLine();
cin >> path;
Here is the code. It works fine when you need to enter some text into console. It prints message and then waits until you write some text and then press the Enter key. BUT, if you don't want to enter any text and you want to leave path string empty, this code doesn't approach: it doesn't recognize Enter as end of the line/new line, so if you try to press Enter without writing any text, cursor would be only switched to the next line and program would still wait until you'll write something.
So, is there any way to recognize Enter key NO MATTER if path string empty or not? Simple: you press Enter key - program stops to read from console.

For QT you could use QTextStream::readLine.
Instead you could also use std::getline along with the normal std::cin and std::cout.

The only way i found is to go to the project tab on Qt Creator, got to run, and check run "in terminal" (under "working directory"). This will just run in external terminal which does recognize the enter key.

Related

Unable to write to file output.txt

I'm trying to write a program. The first file will be opened for input and the second file will be opened for output. (It will be assumed that the first file contains sentences that end with a period.) The program will read the contents of the first file and change all the letters to lowercase except the first letter of each sentence, which should be made uppercase. The revised contents should be stored in the second file.
I've been able to get my code to work in that I successfully converted the contents in input.txt to the above requirements (all sentences are lowercase except for the first word in each sentence). However, this content does not appear in output.txt
Both input.txt and output.txt are in the same directory next to main.cpp.
I tried using different IDEs but that didn't do anything. I also tried moving around the location of output.txt but that did nothing also.
SAMPLE INPUT: google's homepage includes a button labeled "I'm Feeling
Lucky". When a user types in a search AND clicks on the button the
user will be taken directly to the first search result, bypassing the
search engine results page.
SAMPLE OUTPUT: Google's homepage includes a button labeled "i'm
feeling lucky". When a user types in a search and clicks on the
button the user will be taken directly to the first search result,
bypassing the search engine results page.
string inFileName, outFileName;
string line;
char c;
cout << "Enter input file name: ";
cin >> inFileName;
fstream fin, fout;
fin.open(inFileName.c_str(), ios::in);
fout.open(outFileName.c_str(), ios::out);
if (fin.fail())
{
cout << "INPUT FILE DOES NOT EXIST (DNE)\n";
system("pause");
return 1;
}
Nothing shows up in output.txt (it's blank). From what I've noticed this command fout << line << "." << endl; isn't doing anything.
[Here's another screenshot]that shows what's in my terminal as well as what is in input.txt and output.txt:
You'll notice that in the terminal the proper conversion is shown but I am unable to get that text in the terminal into output.txt.
After every source code is compiled, a executable program is created. On Windows, it has .exe extension. The program should have the same name from your source code.
Try to find where it is created. It can be inside the temp folder or maybe where Visual Studio is installed.
Your text document (.txt) file for storing output has to be in the same directory with the executable.
I executed your program on my device (on Code::Blocks IDE though). It went as it should.
I agree with all the above answers and they are appropriate for all the users. In my case though there was a segmentation fault that led to file write failure. Try rectifying any segmentation faults.

Providing default value of cin input

My app reads user input using std::cin stream.
In one place I would like to provide default input and let the user to accept it as it is (by pressing enter) or modify it before continuing (by removing old characters with backspace and adding new text).
I'm aware that characters can be placed directly into cin.rdbuf, but that's not exactly what I want to achieve.
I would like to put characters into console window in the place where console's cursor is when waiting for user input and do no read them before user will accept them. User should be also able to remove them and write their own text.
Can something like this be achieve using cin or do I have to simulate this by reading single characters and repainting content of the console?
No, something like that cannot be done with std::cin. Its read buffer is read directly from standard input. Standard input is a "cooked" character stream. All the editing is handled entirely by your operating system's terminal console, and pressing Enter results in your application's std::cin reading the entered text.
The traditional way this is done is to simply indicate the default input value in the prompt itself, and use the default value in the event of empty input, something like:
std::string buffer;
std::cout << "What color is the sky [blue]? ";
std::getline(std::cin, buffer);
if (buffer.size() == 0)
buffer="blue";

Trouble with getline when using eclipse's console

I recently started to use eclipse for my C++ code, and wanted to run a simple function that asks the user for a filename, then reads from the file and sums up all the numbers to test it out.
string filename;
do {
cout << "Enter File Name: "; // Asks for user input
getline(cin, filename);
}
while (filename.empty());
ifstream myfile (filename);
if (myfile.is_open()) {
// sums up text file & outputs it
}
else {
cout << "Error Opening " << filename << endl;
}
The program works perfectly when I run it via Windows Command Prompt in my executable folder and type in the filename. However, when I try to run it via eclipse's console using the exact same input, where I've set the working directory to the project root where the file most definitely exists, the file does not open.
If I change a single line:
ifstream myfile("test1.txt");
Then the code works again perfectly. So now I've narrowed down that for some reason, the Eclipse console interprets my text input via getline differently from the windows command prompt, but I'm not sure what I can do so it behaves consistently.
Calling
cin.ignore()
Before calling getline() just removes the first character of my input (so it tries to "est1.txt") instead.
Any idea what exactly eclipse does to its console that makes getline unreliable? And if this is the case, what I should do to work around it?

Is it possible to cout without overwriting current text in the input?

Okay, so let's say I have a program that couts a line while the user may be typing in information.
For this example, let's say we're using the code
cout << "THIS CODE IS BEING COUTED" << endl;
Let's say for our example, the user is in the process of typing up an input and as it stands they have only entered "hello", but has not yet pressed enter.
As it stands, when the line is executed, the user will see
"helloTHIS CODE IS BEING COUTED"
and they will be given a new line to input information.
What I want to do is instead of cout'ing, I would like to get the text in the current input, erase it from the input, cout the line that needs to be cout'ed, and then re-enter the info into the input.
Does this make sense or is this a bunch of jumbled nonsense?
Thanks for reading.
Edit: Clarification: I want it so if I have a string entered into my input and I cout, that the cout will be displayed above my input instead of inserting it past my input. I also want my input to be unaffected so the user can continue typing or delete what was already entered.
If you are getting the input character by character then when you need to output your text you could move the output position to the start of the line by printing carriage return, '\r'. Then your output will overwrite the current input, after which print a line feed and reprint what has been input so far
cout << "\r" << output << "\n" << currentinput;
If the output is shorter than the input then you will only partially overwrite the input, in which case you could pad the output with spaces up to the length of the current input
You may ask user in one, main thread with cin >> data there and read data from stdin in other thread by fread or something like that what works with FILE* handle.
After the data you want was typed by user you may clear screen (for example by clrscr() in conio.h or by any other better way) and cout what you want.

how to make input prompt for 7 column by ifstream

I typed in a file name which I want to show on prompt screen but it says that
"'c:\test\sp.csv' is not recognized as an internal or external command,"
even though the file is available on the path.
1 - Why did this error happen? How to fix it?
C:\Users\MS>c:\test\sp.csv
'c:\test\sp.csv' is not recognized as an internal or external command,
operable program or batch file.
2 - The code below only shows one column, if I want to input for 7 columns, how would I edit the code below?
How to print out on prompt screen using ifstream for 7 column with header and price.
Date Open High Low Close Volume Adj Close
6/21/2013 1588.62 1599.19 1577.7 1592.43 5797280000 1592.43
6/20/2013 1624.62 1624.62 1584.32 1588.19 4858850000 1588.19
int main(){
int open;
string fileName;
cout <<"Enter a file name: ";
getline(cin, fileName); //c:\\test\\sp.csv
ifstream inFile(fileName.c_str(), ios::in);
while(!inFile.eof()){
inFile >> open;
cout << open << endl;
}
inFile.close();
system("PAUSE");
return 0;
}
Thank you Kelly
Looks like you're on Windows OS
.csv file extension is not an executable one. It won't get "executed", even if its present in current working directory/folder.
Probably what you want is a .exe, .com or .bat file.
Here, in this case I think you want your .CPP 's executable with command line argument.
May be something like
C:\Users\MS>c:\test\sp.exe c:\test\sp.csv' Considering your C++ file name is sp.cpp
2 . Looks like you want to display out all contents of sp.scv
You may want to read the header first (i.e the Titles), and then read the values.
There are lot of question already asked on StackOverflow, related to this, please refer them.
Also for proper formatting you may want to use std::setw