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.
Related
This question already has answers here:
Understanding the output of typeid().name()
(2 answers)
Checking cin input stream produces an integer
(8 answers)
Closed 18 days ago.
Trying to test if a particular input matches the ideal data type, i.e. if I get an input check if it an int.
#include <iostream>
using namespace std;
int main() {
string integer;
cin >> integer;
if (typeid(stoi(integer)).name() != "i") {
cout << "Bad input";
}
return 0;
}
But it always says that it is not equal to "i".
Tried changing to 'i', haven't found any other functions that might work, open to suggestions.
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.
}
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.
This question already has answers here:
std::string length() and size() member functions
(4 answers)
Closed 7 years ago.
In this exercise from C++ Primer strings chapter, the directions read:
Write a program to read two strings and report whether the strings are
equal. If not, report which of the two is larger. Now, change the
program to report whether the strings have the same length, and if
not, report which is longer.
To which I wrote:
#include <iostream>
#include <string>
using std::string;
using std::cin;
using std::cout;
using std::endl;
/* Write a program to read two strings and report whether the
strings are equal. If not, report which of the two is larger. Now, change
the program to report whether the strings have the same length, and if
not, report which is longer. */
int main()
{
string line;
string word;
getline(cin, line);
getline(cin, word);
if (line == word) {
cout << "String are equal." << endl;
}
else {
if (line > word)
cout << line << " is longer." << endl;
else {
cout << word << " is longer." << endl;
}
}
return 0;
}
Which seemed to work for the problem. Now for the first example of:
Write a program to read two strings and report whether the strings are
equal. If not, report which of the two is larger.
I changed the comparisons to have .size(), and for this one:
Write a program to read two strings and report whether the strings are
equal. If not, report which of the two is larger.
I removed the .size() for the comparisons. I printed out both size and .length() and I got the same answers, so I was wondering if I am misinterpreting this problem or was it to show that length and size are really the same thing?
Both string::size and string::length are synonyms and return the exact same value.
Reference http://www.cplusplus.com/reference/string/string/length/
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).