Error When Trying to Compile in Eclipse (MacOSX) - c++

I am learning Eclipse and C++ and am working on an extremely simple program. I can't build the program now. I just recently installed HomeBrew so I don't know if that has something to do with it. Here is my code (it's from a simple tutorial I'm working off of so I know the code works because I've seen it work on the tutorial):
#include <iostream>
#include <string>
using namespace std;
int main()
{
// Ask the user to input a word while the word length < 5
string word = "";
do
{
cout << "Enter a word that has at least 5 characters: " << endl;
cin >> word;
}while(word.size() < 5);
// Ask the user to input a character
char searchCh = '0';
cout << "Enter a character and the program will tell" <<
" you how many times it appears in the word " << word << "." << endl;
cin >> searchCh;
int counter = 0;
// Iterate over the word
for(int i = 0; i < word.size(); i++)
{
// Get a character
char ch = word.at(i);
// If the character matches the character we're looking for
if(searchCh == ch)
{
// Increment a counter
counter++;
}
}
// Output the number of times the character was found in the word.
cout << "The number of " << searchCh << "'s in the word " << word << " is " << counter << "\n";
return 0;
}
The error I get when build is this:
g++ -o "Program 5" ./Program5.o
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [Program 5] Error 1
I've seen several other similar question on here about going to 'Project' and 'Properties' but I don't know what settings exactly are supposed to be updated to get this working. Also, some of the other answers mentioned that a main() function is needed to fix this issue, but I obviously have one in this code. It was working fine last week when I was using Eclipse on other small projects so I'm not sure what changed.
For further information:
When I first the set the project up, it is set as an 'Empty Project' project type with 'MacOSX GCC' as the toolchain. I then created a source file (with the .cpp extension) within the project, wrote the code, and then that's where I'm at.
Any help would be greatly appreciated. Thanks!

Try resetting up your project. Sometimes installing buildchains can confuse eclipse and make it use the wrong libraries - especially if it is expecting another version.
What I think happened here is that eclipse confused was using a buildpath that did not contain C++ files (due to an upgrade moving them?) but instead contained C files, which don't link with C++ under normal circumstances.
As also mentioned by another commenter, this could also be the result of an initial failed build not being cleaned up for some reason.
Sometimes with eclipse, the solution is the same as with Windows: Restart it, redo whatever went wrong again.

Related

When output to the console, a "%" symbol is added in linux. C++

I use Manjaro Linux, DISTRIB_RELEASE=22.0.0, GNOME 43.1, Kernel 5.19.17-2, and I used zsh.
I decided to learn C++, but I ran into a problem. If I didn't add std::endl when outputting to the console, the symbol "%" is added.
See the screenshots attached.
Code1:
#include <iostream>
int main()
{
int age;
age = 28;
std::cout << "Age = " << age;
return 0;
}
Code2:
#include <iostream>
int main()
{
int age;
age = 28;
std::cout << "Age = " << age << std::endl;
return 0;
}
Why is this happening? All I tried was just adding std::endl. I want to know why the "%" symbol is being added.
Ah, you're omitting the final line break.
Your shell hence should (would it be very true to what your program actually produced in output) display the prompt on the same line as your output.
Now, that would look terrible and be confusing. So, instead your shell inserts a special character with a special background color to mark "hey, this isn't the program's output, but I'm still inserting a line break here, because I don't hate you, dear user".
That percentage symbol is not from your program. It's your shell trying to be sensible.
From the man page for zsh:
When a partial line is preserved, by default you will see an inverse+bold character at the end of the partial line: a % for a normal user or a # for root. If set, the shell parameter PROMPT_EOL_MARK can be used to customize how the end of partial lines are shown.

Cout unsigned char

I'm using Visual Studio 2019: why does this command do nothing?
std::cout << unsigned char(133);
It literally gets skipped by my compiler (I verified it using step-by-step debug):
I expected a print of à.
Every output before the next command is ignored, but not the previous ones. (std::cout << "12" << unsigned char(133) << "34"; prints "12")
I've also tried to change it to these:
std::cout << unsigned char(133) << std::flush;
std::cout << (unsigned char)(133);
std::cout << char(-123);
but the result is the same.
I remember that it worked before, and some of my programs that use this command have misteriously stopped working... In a blank new project same result!
I thought that it my new custom keyboard layout could be the cause, but disabling it does not change so much.
On other online compilers it works properly, so may it be a bug of Visual Studio 2019?
The "sane" answer is: don't rely on extended-ASCII characters. Unicode is widespread enough to make this the preferred approach:
#include <iostream>
int main() {
std::cout << u8"\u00e0\n";
}
This will explicitly print the character à you requested; in fact, that's also how your browser understands it, which you can easily verify by putting into e.g. some unicode character search, which will result in LATIN SMALL LETTER A WITH GRAVE, with the code U+00E0 which you can spot in the code above.
In your example, there's no difference between using a signed or unsigned char; the byte value 133 gets written to the terminal, but the way it interprets it might differ from machine to machine, basing on how it's actually set up to interpret it. In fact, in a UTF-8 console, this is simply a wrong unicode sequence (u"\0x85" isn't a valid character) - if your OS was switched to UTF-8, that might be why you're seeing no output.
You can try to use static_cast
std::cout << static_cast<unsigned char>(133) << std::endl;
Or
std::cout << static_cast<char>(133) << std::endl;
Since in mine all of this is working, it's hard to pinpoint the problem, the common sense would point to some configuration issue.

C++ Gets Identifier Not Found

In my old C++ project I use to use the gets() command. I've done my research and noticed that it is not reliable anymore and my project won't run with using it.
I use this bit of code right here:
Load(gets(new char[50]));
How would I now get this line of code to work properly? And if you could provide an explanation.
Here's a simple solution:
std::string text;
std::cout << "Enter some text to load: ";
std::getline(cin, text);
Load(text.c_str());
If you must use character arrays, here's a code fragment:
const size_t ARRAY_CAPACITY = 64U;
char text[ARRAY_CAPACITY];
std::cout << "Enter some text to load: ";
cin.getline(&text[0], ARRAY_CAPACITY);
Load(text);

G++ Compiles, Executable Won't Run

I've been using a text editor and g++ to compile and run programs for a while, but for some reason, it stopped working today.
It will compile without errors but when I try to run the executable, it doesn't do anything. No errors, no output. Nothing. This is the code I'm trying to run.
#include <iostream>
#include <fstream>
int main(){
// Initializes variable.
int coordinatePair;
// Creates an object to use for the file.
std::ofstream fileReader;
// Initiates a for-loop to get the value of each variable.
for(int i = 0; i == 5; i++){
std::cout << "Please enter the x-coordinate of your coordinate pair. " << std::endl << "You have " << 5 - i << " pairs left to enter." << std::endl;
std::cin >> coordinatePair;
// Opens the file.
fileReader.open("points.txt");
// Writes the user's values to the file.
fileReader << coordinatePair << std::endl;
// Closes the file.
fileReader.close();
}
}
In the terminal, I created a directory to the file location...
cd ~/file location
And then I compiled it.
g++ points_out.cpp -o points_out
And I tried to run it.
./points_out
And nothing happens. No error messages, no output, no nothing. While the code isn't exactly efficient (I'm probably better off opening and closing the file outside of the for-loop.) it should still run. I tried putting in incorrect code to see what would happen, and it gave me the proper error codes. I also tried
g++ -W -Werror points_out.cpp
...and that didn't give me any error codes either. I tried making a new directory to a different .cpp file I had compiled and ran earlier this week and it worked just fine. For some reason, this one just will not run. And I'm confident that it is compiling because the executable is being created. I'm also confident that it is not running, because the text file is not being created.
I got this thing to compile successfully and run once and it created the text file when it did. This is the first time I've run into issues with g++. I've looked everywhere for a solution but most of the people who had this problem just weren't doing the
./points_out
part or had issues with MingW or needed to uninstall Avast. None of these are the issue I have so I'm at a loss.
Also, not sure if this will help, but running
g++ --version
gets me the following output:
g++ (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
The behavior is normal for a program doing nothing and completing successfully.
The C++ for-loop evaluates the loop condition before entering the loop body. When the loop body is completed, the optional post-loop expression is evaluated and the program returns to the conditional check.
So your code assigns the value 0 to i and then tests to see if i == 5 before allowing entry into the loop.
It's not entirely clear what you were intending, but you probably mean't
for (int i = 0; i < 5; i++)
We can demonstrate that quickly with this piece of code:
#include <iostream>
int main() {
std::cout << "original loop:\n";
for (int i = 0; i == 5; i++)
std::cout << i << '\n';
std::cout << "corrected loop:\n";
for (int i = 0; i < 5; i++)
std::cout << i << '\n';
}
Live demo: http://ideone.com/O0Ul2z
-- Addendum --
Most programmers would have thought to add something like a no-frills, no-conditions
std::cout << "hello world\n";
at the top of their program to verify that it wasn't running vs not working.

Can't run a very basic code in Visual Studio 2013 - Operator ">>" error / build error

I have always used Linux Text Editor for C/C++ coding.
I'm totally new to Visual Studio. I downloaded Visual Studio 2013 and wrote a very basic code.
Before I tell you my problem, I think I should mention how I opened the new project so that it's not the matter of opening a wrong new project!
So here it is:
File > New > Project > Visual C++ > Win32 Console Application > ok > (Window appears saying "Welcome to Win32 Application wizard") > Next > checked the box saying "Empty Project" under Additional options > Finish
So then, I just right click on the "Source Files" > Add > New Item > C++ File(.cpp) > Add
Then my desired window appears so that I can write some code that should work!!
I wrote the following (very basic code) in Visual Studio 2013:
#include <iostream>
using namespace std;
int main()
{
cout << "What's your name?";
string name;
cin >> name;
cout << "Enter your age: ";
int age;
cin >> age;
cout << "In a decade, you will be " << age + 10 << "years old!" << endl;
return 0;
}
To run it, when I press "Local Windows Debugger", a box appears saying:
"There were build errors. Would you like to continue and run the last successful build?"
I pressed "Yes"
Then it shows another box saying "Unable to start the program....the system cannot find the file specified"
The following errors I get to see in the error list:
no operator ">>" matches the operands
operand types are: std::istream >> std::string
I would appreciate your help.
Thanks.
You forgot to include C++'s string library.
Adding
#include <string>
at the beginning of your file should make it work.
You can either do:
std::string name;
or include string class at the top:
#include <string>
You did not include string class like #include<string>
Use cin.getline(name) to take input in string otherwise it will take just a word as input, because ">>" operator ignore white spaces (space, tab etc..)
When you find this Box Message: "There were build errors. Would you like to continue and run the last successful build?" Do not press YES because it uses the last error free build to execute your program.