About getenv() on OSX - c++

I need to get the value of the environment variable ANDROID_HOME on OSX (set in .bash_profile). I can verify its existence by typing echo $ANDROID_HOME in the terminal.
Here is the code: (Xcode project)
void testGetEnv(const string envName) {
char* pEnv;
pEnv = getenv(envName.c_str());
if (pEnv!=NULL) {
cout<< "The " << envName << " is: " << pEnv << endl;
} else {
cout<< "The " << envName << " is NOT set."<< endl;
}
}
int main() {
testGetEnv("ANDROID_HOME");
}
The output is always The ANDROID_HOME is NOT set.. I don't think I'm using getenv() correctly here. Either that, or .bash_profile is not in effect when the getenv() is called.
What am I missing?

Your code seems correct - so you're most probably calling your program in an environment where ANDROID_HOME is indeed not set. How are you starting your program?
I changed your source code to actually be compilable, and it works fine on my OS X system:
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
void testGetEnv(const string envName) {
char* pEnv;
pEnv = getenv(envName.c_str());
if (pEnv!=NULL) {
cout<< "The " << envName << " is: " << pEnv << endl;
} else {
cout<< "The " << envName << " is NOT set."<< endl;
}
}
int main() {
testGetEnv("ANDROID_HOME");
}
Compile that with:
g++ getenv.cpp -o getenv
Now run:
./getenv
The ANDROID_HOME is NOT set.
export ANDROID_HOME=something
./getenv
The ANDROID_HOME is: something

Related

Using include <string> and using namespace std; but string still breaks my program

I'm following a tutorial for beginner C++ (https://www.udemy.com/course/free-learn-c-tutorial-beginners/learn/lecture/1368442#overview) and they very neatly wrote string text1 = "Hello" and then printed it. I tried this, then again with #include <string> and I'm still getting an error in the console. My code is here:
#include <iostream>
#include <string>
using namespace std;
int main() {
int numberCats = 5; // creates an int variable numberCats with value 5
int numberDogs = 1;
cout << "I have " << numberCats << " cats and " << numberDogs << " dogs." << endl; // prints
cout << "In total I have " << numberCats+numberDogs << " pets." << endl;
numberDogs += 1; // update variable
cout << "I just bought a dog. Now I have " << numberDogs << "." << endl;
string text1 = "Hello!" ; // creates a string variable (class)
cout << text1 << endl;
return 0;
}
And I get this message at the top of my console, and no output:
<terminated> (exit value: -1,073,741,511) udemy_course.exe [C/C++ Application] ...
If I remove the string line and the cout line underneath it, the program runs fine with this message at the top of the console:
<terminated> (exit value: 0) udemy_course.exe [C/C++ Application] ...
And the output:
I have 5 cats and 1 dogs.
In total I have 6 pets.
I just bought a dog. Now I have 2.
I feel like this should be REALLY easy, and can't figure out why it's not working at all. Everything I've looked up says to put #include and/or using namespace std; or change every string to std::string. None of this helps.
I'm using eclipse and minGW, pretty much exactly the same as the udemy course I'm following (except Windows, not Mac). Any help would be much appreciated.

VS code set up for c++

I am new in VS code. I wrote a C++ code like one below. but unfortunately in the terminal or output panel I cannot get both of the string and variable value. in the terminal only variable's inputted value is showing. How to fix this?
#include <bits/stdc++.h>
int main()
{
int slices;
std::cin >> slices;
std::cout << "You got " << slices << " of pizzas" << std::endl;
return 0;
}

Boost locale compare throws EXC_BAD_ACCESS

The following trivial usage of Boost's locale library generates a runtime error:
#include <boost/locale.hpp>
int main(int argc, const char * argv[]) {
boost::locale::generator gen;
std::locale loc = gen("");
std::locale::global(loc);
std::wstring a=L"Façade", b=L"facade";
bool eq = std::use_facet<boost::locale::collator<wchar_t>>(loc).compare(
boost::locale::collator_base::secondary,
a, b
) == 0;
if (eq) std::cout << "OK" << std::endl;
return 0;
}
The debugger traps the error in collator.hpp here:
int compare(level_type level,string_type const &l,string_type const &r) const
{
---> return do_compare(level,l.data(),l.data()+l.size(),r.data(),r.data()+r.size());
}
The error is "Thread 1: EXC_BAD_ACCESS (code=1, address=0x0)"
I'm on a late 2013 iMac running macOS Sierra version 10.12 with Xcode 8.0 (8A218a) and boost-1.61.0_1. In hopes that maybe I just failed to install ICU with Boost, I just ran this:
brew install boost --with-icu4c --cxx11
This sample is based on Boost's own documentation:
http://www.boost.org/doc/libs/1_49_0/libs/locale/doc/html/index.html
Thoughts? Thank you.
---- EDIT -----
I tried dumping out info about the default locale:
std::cout << std::use_facet<boost::locale::info>(loc).name() << std::endl;
It contained simply "C" and the other attributes were not what I expected. I thus got explicit in my setup:
boost::locale::generator gen;
std::locale loc = gen("en_US.UTF-8");
std::locale::global(loc);
std::cout << std::use_facet<boost::locale::info>(loc).name() << std::endl;
std::cout << std::use_facet<boost::locale::info>(loc).language() << std::endl;
std::cout << std::use_facet<boost::locale::info>(loc).country() << std::endl;
std::cout << std::use_facet<boost::locale::info>(loc).variant() << std::endl;
std::cout << std::use_facet<boost::locale::info>(loc).encoding() << std::endl;
std::cout << std::use_facet<boost::locale::info>(loc).utf8() << std::endl;
Which output:
en_US.UTF-8
en
US
utf-8
1
Sadly, this did not help. Also, I noticed that the following code didn't work as expected:
const std::string g = "grüßEN";
std::cout << boost::locale::normalize(g, boost::locale::norm_nfc) << std::endl;
std::cout << boost::locale::normalize(g, boost::locale::norm_nfd) << std::endl;
std::cout << boost::locale::normalize(g, boost::locale::norm_nfkc) << std::endl;
std::cout << boost::locale::normalize(g, boost::locale::norm_nfkd) << std::endl;
It output all the same strings:
grüßEN
grüßEN
grüßEN
grüßEN
This seem to suggest there's something screwy with my install of Boost or ICU.

