c++ issues using cout and cin on same line (xcode8) - c++

Using Xcode with c++ I'm trying to create a simple console application. However my usage of cout and cin does not seem to work as I intend it to.
I'm expecting:
Testing: 12
input was 12
edit: i've cut down the code test as much as I can:
#include <iostream>
int main(int argc, const char * argv[]) {
// insert code here...
int num;
std::cout << "Testing: ";
std::cin >> num;
std::cout << "input was " << num << std::endl;
return 0;
}
Sample output:
12
Testing: input was 12
Program ended with exit code: 0
Is there something I'm missing here?

Apparently is a specific problem with C++ streams, in Xcode debugger, Debug build.
Try this:
1. Project -> Edit Active Target ...
2. Search for "preprocessor" in Build
3. Delete the values:
Preprocessor Macros = _GLIBCXX_DEBUG=1 _GLIBCXX_DEBUG_PEDANTIC=1
I found a similar issue but for Xcode 3.2.1 and C++ string fails!
You can try also this workaround:
Paste these lines at the very beginning of your program (before any #include statements):
#define _GLIBCXX_FULLY_DYNAMIC_STRING 1
#undef _GLIBCXX_DEBUG
#undef _GLIBCXX_DEBUG_PEDANTIC

Related

How to use escape sequences in Xcode?

Here is a program. Its only purpose is to delete the line printed, after some user input.
main.cpp
#include <iostream>
#define ESC_PREV_LINE "\e[F"
#define ESC_CLEAR_TO_END "\e[K"
int main(){
int i;
std::cerr << "Some input to clear: " << std::flush;
std:: cin >> i;
std::cerr << ESC_PREV_LINE ESC_CLEAR_TO_END << std::flush;
}
Inside Xcode, if the user were to input 2, this is the output:
Some input to clear: 2
[F[KProgram ended with exit code: 0
If compiled in terminal, using: g++ -std=c++20 main.cpp -o test, the output is cleared after user input, right before the program exists, and the shell looks like the program never ran.
How can I include this functionality within Xcode? I am writing a program that requires it, but developing inside Xcode, and it would be highly inconvenient to have to swap to the terminal just to test my program output.

VS Code exits without displaying any output when a character array is taken as input

I have tried updating my g++ installation but there has been no solution.
Here is the code.
#include <iostream>
using namespace std;
#include<string>
#include <cstring>
int main()
{
char str[100];
cout << "Enter a string: ";
cin>>str;
cout << "You entered: " << str << endl;
cout << "\nEnter another string: ";
cin>>str;
cout << "You entered: "<<str<<endl;
return 0;
}
The output that this code shows is:
Check the terminal
Please give me a solution or at least a reason for this. I am new to Stack Overflow so please free to correct me if I made any mistake in the post. [This problem only happens in vs code but works in online gdb compiler.]
Edit:After I tried executing this in the cmd line this what it shows
cmd line execution
The output you're seeing is from some kind of makefile. You didn't say how you're trying to build the file so it's hard to say what caused the issue.
However, you can simply compile your file directly by typing g++ hgg.cpp -o hgg in the command shell
After writing the code, switch to Command Prompt (Cntrl + x) move to the directory, in which the C++ program is present.
Then run the following:
g++ -Wall filename.cpp -o filename
If nothing is printed on the console, then your code is syntactically correct. Then do the following:
filename.exe
It should work now.

[C++]Eclipse ignores console input during debugging

During debugging Eclipse doesn't "see" INPUT from built-in console, just ignores it. Simple example:
#include <iostream>
using namespace std;
int main() {
int a;
cin >> a;
cout << a << endl;
cin >> a;
cout << a << endl;
return 0;
}
works perfectly fine when just run, but when I try to debug it, first "data on input"(?) is always a number around 40, and only zeros next, no matter what i will write to console.
So program executes, first variable is set to ~40 and all next to zeros.
Output works fine, values are written to console, only input doesn't work.
I work on Windows 10 and use MinGW.
Thanks in advance.
#EDIT
Everything works, when I use native Windows console
(.gdbinit file with set new-console on line)

Visual Studio 2012. "unable to start program" *.exe "cannot find the file specified"

Yes, I know this is a common question. No other question/answer sets meet my needs here. Taking c++ this quarter. Been going along just fine. Trying to do last assignment, and I'm getting the above error, even on a fresh project with:
#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
cout << "hello world" << endl;
}
As an experiment, I tried pasting in much more elaborate code from previous assignments. This runs. Just pasting in -- not running the previous assignment.
I took the simplest of them, and stripped it down to this:
Main.cpp:
#include <iostream>
using namespace std;
int main()
{
double hours, rate;
cout << "Hello World";
cin >> hours;
cout << "Hello World";
cin >> rate;
system("pause");
return 0;
}
This compiles and runs fine. However, if I remove either of the cin lines I get the error stated in the title.
I'm just flabbergasted because pasted in code compiles and runs, but even "Hello World" in a fresh project won't.
Does my above example provide any clues?
Does it happen if you run it as administrator? Were you running it from Visual Studio or did you clicked the executable?
If you have an anti virus, it may be causing the error as well.

(lldb) error code not working in Xcode with C++

Im just starting with C++ and I wanted to try running this test... When I try compiling the code it just gives an error saying (lldb):
#include <iostream>
using namespace std;
//---------My Function--------//`
int addNums(int x, int y){
int answer = x + y;
return answer;
}
int main(int argc, const char * argv[]){
// insert code here...
/*
int num;
cin >> num;
cout << num;
cout << "\n";
char hm[] = "eef";
cout << hm[2] << endl;
*/
cout << addNums(1, 2);
return 0;
}
If by "Build" you mean you clicked the Play button - the leftmost button in the toolbar - then this is going to build and run your code. The build probably went fine. You can check that the build went okay by switching to the Reports navigator (the one with the speech bubble icon) and click on the latest Build report... If you want to build without running, Cmd-B is what you want to do.
Anyway, if you asked to build & run, and the build went okay, Xcode will start your program in the debugger, which will switch to the Debugger UI. Since your program just prints something and exits, the Debugger should have just printed "3" in the Debugger Console and exited. Not sure why you are seeing an lldb prompt, that is not what I see.
You might try setting a breakpoint, and see what the debugger looks like when you hit the breakpoint.