string Comparison - c++

I want to compare two user input strings, but not able to do so...
#include "stdafx.h"
#include "iostream"
#include "string"
using namespace std;
int _tmain(int argc, _TCHAR* argv0[])
{
string my_string;
string my_string2;
cout<<"Enter string"<<endl;
cin>>my_string;
cout<<"Enter 2nd string"<<endl;
cin>>my_string2;
cout<<my_string<<" "<<my_string2;
strcmp(my_string,my_string2);
int result;
result= strcmp(my_string,my_string2);
cout<<result<<endl;
return 0;
}
This error is appearing.
Error 1 error C2664: 'strcmp' : cannot convert parameter 1 from 'std::string' to 'const char *' c:\users\asad\documents\visual studio 2008\projects\string\string\string.cpp 23 String

Since you're using std::string, strcmp is unnecessary -- you can just use <, ==, !=, etc.

Your includes:
Since you are including standard headers, they should be in <>
#include <string>
#include <iostream>
#include with "" is generally used for your own header files, not standard header files.
You are using C++, and therefore need not use strcmp. In C++, you can simply use == & != to compare two strings.
if (my_string == my_string2) result = 0;
else result = 1;
Also, if you want to convert a string to a const char*, you can use mystring.c_str()

If you want to use strcmp note that it takes different parameters than the ones you used.
http://www.cppreference.com/wiki/c/string/strcmp

Another way to do this is also
result= strcmp(my_string.c_str(),my_string2.c_str());

Related

Array strings C++

I don't remember how to do this in C++:
#include <iostream>
#include <conio.h>
#include <string.h>
int main(){
char categorias[3][20];
/*char pais[3][20];
char movimiento[3][50];
char obras[100][50]; */
categorias[0]="Alta";
categorias[1]="Media";
categorias[2]="Baja";
}
This throws this error: 19 15 C:\Users\dell\Desktop\Subasta.cpp [Error] incompatible types in assignment of 'const char [5]' to 'char [20]';
Long time ago I don't use C++ and I canĀ“t solve the problem.
Use C++ abstractions and containers from the standard library:
int main()
{
using namespace std::string_literals;
auto categorias = std::vector{"Alta"s, "Media"s, "Baja"s};
// Or if you know you have a fixed number of categories:
auto categorias = std::array{"Alta"s, "Media"s, "Baja"s};
}
To copy the string literal into the char array
strcpy(categorias[0], "Alta");

Check word function

i 've started learn c++ 1 week ago,i need an advice about how i can check entered word without function check_pass.How can i use if or while function for it please help.(Sorry for some mistakes )
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int enter_word;
cout<<"Hey bro what is your favourite color? ";
cin>>enter_word;
cout<<"So what is my favourite color? ";
if (enter_word="yellow"){ cout<<"Yep you are right bro!";}
system("pause");
return 0;
}
There are two major mistakes in the code you show: First is that enter_word is not a std::string object, it's an integer variable so can only contain integers. Secondly, you don't compare enter_word to "yellow" in the condition, you assign to the variable.
The first problem is solved by including <string> and declare enter_word as a string:
std::string enter_word;
The second problem can be solved by using the equality comparison operator == instead of assignment:
enter_word == "yellow"

Converting 'const char*' to 'LPCTSTR' for CreateDirectory

#include "stdafx.h"
#include <string>
#include <windows.h>
using namespace std;
int main()
{
string FilePath = "C:\\Documents and Settings\\whatever";
CreateDirectory(FilePath, NULL);
return 0;
}
Error: error C2664: 'CreateDirectory' : cannot convert parameter 1 from 'const char *' to 'LPCTSTR'
How do I make this conversion?
The next step is to set today's date as a string or char and concatenate it with the filepath. Will this change how I do step 1?
I am terrible at data types and conversions, is there a good explanation for 5 year olds out there?
std::string is a class that holds char-based data. To pass a std::string data to API functions, you have to use its c_str() method to get a char* pointer to the string's actual data.
CreateDirectory() takes a TCHAR* as input. If UNICODE is defined, TCHAR maps to wchar_t, otherwise it maps to char instead. If you need to stick with std::string but do not want to make your code UNICODE-aware, then use CreateDirectoryA() instead, eg:
#include "stdafx.h"
#include <string>
#include <windows.h>
int main()
{
std::string FilePath = "C:\\Documents and Settings\\whatever";
CreateDirectoryA(FilePath.c_str(), NULL);
return 0;
}
To make this code TCHAR-aware, you can do this instead:
#include "stdafx.h"
#include <string>
#include <windows.h>
int main()
{
std::basic_string<TCHAR> FilePath = TEXT("C:\\Documents and Settings\\whatever");
CreateDirectory(FilePath.c_str(), NULL);
return 0;
}
However, Ansi-based OS versions are long dead, everything is Unicode nowadays. TCHAR should not be used in new code anymore:
#include "stdafx.h"
#include <string>
#include <windows.h>
int main()
{
std::wstring FilePath = L"C:\\Documents and Settings\\whatever";
CreateDirectoryW(FilePath.c_str(), NULL);
return 0;
}
If you're not building a Unicode executable, calling c_str() on the std::string will result in a const char* (aka non-Unicode LPCTSTR) that you can pass into CreateDirectory().
The code would look like this:
CreateDirectory(FilePath.c_str(), NULL):
Please note that this will result in a compile error if you're trying to build a Unicode executable.
If you have to append to FilePath I would recommend that you either continue to use std::string or use Microsoft's CString to do the string manipulation as that's less painful that doing it the C way and juggling raw char*. Personally I would use std::string unless you are already in an MFC application that uses CString.

echo command using c++

I want to issue the echo command inside a c++ file.
All i need to do is
echo "xml::/var/some.xml" >> /var/config
In C++ file, I tried,
system("echo" + "xml::/var/some.xml" +">> /var/config");
But it throws invalid operands of types const char[6] and const char[46] to binary operator +.
Need help
I guess this is one where you may be able to get away with "can has codez?":
#include <iostream>
#include <iterator>
#include <algorithm>
int main(int argc, char **argv) {
std::copy(argv+1, argv+argc, std::ostream_iterator<char *>(std::cout, ""));
return 0;
}
You could just output the data yourself using stdio methods fopen/fputs/etc..
http://linux.die.net/man/3/fputs
Try
#include <string>
system(std::string("echo" + "xml::/var/some.xml" +">> /var/config").cstr())
you are passing raw character strings which do not support the + operator -- so try to use std::string instead.

C++ STL remove error

I'm having trouble understanding where I went wrong with my code:
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char* argv[]) {
string str = "";
cin >> str;
remove(str.begin(), str.end(), ' ');
cout << str;
cin.ignore();
}
The error says "'remove': function does not take 3 arguments (C2660)"
Try adding
#include <algorithm>
"algorithm" is an STL header containing a lot of functions, including std::remove, which the OP is trying to call. The error he got was because there is another function that takes a single argument, called "remove", which deletes a file.