Sep 2, 2015 at 6:51pm UTC
use
if ( std::strcmp(a,b) == 0)
or
if ( std::memcmp(a,b , size) == 0)
Sep 2, 2015 at 6:55pm UTC
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 Sep 2, 2015 at 6:56pm UTC
Sep 2, 2015 at 7:07pm UTC
short stringcompare(char s1, char s2);
You are declaring stringcompare as function taking single characters, not pointers.
Sep 2, 2015 at 7:29pm UTC
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;
}
Sep 2, 2015 at 7:32pm UTC
replace const char s1[] , const char s2[] by const char * s1 , const char * s2 . Maybe that the issue
Sep 2, 2015 at 7:32pm UTC
You do not call your stringcompare
function anywhere. Hint: you did call it in your original program
Sep 2, 2015 at 7:40pm UTC
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 Sep 2, 2015 at 7:40pm UTC
Sep 2, 2015 at 7:42pm UTC
You are incrementing iter twice in each loop.