Update: program shows adress of fstream instead of the text file

I am about to write a program which asks the user if they want to "search or convert" a file, if they choose convert, they need to provide the location of the file.
I do not know why the program shows the address of the file instead of opening it.
Here is my first approach:
#include <fstream>
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
char dateiname[64], kommando[64];
ifstream iStream;
cout << "Choose an action: " << endl <<
" s - search " << endl <<
" c - convert" << endl <<
" * - end program" << endl;
cin.getline(kommando,64,'\n');
switch(kommando[0])
{
case 'c':
cout << "Enter a text file: " << endl;
cin.getline(dateiname,64,'\n');
iStream.open("C://users//silita//desktop//schwarz.txt");
case 's': break;
case '*': return 0;
default:
cout << "Invalid command: " << kommando << endl;
}
if (!iStream)
{
cout << "The file " << dateiname << " does not exist." << endl;
}
string s;
while (getline(iStream, s)) {
while(s.find("TIT", 0) < s.length())
s.replace(s.find("TIT", 0), s.length() - s.find("TIT", 3),"*245$a");
cout << iStream << endl;
}
iStream.close();
}
At first you can't compare c-strings using ==. You must use strcmp(const char*, const char*). More info about it you can find there: http://www.cplusplus.com/reference/cstring/strcmp/
For example: if (i == "Konvertieren") must become if(!strcmp(i,"Konvertieren"))
As mentioned in Lassie's answer, you can't compare strings in this way using c or c++; just to flesh it out, however, I'll explain why.
char MyCharArr[] = "My Character Array"
// MyCharArr is now a pointer to MyCharArr[0],
// meaning it's a memory address, which will vary per run
// but we'll assume to be 0x00325dafa
if( MyCharArr == "My Character Array" ) {
cout << "This will never be run" << endl;
}
Here the if compares a pointer (MyCharArr) which will be a memory address, ie an integer, to a character array literal. Obviously 0x00325dafa != "My Character Array".
Using cstrings (character arrays), you need to use the strcmp() function which you will find in the cstring library, which will give you a number telling you "how different" the strings are, essentially giving the difference a numerical value. In this instance we are only interested in no difference, which is 0, so what we need is this:
#include <cstring>
using namespace std;
char MyCharArr[] = "My Character Array"
if( strcmp(MyCharArr,"My Character Array")==0 ) {
// If there is 0 difference between the two strings...
cout << "This will now be run!" << endl;
}
While you are not doing so in your question, If we were using c++ strings rather than character arrays, we would use the compare() method to similar affect:
#include <string>
using namespace std;
string MyString = "My C++ String"
if( MyString.compare("My C++ String")==0 ) {
// If there is 0 difference between the two strings...
cout << "This will now be run!" << endl;
}

error in compiling flex program with g++

Here is my simple lex file with main function inside it .
I want to compile it using g++ .
%{
#include <iostream>
%}
%%
[ \t] ;
[0-9]+\.[0-9]+ { cout << "Found a floating-point number:" << yytext << endl; }
[0-9]+ { cout << "Found an integer:" << yytext << endl; }
[a-zA-Z0-9]+ { cout << "Found a string: " << yytext << endl; }
%%
main() {
// lex through the input:
yylex();
}
I run following commands on my terminal
lex ex1.l
g++ lex.yy.c -lfl -o scanner
i get following error
cout lives in the std namespace. You need to refer to it as
std::cout
The same applies to endl.
Note, you can say
using std::cout;
using std::endl;
somewhere before using cout and endl names. You should be careful that there is no potential for name clashes when doing this. Use it in limited scopes, and not in header files.