Afzaal Ahmad Zeeshan
Afzaal Ahmad Zeeshan

Reputation: 15860

Why isn't value matching up?

I am having a C++ console application code, which would look up for the condition and then save the text to the (.txt) file.

But the condition is not being applied here. What I have in condition is this:

char command[100];
cin >> command;
if (command == "save") {
    fstream file;
    file.open("C:\\Users\\AfzaalAhmad\\Documents\\text.txt");
    file << "Data you provided was as saved!";
    cout << "File Saved!";
}
else {
    cout << "Ummm, sir I think there is an error!\n" << 
    "The command you entered was: " << command;
}

What I am doing is, to check the command provided by the user, if the command is 'save'

if(command == "save") 

then save the data to a file, in the file the data is present when I use this code:

if(command != "save") 

Because the command doesnot have to be save, the code executes and gives me the data in the file that is in documents folder.

However, if the else block gets executed, I can still see the correct command 'save' at the end, which is being shown by the code as you can see in the code block.

enter image description here

You can see there, I am using the correct command, but it is not executing as if(command == "save") but it executing if I use this code if(command != "save").

Any guidance please?

Upvotes: 0

Views: 73

Answers (7)

αλεχολυτ
αλεχολυτ

Reputation: 5039

For comparing c-strings you should use strcmp() function.

Upvotes: 0

Vlad from Moscow
Vlad from Moscow

Reputation: 310930

In this condition

if (command == "save") 

you are trying to compare character arrays. But neither C nor C++ has the comparison operator for arrays. Instead this the string literal and the array in the left part of the operation are implicitly converted to pointers to their first characters and in fact you are comparing two pointers that will be always unequal. To compare character arrays you should use standard C function std::strcmp that is declared in header <cstring>( in C++ ) or <string.h> (in C)

For example

if ( std::strcmp( command, "save" ) == 0 ) 

Upvotes: 2

Mats Petersson
Mats Petersson

Reputation: 129314

Because you should use strcmp to compare C style strings. Or use std::string, which is also better in many other ways, besides being able to compare strings with ==.

A C style string constant is just the address to some memory that holds that particular string, so when you compare command == "save", then for that comparison to be true, command must have the same address as the constant string "save" - which clearly isn't the case ever.

Upvotes: 1

Nikos Athanasiou
Nikos Athanasiou

Reputation: 31489

command is a char array, you cannot compare it to a string literal like that (you are actually comparing adresses). If you don't want to use std::string that overload the == operator you could say

strcmp(command, "save"); // it returns zero if equal

Upvotes: 1

Futile
Futile

Reputation: 133

The variable command is of type char*, because an array in C/C++ is (basically) a pointer (and a fixed maximum array size). Since you are using C++, consider using std::string instead: http://www.cprogramming.com/tutorial/string.html.

What you are using is a C-Style string, and you have to use functions such as strcmp, for example described here: How do I properly compare strings?. But that is the C-way, and, if you're writing C++, you should stick to the std::string class for such purposes.

Upvotes: 0

Joseph Mansfield
Joseph Mansfield

Reputation: 110648

command and the string literal "save" are both arrays of chars. They are both being converted to pointers to the first elements (this is called array-to-pointer conversion) and you are comparing those pointers. So what you're really doing with command == "save" is checking if their first characters have the same address.

Instead, to compare C-style strings like you want (checking if all characters in the strings are the same), you need to use std::strcmp. However, a much more preferable solution in C++ is to make command a std::string, instead of an array of char. std::string supports comparison using ==.

Upvotes: 6

Ben Voigt
Ben Voigt

Reputation: 283624

You are comparing the address of command to the address of a string literal. They will never be the same.

Possible solutions:

  • use the C library function strcpy: if (0 == strcmp(command, "save"))
  • cast one to a C++ string: if (std::string(command) == "save)
  • Use a C++14 string literal: if (command == "save"s)
  • Store the user's input into a C++ string to begin with.

The last of these is the best option, and looks like:

std::string command;
cin >> command;
if (command == "save") {

Upvotes: 0

Related Questions