(lldb) error code not working in Xcode with C++ - 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.

Related

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

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

[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)

Compile command works but Run command is not responsive

Here is my c++ code to read an integer, double it and then print it out on the screen:
#include
<iostream>
int doubleNumber(int x) { return 2 * x; } int main() { using namespace std; int x; cin >> x; cout
<< doubleNumber(x) << endl; return 0; }
I am compiling it using:
g++ -o example9 example9.cpp
It seems it is fine and it creates the object file but it is impossible to run the file using the following command:
./example9
In fact it does nothing (not even an error message)
What I am doing incorrectly?
Your help is appreciated.
When you say it is "impossible to run the file", I expect you are seeing something like the following:
$ ./example9
_
where I have indicated the cursor position with _ (just sitting there, on the next line, not doing anything except blinking). In this case, your program is running fine. It is waiting for you to type a number (the cin >> x statement). Type a number and press Enter.

Variables cause error in c++

In C++ when I try to use variables with cout it ill cive me an error pop up. I use eclipse as IDE and minGW as compiler. This is the code:
#include <iostream>
using namespace std;
int main() {
int num = 4;
cout << num << endl;
return 0;
}
between the cout and num and num and endl; I got those to diamond breckets pointing left but they won't show here.
can anone please help me?
edit:
the error that pops up is(translated from dutch to english):
file.exe doesn't work anymore
A problem occurd causing the program to malfunction. The program will be closed and a notification wil be shown when there is a solution available.
rebuild your project : probably you 're running old version of your project

Why doesn't the cout command print a message?

#include <iostream>
using namespace std;
int main()
{
int x = 42;
cout << x; // This line doesn't print! Why?
return 0;
}
Screenshot of Visual C++: http://bildr.no/image/ZlVBV0k0.jpeg
This code gives me nothing but a black console window that flashes by when I click on debug. Isn't the number 42 supposed to be printed in the console window? This is my first application in C++. I have experience in C# from high school.
EDIT:
Now I have tried this code:
// Primtallsgenerator.cpp : Defines the entry point for the console application.
//
#include <iostream>
using namespace std;
int main()
{
int x = 42;
cout << x << endl; // This line doesn't print! Why?
cin >> x;
return 0;
}
It still doesn't work. Screenshot of the code here: http://bildr.no/image/ODNRc3lG.jpeg
The black windows still just flashes by...
Two things to note:
First, you are not forcing the buffer to flush, so there is no guarantee the output is being sent to the screen before the program ends. Change your cout statement to:
cout << x << endl;
Second, Visual Studio will close the console when it ends (in Debugging mode). If you do not debug it (Ctrl-F5 by default), it will keep the console open until you press a key. This will allow you to see the output. Alternatively, you can add a cin.get() before your return statement which will force the program to wait for a character to be in the input stream before the program is allowed to exit.
It did print the message, it was just too fast for you to see.
add this command:
cin >> x;
or this one
while(true) {}
before the return statement.
Yes, it will print the number. Then the program ends, and the console window is closed. Run it in the debugger, and put a breakpoint on the return 0; line. Then you'll see it.
This code should work fine:
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
int x = 42;
cout << x;
getchar();
return 0;
}
Also check this documentation about getchar().
I recommend to use system pause at the end before the "return 0" statement like this:
system("PAUSE");
This is cleaner and much more effective.
If you are working with a console application in Visual Studio, you have to go to your project's linker properties and set your SubSystem setting to CONSOLE.
And get a habit of running your code without a debugger (Ctrl+F5 by default) when you don't need the debugger. That way the console window will not flash and disappear by itself.