Please tell me why this doesn't work:
#include<iostream>
using namespace std;
int main()
{
char felix[3] = "ho";
if(felix == "ho")
cout << felix;
}
Please tell me why this doesn't work:
#include<iostream>
using namespace std;
int main()
{
char felix[3] = "ho";
if(felix == "ho")
cout << felix;
}
You can't use the comparison-operator (==) on char-arrays to check if the arrays contain the same values.
Two ways around this:
Use std::strings (personal favorite):
std::string felix = "ho";
if(felix == "ho")
cout << felix;
Use strcmp
char felix[3] = "ho";
if(strcmp(felix, "ho") == 0) //are the same
cout << felix;
if(felix == "ho")
Change this line to if(strcmp(felix,"ho") == 0)
Also make sure you put return 0; at the end of your int main function.
You're confusing the lax C++string container method with the C way.
http://www.thinkage.ca/english/gcos/expl/c/lib/strcmp.html
http://www.cplusplus.com/reference/string/operators/
edit:
Wow, everyone replied at the same time.
You're confusing the lax C++string container method with the C way.
http://www.thinkage.ca/english/gcos/expl/c/lib/strcmp.html
http://www.cplusplus.com/reference/string/operators/edit:
Wow, everyone replied at the same time.
Yep, they sure did. What the hell, I'll post mine anyway.
#include<iostream>
using namespace std;
int main()
{
char felix[3] = "ho";
if(felix == "ho")
cout << "1. This will not execute\n";
if(strcmp (felix, "ho") == 0)
cout << "2. This will execute.\n";
string felix2 = "ho";
if(felix2 == "ho")
cout << "3. This will execute.\n";
char* felix3 = felix;
cout << "Address felix points to " << (void*) felix << "\n";
cout << "Address felix3 points to " << (void*) felix3 << "\n";
if (felix == felix3)
cout << "4. This will execute.\n";
return 0;
}
Ok, thanks for the advice guys.
Another question: If I were to switch to a string array, is possible to store this into a file?
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
string name[2];
ifstream fromfile("test.txt");
for(int x=0;x<2;x++)
fromfile >> name[x];
fromfile.close();
ofstream tofile("test.txt");
for(int x=0;x<2;x++)
tofile << name[x] << endl;
tofile.close();
if(name[0]!="Gof")
cout << name[0];
cout << name[0];
}
Edit: Just realized that I didn't put an 'if' statement in to verify whether the file is operable. -- minor implication.
edit:
Wow, everyone replied at the same time.
It's nice to see that we all agree with each-other :)
>> Another question: If I were to switch to a string array, is possible to store this into a file?
Yes:
ofstream myfile ("example.txt");
std::string bla = "something";
if (myfile.is_open())
myfile << bla;
A constant sized array is fine if you only ever require that size. Check out <vector> if you want a dynamic solution.
Alright, I decided not to switch to a string. Thanks for the advice. If I have further questions, I will ask later. Topic solved.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.