Using cin in QtCreator - c++

For school, we use C++ as the language of choice. I am currently using QtCreator as an IDE, and for its GUI library, it is wonderful. The school is using Visual Studio.
However, most of the programs we are writing make use of cin and cout for input/output. cout works fine as output, as you can see what it puts out in the application output, but there is no way to provide to cin as if it were on a console, like Visual Studio uses for its C++.
An example:
#include <iostream>
#include <string>
using namespace std;
int main() {
string name;
cout << "Enter name: ";
cin >> name;
cout << "Your name is " << name << endl;
}
Is there a way to use a console or provide input to cin like in Visual Studio?
I am currently running OS X Leopard, if it matters.

In Preferences, under the Environment section, set the "Terminal" option to /Applications/Utilities/Terminal.app, as pointed out by Alex Martelli.
Then, in the Projects tab, under Run Settings, check the box marked "Run in Terminal".
Now, QtCreator will use Apple's built-in Terminal.app instead of Qt's console, allowing for interactive input.

If you're doing "console"-style apps with no GUI, Qt Creator may not be the most appropriate IDE -- why not try Apple's own XCode, which probably comes on your OS DVD (as a separate installer), and worst-case can be freely downloaded by registering at Apple Developer Connection?
Edit: as the OP indicates that all they need is the location to Mac's terminal app, that's easy: it's /Applications/Utilities/Terminal.app.

I thought I would just add that if you are using Windows 10 and Qt Creator close to v4.13.2.
And you are developing a general console project.
Go to menu:
Tools > Options > Build & Run > General
and Find Default for "Run in terminal": and select Enabled, as its disabled by default.
Now when you click Run it will automatically open a command prompt window. Provided your code compiles :)

The following solution is currently working.
go to:
Project settings > Run > mark "Run in terminal"

#include <QCoreApplication>
#include <iostream>
#include <string>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
std::string name;
std::cout << "Enter name: ";
std::cin >> name;
std::cout << "Your name is " << name << std::endl;
return a.exec();
}

Related

Clion `cout` and `cin` combination causes console to not work properly

I've recently started trying out CLion for C++ programming. I wanted to test a sample application (below):
#include <iostream>
int main()
{
std::cout << "Please enter a number: ";
int x;
std::cin >> x;
std::cout << "Your number was " << x << "!\n";
return 0;
}
This is what I was expecting (the number is user input):
Please enter a number: 10
Your number was 10!
And this is exactly what happens when I compile and run manually (g++ main.cpp -o main && ./main)
However, this is what happens when I run with CLion:
Does anyone know why this is happening, and how I can fix this?
Note: I am using CLion with the g++ compiler (version 9.3.0) on WSL2
After some more searching, I came across this StackOverflow post, which led me to this issue (upvote it!) which finally led me to do what was told in the comments:
Two workarounds are available:
Turn off PTY: by disabling run.processes.with.pty option in the Registry (Help -> Find Action -> Registry...)
Use Cygwin64 instead
I did the first option and CLion works fine now:
It looks like this is an issue with MinGW and WSL.

How to run SSH with system() in C++

I've written a simple program to connect to a Linux server using SSH via my C++ program. Here's the code:
#include <iostream>
#include <cstring>
#include <stdlib.h>
using namespace std;
int main()
{
string hostIP;
string username;
string password;
cout << "Welcome to SSH Program" << endl;
cout << "----------------------" << endl;
cout << "\nEnter host ip or name Example: \"capa.its.uow.edu.au\": ";
cin >> hostIP;
cout << "Enter username: ";
cin >> username;
cout << "\nConnecting...\n" << endl;
string composite = "ssh " + username + "#" + hostIP;
char command[100];
strcpy(command, composite.c_str());
system(command);
system("pause");
}
It runs well on Ubuntu, but when I compile this same code on Visual Studio in Windows and execute it, the console present me with this error: 'ssh.exe' is not recognized as an internal or external command, operable program or batch file.
This didn't make sense to me because OpenSSH Client is clearly installed on my computer and I'm able to establish an SSH connection if I directly enter ssh username#server.ip.address in the command prompt.
I thought it might be an issue with the environment paths and so in Visual Studio, I checked Project>Properties>VC++ Directories>Executable Directories. There I found C:\WINDOWS\System32\OpenSSH\ among other paths, located in the Evaluated value: box.
Doesn't this mean everything should run fine since the OpenSSH directory is located in the path? Also, like I said, I am able to connect via SSH if I enter the command directly into the command prompt instead of the program.
Please help. I've been really scratching my head over this since last night.
I've managed to solve this issue. It seems to have been a problem with the selected platform in Visual Studio. Initially the platform for the project was set for an x86 architecture. This is the reason why ssh.exe could not be accessed even though it was present in the environment path.
By changing the platform to 64-bit architecture in the project properties, the program could reference the correct environment variables and execute ssh.exe as intended.

