If statement is not evaluating correctly [duplicate] - c++

This question already has answers here:
Using the equality operator == to compare two strings for equality in C [duplicate]
(9 answers)
Why is "a" != "a" in C?
(11 answers)
Closed 8 years ago.
I am working on a console project and I just can't get this part to work.
void execute(char* argv[]) {
char* printex = "print";
if (argv[1] == printex) {
print(argv);
}
else {
cout << "Unknown function." << endl;
}
}
Every time I type in "print" for argv[1], it thinks I have typed in something else. I tried putting in
cout << argv[1];
and the output was print. Why is it not working then?

argv[1] is a char*, so is printex. Comparing them will compare the address they contain, not the actual string. So they will never be same. You can use std::string (which is safer), or in the current form, use strcmp for comparison.
if( strcmp( argv[1], printex) == 0 )
//mathced

Try to replace if (argv[1] == printex) with if (strcmp(argv[1], printex)==0) to compare strings (not pointers on strings).

Related

C++ range based loops - How do they work? [duplicate]

This question already has answers here:
How does the range-based for work for plain arrays?
(6 answers)
C++11 range based loop: How does it really work
(3 answers)
Closed 2 years ago.
I'll preface this question by saying I am knowledgeable on C, and I'm learning C++.
Suppose I've got an std::string object and I want to go thru all of its characters to verify it (for example, only 4 letters allowed).
I have seen for (type var : array) being used in some code around a week ago, but I couldn't remember how it was used. I'm wondering.. exactly how does this work, and why does it work? Logically it shouldn't.
AFAIK, arrays don't really "know" what their length is. So suppose I have this code:
int arr[] = { 1, 2, 3 };
for (int item : arr) {
std::cout << item << std::endl;
}
How the hell does the for loop know when to stop? Arrays are simply put allocated memory (stack, in this case) that start from a specific address and go on. There's no "stored length" (AFAIK) for arrays. This is why it is a programmer's responsibility to store the length of the array, and do for (int i = 0; i < [length]; i++) but this somehow works?
Would love some explanation. Specific use case for my code:
std::string test = "Hello World!";
for (char letter : test) {
if (letter == 'l') {
std::cout << "'l' exists!" << std::endl;
} // result should be 3 prints.
}

C++ String Comparison "Good Luck" > "Good Bye" [duplicate]

This question already has answers here:
C++ Comparison of String Literals
(8 answers)
Closed 4 years ago.
I am comparing two strings "Good Luck" > "Good Bye" in c++. Shouldn't the boolean result be true? The 'L' is greater than the 'B' The blank space between the words is one whitespace.
However, I am getting false with run the code.
Do you know why? Below is my code.
#include <iostream>
using namespace std;
int main()
{
bool result = "Good Luck" > "Good Bye!";
cout << result;
return 0;
}
You're comparing char*s, which is just comparing the memory addresses that the strings live at.

Concatenating string and int in c++ [duplicate]

This question already has answers here:
Why in the code "456"+1, output is "56" [duplicate]
(3 answers)
Closed 6 years ago.
I am doing some exercises in C++ when I came upon something not so clear for me:
cout << "String" + 1 << endl;
outputs : tring
I suggest it is something with pointer arithmetic, but does that mean that everytime I print something in quotes that is not part of previous defined array,I actually create a char array ?
A quoted string (formally a string literal) is an array of const char, regardless of whether your printing it or doing anything else with it.
Code:
cout << "String" + 1 << endl;
has the same effect as this:
const char *ptr = "String";
cout << ptr + 1 << endl;
so no you do not create a new array, you just change pointer and pass it to std::cout

How can I compare C-style strings? [duplicate]

This question already has answers here:
Compare equality of char[] in C
(5 answers)
Closed 6 years ago.
The for loop reverses original string 's' and stores it in 'temp.'Temp is printed correctly. After which, s and temp are compared, but the result always shows NO. :(
#include<cstring>
#include <iostream>
using namespace std;
int main()
{
char s[100], temp[100];
cin >> s;
cout << strlen(s);
for(int i=0;i<strlen(s);i++)
{
temp[i]=s[strlen(s)-i];
}
cout << "temp is" << temp;
if(temp==s)
{
cout << "YES";
}
else
{
cout << "NO";
}
return 0;
}
You should use strcmp instead of == because the latter is merely a pointer comparison. Also, '\0' has to be used to end your string. And your current code would actually create an empty string because your string at position strlen(s) contains '\0'.
Then again, you should not work with char arrays in C++ on your own, rather use std::string as already pointed out in comments.

How can I seperate a string into individual characters in c++? [duplicate]

This question already has answers here:
For every character in string
(10 answers)
Closed 8 years ago.
As the title says, how can I seperate a string into individual characters in c++? For example, if the string is "cat" how can I separate that into the characters c,a, and t?
Thanks
By using the operator[]. e.g.
std::string cat{"cat"};
if(cat[0] == 'c')
//stuff
If you're using std::string you can simply use .c_str( ) that will give you an array of the chars.
In c++11 you could also do:
for( auto c : a )
{
cout << c << '\n';
}
http://ideone.com/UAyxTo
If you want to store them in a vector:
string str("cat");
vector<char> chars(str.begin(), str.end());
for (char c : chars)
cout << c << endl;