Xcode 6 Beta 3 Unusable Variable Issue - c++

So I am a novice C++ programmer, and have never coded anything on MacOS. Currently I am using Xcode 6 Beta 3 and I am getting an unusable variable error whenever I want to name a variable to an integer.
my setup looks like this
#include <iostream>
using namespace std;
int main ()
{
int a;
cout << "Hello World!" << endl;
return 0;
}
I am getting a warning sign beside the int a; saying that it is an unusable variable. Why is that?
Also because that is such a novice question perhaps you could direct me to some solid Xcode 6 tutorials that would teach me how to code in the command line.

Related

Console doesn't show output from vector

I have a weird problem when trying to print out the content of a vector.
I'm using Visual Studio Code with the CMake extension.
I can print out simple text using cout
#include <iostream>
#include <vector>
using namespace std;
int main() {
cout << "test" << endl;
return 0;
}
But I can't print out the vectors content
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> test = {1,2,3};
cout << "test" << endl;
cout << test[1] << endl;
return 0;
}
I've never really worked with C++ vectors, so I'm probably missing something fairly obvious but I followed a C++ vector tutorial step by step and for them, the output works fine.
Cheers,
Luca
I'm new to C++ and have exactly the same problem.
Temporary workaround as posted by Luckylone here
would be to copy libstdc++-6.dll from your mingw64\bin folder into your project folder.
Something is messing up our link with the resources even though folders are properly added to system PATH.
EDIT: After an afternoon of troubleshooting, I've solved my problem by uninstalling compilers (originally provided through WinLibs) and reinstalling them using MSYS2.
Delete the existing Mingw64 folder, remove the PATH variables, then carefully follow these instructions. Keep in mind that I had to add mingw64/bin to both user and system Path, and restart VS Code before damn std::vector finally started to print.

C++ cout is not printing to command prompt

I'm getting started with C++ and am trying to work with vectors and printing stuff. I am using Windows 10, writing my code in Visual Studio Code, I just downloaded the MinGW-g++ compiler, and am trying to run my code in CMD.
This is my code, I've tried printing different things, and it's not even printing this "Hello". I've also tried using << endl; which also prints nothing. I also tried adding and remove a "return 0;" at the end but it changed nothing. The program compiles just fine. Any advice?
#include <iostream>
using namespace std;
int main(void)
{
std::cout << "Hello";
std::cout.flush();
}

getline(ifstream, string) on Mac causes EXC_BAD_ACCESS

I've been a C++ developer since it arrived. All was on windows, and I haven't touched it in about 6 years.
Now I'm trying to get an old code-base working using VS Code on my Mac. I'm using clang++ with c++17.
This problem is vexing; I've seen many other posts with the same issue, but the problem always seemed to be something in the code.
Note: this code worked fine with C++11 on Windows.
To simplify, I copied the code to execute right at the top of main. Here is is:
ifstream file("assets/textures/blocks.txt", ios::in);
if( file.is_open() ) {
string s;
getline(file, s); // <-- This line causes the error.
cout << s << endl;
}
As this code worked elsewhere, I assume I've got a setup or environment problem and am looking for hints towards what to check on.
Thank you for any help!
An update:
Thank you. I paired the program down and tried a few things. Here's the deal:
If I leave all my files to be compiled, but replace main.cpp with the below code, the cout line generates the same exception.
If I cull all the unused files, the code works.
Something in some other file is somehow breaking the stream code. I'm clueless.
#include <iostream>
#include <istream>
using namespace std;
int main() // int argc, char** argv)
{
cout << "Hello World" << endl;
}
I should add: this is a GLFW 3D game engine app. It does not subclass or interact with any stream in any way other than the most basic file read/write operations.

Why is it mandatory to use namespace std in new IDEs whereas the programs written in Turbo C++/Borland C++ don't require namespace std? [duplicate]

This question already has answers here:
Why doesn't a simple "Hello World"-style program compile with Turbo C++?
(3 answers)
Closed 3 years ago.
Why is it mandatory to use namespace std in new compilers whereas the programs written in Turbo C++/Borland C++ don't require namespace std ?
This works in old compilers
#include <iostream.h>
int main () {
cout << "Hello Programmers";
return 0;
}
but we have to write the below given program in new compilers instead of the above one, as the above programs don't work in new compilers.
#include <iostream>
using namespace std;
int main () {
cout << "Hello Programmers";
return 0;
}
That's because turbo-c++ was released even before any c++ standard was released, and they didn't introduce a std namespace.
It was never updated since then.
Also it isn't mandatory to use the using namespace std; statement, but rather discouraged.
The code should be:
#include <iostream>
int main () {
std::cout << "Hello Programmers";
}
or
#include <iostream>
int main () {
using std::cout;
cout << "Hello Programmers";
}
Also IMO questions about turbo-c++ are quite futile this time. It's outdated and not remotely has to do anything with modern c++.
If your professors / teachers force you to use it1, tell them that they're doing it wrong and don't teach c++ in any way.
1)I know it's common at indian schools, but that's simply bad practice, and doesn't have a sound reasoning.
May be they want you to teach some things from scratch, because turbo-c++ doesn't support containers like std::vector or such.
But I still believe that's the wrong approach, because manual memory management is advanced stuff, and shouldn't be used to confuse beginners.
I might be splitting hairs, but it is never mandatory to use using namespace std;. See here for why it is considered bad pratice.
Your first version may "work" in some ancient non-standard conformant compilers.
What you should do is write
#include <iostream>
int main () {
std::cout << "Hello Programmers";
return 0;
}
If you are lazy you could use
#include <iostream>
using std::cout;
int main () {
cout << "Hello Programmers";
return 0;
}
And the version with using namespace std; is also technically correct, but it will lead to all sorts of nasty problems in bigger projects.
First of all, it's not an IDE question, but a C++ compiler (C++ language implementation) question.
The first TurboC/BorlandC was shipped decades ago, when there were no namespaces intruduced in C++.

11db error in XCode C++

I'm following some C++ tutorials and have started experimenting with structures, however a test structure I built is not behaving as intended. I have tried running it a few times and all it outputs is (11db), and when I try rerunning it I get a window saying that the product is already running.
Here is the code...
#include <iostream>
using namespace std;
int main(){
struct Address {
int streetNum;
string streetName;
};
Address myHouse;
myHouse.streetNum = 911;
myHouse.streetName = "Inverness Street";
cout << myHouse.streetName << endl;
return 0;
}
I would expect the output to be "Inverness Street". Why am I getting an error instead?
EDIT ** Bonus points for anyone who can tell me how to remove a missing file warning? I removed a useless file that was created as a means to find my way around creating different types of files, but since I deleted it I have had a warning in the file that it once shared a folder with.