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"
#include <iostream>
#include <cctype>
#include <cstring>
usingnamespace std;
int main()
{
constint 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;
elseif (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?)
#include <iostream>
#include <cstring>
usingnamespace std;
int main()
{
constint 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";
elseif (cmpVal < 0)
cout << '"' << name1 << "\" comes before \"" << name2 << "\"\n";
else
cout << '"' << name1 << "\" is the same as \"" << name2 << "\"\n";
}