Java and c++ user input on M1 MacBook Pro

My macbook pro m1 doesn't take user's input when i run java or c++, (I haven't tried others), in java case it's the Scanner and in c++ it's cin. Peculiar thing is that it only happens when I run it in vim or emacs, when run it in "regular" IDE like vscode, visual studio, intelij or xcode everything's fine. I've tried different compilers but to no avail.

I don't use windows but ran it in Linux and everything works as it should.

Is it something wrong with my settings?

PLEASE HELP

Thanks, Louis

Posted on Apr 27, 2022 6:50 AM

Reply
Question marked as Top-ranking reply

Posted on May 1, 2022 3:04 PM

Found out what was wrong,

there's a command prompt in vim and emacs(evil mode) (:!<command here>) and I just found out today that it doesn't have a full capabilities of a terminal app. So, all the time I was looking for a bug but there wasn't one.

I'm really sorry for wasting your time but it wasn't intentional.

To run the programme I have to do it from terminal app, no shortcuts.

Louis

Similar questions

11 replies
Question marked as Top-ranking reply

May 1, 2022 3:04 PM in response to MrHoffman

Found out what was wrong,

there's a command prompt in vim and emacs(evil mode) (:!<command here>) and I just found out today that it doesn't have a full capabilities of a terminal app. So, all the time I was looking for a bug but there wasn't one.

I'm really sorry for wasting your time but it wasn't intentional.

To run the programme I have to do it from terminal app, no shortcuts.

Louis

Apr 29, 2022 2:18 PM in response to LouisCyphre

The C++ getline() call can be subtle and quick to anger, particularly around cin.


Here's a tweaked example, with a cin.ignore() added and with a header-file fix (using the C++ header and not the C header), and also showing a different means of performing the user input query. This sequence uses bash.


$
$ xcodebuild -version
Xcode 13.3.1
Build version 13E500a
$ sw_vers
ProductName:	macOS
ProductVersion:	12.3.1
BuildVersion:	21E258
$ clang++ -std=gnu++17 -o x x.cpp
$ ./x
Enter your favorite color >> 
blue
You picked blue
Enter your favorite number >> 
6  
You picked 6
$ cat x.cpp
#include <cstdio>
#include <iostream>
using namespace std;
int main(int argc, const char * argv[]) {
    string answer;
    cout << "Enter your favorite color >> " << endl;
    cin >> answer;
    cin.ignore();
    cout << "You picked " + answer << endl;
    cout << "Enter your favorite number >> " << endl;
    getline( cin, answer );
    cout << "You picked " + answer << endl;    
    return 0;
}
$ 



I'd guess the Oracle Java issue has a similar origin, but no Java is here installed, nor is Java usage here common.


PS and entirely FWIW: when posting questions and source code reproducers for problems, I'd suggest using the source code insertion button <>, and also posting the macOS and Xcode versions as those are (generally) more central to a C++ question than is the hardware, as well as posting the particular command-line clang++ command used.

May 1, 2022 7:39 AM in response to LouisCyphre

LouisCyphre wrote:

Hi,
Thanks for the help but it still bahaves the same, program just doesn't wait for user's input, spits out everything at once.
I'll kepp searching, I guess.
Louis


So to confirm, you used and tested with the app source code I provided, built with the clang++ command shown, and it did not prompt for input?


Interesting.


(That is a command-line app, so it’s necessarily built as a command-line app within Xcode, and build with the clang++ command shown from the command line, and run from Terminal.app or some other emulator.)


What are your terminal settings for character encoding?


Have you tried using the Apple Terminal.app and not another emulator?


Which Xcode and which macOS?


(I’m still not entirely sure what you’re actually doing here, either. Running it in vim or emacs? That’s probably possible with some customizations or extensions, but lets get the code working at the command line first…Edit, exit, compile and link, run and test, repeat, etc.)


May 1, 2022 10:10 AM in response to LouisCyphre

I do not yet understand how vim and emacs text editors are involved here, though you can and do reference those repeatedly.


With a default install on macOS, AFAIK the text editors are not involved in C++ processing cin for input, or getline().


Because unless you're using those editors as an IDE and are trying to build and run (and the results failing to prompt, as you keep repeating) within the context of those text editors—with an add-on plug-in or other such customization or makeprg or such—those editors are not involved here.


And if the add-on plug-in or customization you are using here is not working, well, at the risk of demonstrating my hammerlock grasp of the obvious, stop using it and build using the classic command-line pattern, and contact the folks maintaining the vim or emacs plug-in for assistance.


Here's an example of what I'd expect to happen:


https://stackoverflow.com/questions/21474662/compiling-and-running-a-c-program-with-vim#21477312


If you're doing something different than this, please be more specific.


I use vim a whole lot, quite possibly with some old-fashioned habits, and this isn't a sequence or situation I've encountered. But as an IDE, Xcode... Which runs circles around older methods.

May 1, 2022 9:56 AM in response to LouisCyphre

Using the exact code and compilation that MrHoffman provided, and with either vim 8.2.3455 from the command line, or MacVim 8.2.3455 on macOS Monterey 12.3.1, I can enter !x or !~/Code/CXX/x and have interactive prompts and inputs just as if I ran the same code from the command line outside of vim. I don't touch emacs with a stick.


Did you omit the cin.ignore() line which would flush the remnant newline from the buffer?


Apr 29, 2022 12:36 PM in response to MrHoffman

Hi, sorry it took me so long, busy at the uni.

This is a simplest example, when code runs, it doesn't wait for user's input and it's happening here in vim(iTerm2) and in emacs. When i run the same code in IDE, whether it's xcode or vs code, it runs as it suppose to. Quite frankly, I'm discombobulated.

The same is happening when I run java programme using Scanner class.

 example.cpp  

1 #include <iostream>

2 ▏

3 using namespace std;

4

5 int main()

6 {

7 ▏ string name;

8 ▏

9 ▏ cout << "Enter your name >> " << endl;

10 ▏ getline(cin, name);

11 ▏ cout << "Hello " + name << endl;

12 }

Output:

:!./prog

Enter your name >>

Hello


Press ENTER or type command to continue

Thank you for taking the time.

For clarity, I use M1 MacBook pro 16 inch.

May 1, 2022 9:25 AM in response to MrHoffman

Hi,

let me be clear, programme asks for input but doesn't stop and wait for it, just runs right through.

This behavior is only in vim or emacs, also since then I've tried similar programm in c, again the same thing is happening.

That's why I think it must be "environment" not language specific issue

All I ask the programme to do is to stop after the question and wait for user inputn, then take this input and display it.

It just runs through

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Java and c++ user input on M1 MacBook Pro

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.