application console input no response on QtCreator 3.2.1 debian system

i am new to qt. Trying to run some simple application in QtCreator 3.2.1 which is based on QT5 on Debian system.
i create c++ plain project with cmake
#include <iostream>
using namespace std;
int main()
{
string name;
cout << "please enter your name:\n";
cin >> name;
cout << "hello" << name << "\n";
return 0;
}
The problem is that when i launch the application, and input some string. The application console dont have any resp.
I confirm that the application can be run in my gnome-terminal.
After that, i check the same message from the stackoverflow.
there is a solution "set project run in terminal".
Then i look for that on the QtCreator 3.2.1. I cant find that menu. Only have the terminal setting.
unfortunately, whichever i choose,
1. /usr/bin/xterm -e
2. /usr/bin/gnome-terminal -x
3. /usr/bin/x-terminal-emulator -e
the problem still exist.
you must choose the "Run in Terminal" if you create CMake project with C++

how to change the working directory to the location of the program

I want to use c++ to open a file on Mac OS.
If I run the program under Xcode, the working directory is the same with the program, which is fine. However, if I try to run the program in terminal, the working directory is alway "Users/username". Do you know how to change the working directory to the location of the program?
Here is the sample code:
#include <iostream>
#include <fstream>
using namespace std;
int main (int argc, const char * argv[])
{
char * dir = getcwd(NULL, 0);
cout << "Current dir: " << dir << endl;
ifstream fin("hi.txt");
if (fin.is_open()) cout << "File is Open" << endl;
else cout << "File is not open" << endl;
fin.close();
return 0;
}
Use the value $(PROJECT_DIR) in the working directory in your scheme debug settings:
You can set a custom working directory for your project in Xcode. In Xcode 4 choose Edit Scheme from the Scheme menu in the project window toolbar to open the scheme editor. Select the Run step from the left side of the project editor. Click the Options button at the top of the scheme editor. Select the Use custom working directory checkbox. Click the button on the right side of the text field to choose the working directory.
This is a really old post - updating some info for Xcode 12 (Sept 2020)
Step 1). Xcode -> Product -> Scheme -> Edit Scheme (or create a new one)
Step 2) RUN(DEBUG), Working Directory(Checkmark) and enter $(PROJECT_DIR) as a starting point.
You could use chdir(), see here: Change the current working directory in C++.
Or if you could always just issue a system call (stdlib.h): http://www.cplusplus.com/reference/clibrary/cstdlib/system/. This won't be portable, but it might be good enough for what you need.

How do I run a C++ program in Xcode 4?

I want to write a C++ program in Xcode on my Mac. I wrote this basic one to test it. When I build, I get a "Build Successful" message.
However, I cannot run it! If I hit Command+R nothing happens. When I go to Project->Run the Run option is disabled.
#include <iostream>
using namespace std;
int main()
{
cout << "helo" << endl;
}
Launch XCode
In the "Choose template" box, pick Mac OS X, then Command Line Tool. Press Next
Give your project a name, select C++ as the type
You should see a new project with main.cpp
press the Run button
At the bottom of the screen, under All Output you should see:
Hello, World!
Program ended with exit code: 0