Char and pointer issue

I'm just starting in c++ and im sure I only missed this when we went over it in class but I keep running into this error and haven't been able to find it online. The assingment is to create a program that prompts the user to enter 2 names and then the program puts them into alphabetical order. I keep getting the error
" error C2664: 'int strcmp(const char *,const char *)' : cannot convert argument 1 from 'char' to 'const char *'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast"

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
  #include <iostream>
#include <cctype>
#include <cstring>
using namespace std;

int main()
{ 
	const int MAX = 25;
	char name1 [MAX+1];
	char name2 [MAX+1];
	int length1;

	cout<<"Please enter Name 1. Last, then first, seperated by a comma"<<endl;
	cin.get(name1, 25);
	

	cout<<"Please enter Name 2. Last, then first, seperated by a comma"<<endl;
	cin.get(name2, 25);
	

	length1 = strlen(name1);

	for(int count=0;count<length1;count++)
	{
		strcmp(name2[count], name2[count]);
		if (strcmp<0)
			cout<<name2<<endl
			<<name1<<endl; 
		else if (strcmp>0)
			cout<<name1<<endl
			<<name2<<endl;
	}
	return 0;
}
strcmp compares C-style strings. It does not compare single characters. Thus, the parameters to strcmp are pointers to const char as is befitting for a C-style string.

array_name[i] is the element of array array_name at index i. In an array of char, that element is a char. So, name2[count] is a single char. (And btw, why are you trying to compare a thing to itself?)

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
#include <iostream>
#include <cstring>
using namespace std;

int main()
{
    const int MAX = 25;
    char name1[MAX];
    char name2[MAX];
    int length1;

    cout << "Please enter Name 1. Last, then first, seperated by a comma" << endl;
    cin.getline(name1, MAX);    // getline removes the delimiter, so the next input operation
                                // doesn't run into it right away.

    cout << "Please enter Name 2. Last, then first, seperated by a comma" << endl;
    cin.getline(name2, MAX);

    int cmpVal = strcmp(name1, name2);  // capture the value returned by strcmp.

    if (cmpVal > 0)
        cout << '"' << name1 << "\" comes after \"" << name2 << "\"\n";
    else if (cmpVal < 0)
        cout << '"' << name1 << "\" comes before \"" << name2 << "\"\n";
    else
        cout << '"' << name1 << "\" is the same as \"" << name2 << "\"\n";
}

Topic archived. No new replies allowed.