strcmp and strcpy error

Oct 3, 2008 at 9:41pm
hi, I have 3 std::string and I try to use strcmp and strcpy. but for both of them I've got this error "cannot convert parameter 1 from 'std::string' to 'const char *'"

where is the problem?
Last edited on Oct 3, 2008 at 9:42pm
Oct 3, 2008 at 9:49pm
post ur code. we cant do anything without code.
Oct 3, 2008 at 10:02pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
.
.
.
	int wflag = 1;
	string newPassword = "";

	if (db_run != 0 && pass_run != 0)
	{
		cout << "#" << endl
			 << "#ERROR --> No administrator account found." << endl
			 << "#You should create a new account." << endl;
		while(wflag != 0)
		{
			string pass1 = getpassword("#Please enter new password:");
			string pass2 = getpassword("#Please confirm the password:");
			if (strcmp(pass1, &pass2) != 0)
			{
				cout << "#ERROR --> The Passwords you entered did not match!" << endl
					 << "#Please try again" << endl;
				wflag = 1;
				continue;
			}
			else
			{
				cout << "#Account successfully created." << endl;
				strcpy(newPassword, &pass1);
				wflag = 0;
				break;
			}
		}
	}
.
.
.


getpassword function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
string getpassword(const string& prompt)
{
	string result;

	// Set the console mode to no-echo, not-line-buffered input
	DWORD mode, count;
	HANDLE ih = GetStdHandle(STD_INPUT_HANDLE);
	HANDLE oh = GetStdHandle(STD_OUTPUT_HANDLE);
	if (!GetConsoleMode(ih, &mode))
		throw runtime_error("getpassword: You must be connected to a console to use this program.\n");
	SetConsoleMode(ih, mode & ~(ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT));

	// Get the password string
	WriteConsoleA(oh, prompt.c_str(), prompt.length(), &count, NULL);
	char c;
	while (ReadConsoleA(ih, &c, 1, &count, NULL) && (c != '\r') && (c != '\n'))
	{
		if (c == '\b')
		{
			if (result.length())
			{
				WriteConsoleA(oh, "\b \b", 3, &count, NULL);
				result.erase(result.end() -1);
			}
		}
		else
		{
			WriteConsoleA(oh, "*", 1, &count, NULL);
			result.push_back(c);
		}
	}

	// Restore the console mode
	SetConsoleMode(ih, mode);

	return result;
}
Oct 4, 2008 at 8:44am
You dont use strcpy and strcmp with C++/stl strings.

1
2
3
4
5
6
7
8
string x;
cin>>x;
if(x=="yes") //legal with stl strings
//whatever

string x;
string y="Hello";
x=y; //legal with stl strings  


strcmp and strcpy only work for C-style strings
Oct 4, 2008 at 11:33am
To use them, you would:

strcmp(str.c_str(), str2.c_str);
Topic archived. No new replies allowed.