C-string comparison need help to debug

I need some help, i do program that compares two cstrings, if they are >, <, or equal to each other.

|34|error: invalid conversion from 'char*' to 'char' [-fpermissive]|
|10|error: initializing argument 1 of 'short int stringcompare(char, char)' [-fpermissive]|


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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
 #include <iostream>
#include <climits>
#include <cstring>
#include <cctype>


using namespace std;

char get_upchar(const char s1[], const char s2[]);
short stringcompare(char s1, char s2);

int main(void)
{
 const short MAX_S = 20;
 char s1[MAX_S], s2[MAX_S];

    cout << "Enter two strings and i will tell you if they are the same\n";
	cout << "\nEnter first string: ";
	cin.getline(s1, MAX_S);

	cout << "\nEnter second string: ";
    cin.getline(s2, MAX_S);

    for (char *iter = s1; *iter != '\0'; ++iter)
   {
       *iter = std::toupper(*iter);
       ++iter;
   }
   for (char *iter = s2; *iter != '\0'; ++iter)
   {
       *iter = std::toupper(*iter);
       ++iter;
   }
    stringcompare(s1, s2);

         return 0;
}

short stringcompare(const char s1[], const char s2[])
{

    if (strcmp(s1, s2) == 0)
    {
        cout << "[" << s1 << "]\n equals \n[" << s2 << "]\n";
    }
    else if(strcmp(s1, s2) < 0)
    {
        cout << "[" << s1 << "] \nis less than \n[" << s2 << "]\n";
    }
    else if(strcmp(s1, s2) > 0)
        cout << "[" << s1 << "]\n is  bigger than \n[" << s2 << "]\n";

        return 0;
}
use
if ( std::strcmp(a,b) == 0)
or
if( std::memcmp(a,b , size) == 0)
i just did, but it still reports and error of invalid conversion from char* to char(((
do i need to change < or > signs probably?

short stringcompare(const char s1[], const char s2[])
{

if ( std::strcmp(s1, s2) == 0)
{
cout << "[" << s1 << "]\n equals \n[" << s2 << "]\n";
}
else if( std::strcmp(s1, s2) < 0)
{
cout << "[" << s1 << "] \nis less than \n[" << s2 << "]\n";
}
else if( std::strcmp(s1, s2) > 0)
cout << "[" << s1 << "]\n is bigger than \n[" << s2 << "]\n";

return 0;
Last edited on
short stringcompare(char s1, char s2); You are declaring stringcompare as function taking single characters, not pointers.
1
2
3
short stringcompare(char s1, char s2);

short stringcompare(const char s1[], const char s2[]) {}


does it look the same signature ?

it compiles now, but there are no cout for "equal", "less than" or "greater than"


#include <iostream>
#include <climits>
#include <cstring>
#include <cctype>


using namespace std;


short stringcompare(const char *s1, const char *s2);

int main(void)
{
const short MAX_S = 20;
char s1[MAX_S], s2[MAX_S];

cout << "Enter two strings and i will tell you if they are the same\n";
cout << "\nEnter first string: ";
cin.getline(s1, MAX_S);

cout << "\nEnter second string: ";
cin.getline(s2, MAX_S);

for (char *iter = s1; *iter != '\0'; ++iter)
{
*iter = std::toupper(*iter);
++iter;
}
for (char *iter = s2; *iter != '\0'; ++iter)
{
*iter = std::toupper(*iter);
++iter;
}
short stringcompare(const char *s1, const char *s2);

return 0;
}

short stringcompare(const char *s1, const char *s2)
{

if (std::strcmp(s1, s2) == 0)
{
cout << "[" << s1 << "]\n equals \n[" << s2 << "]\n";
}
else if(std::strcmp(s1, s2) < 0)
{
cout << "[" << s1 << "] \nis less than \n[" << s2 << "]\n";
}
else if(std::strcmp(s1, s2) > 0)
cout << "[" << s1 << "]\n is bigger than \n[" << s2 << "]\n";

return 0;
}
replace const char s1[] , const char s2[] by const char * s1 , const char * s2 . Maybe that the issue
You do not call your stringcompare function anywhere. Hint: you did call it in your original program
Thank you so much, it works now!!!
But output looks ugly(
I need to uppercase both strings, but instead it gives me this kind of output:

****
Enter two strings and i will compare them!

Enter first string: mary

Enter second string: peter

[MaRy] is less than [PeTeR]


***
What could make it wrong?

for (char *iter = s1; *iter != '\0'; ++iter)
{
*iter = std::toupper(*iter);
++iter;
}
for (char *iter = s2; *iter != '\0'; ++iter)
{
*iter = std::toupper(*iter);
++iter;
}
Last edited on
You are incrementing iter twice in each loop.
Thank you very much!!!!
Topic archived. No new replies allowed.