Sign in with
Sign up | Sign in
Your question

Help with a C++ program please

Last response: in Applications
Share
November 2, 2010 12:38:11 AM

If anybody has the time to spare to help me out with this I'd be very grateful. Been working on this for hours and can't figure out how to get rid of the last two errors.

The problem: Accept input of two strings using getline(), sort the strings, and use function: "bool isAnagram(const string &s1, const string &s2)" to return if the two strings are anagrams. Use a loop to continue checking anagrams until the user ends the loop.

My problem: The error that I'm having is saying that the compiler cannot convert std::string to char in the sortString function. I have no idea how to fix this without changing all of the code to use 'char', which would go against the directions.

My code:

#include <iostream>
#include <string>

using namespace std;

bool isAnagram(const string &s1, const string &s2);
void sortString(string s1, string s2);

int main()
{
string string1;
string string2;
string yorn;

cout << " Stacey Grieshop " << endl;

cout << endl;

cout << "Enter the first string : ";
getline(cin, string1);

cout << "Enter the second string: ";
getline(cin, string2);

cout << endl;

if (isAnagram(string1, string2))
cout << string1 << " and " << string2 << " are anagrams" << endl;
else
cout << string1 << " and " << string2 << " are not anagrams" << endl;

cout << endl;

cout << " Would you like to try another pair?" << endl;
cout << " Y to continue, any other key to quit : ";

getline(cin, yorn);


while (yorn == "y") {

cout << endl;

cout << "Enter the first string : ";
getline(cin, string1);

cout << "Enter the second string: ";
getline(cin, string2);

cout << endl;

if (isAnagram(string1, string2)) {
cout << string1 << " and " << string2 << " are anagrams" << endl; }
else {
cout << string1 << " and " << string2 << " are not anagrams " << endl; }

cout << endl;

cout << " Would you like to try another pair?" << endl;
cout << " Y to continue, any other key to quit : ";
getline(cin, yorn);
}

return 0;
}


bool isAnagram(const string &s1, const string &s2)
{
sortString(s1, s2);

if (s1 == s2)
return true;
else
return false;
}

void sortString(string s1, string s2)
{
int i;
int j;
string temp;
int size1;
int size2;

size1 = s1.length();
size2 = s2.length();

for (i = 0; i < size1; i++) {
if (s1.at(i) > s1.at(i + 1)) {
temp = s1.at(i);
s1.at(i) = s1.at(i+1);
s1.at(i+1) = temp; } }

for (i = 0; i < size2; i++) {
if (s2.at(i) > s2.at(i + 1)) {
temp = s2.at(i);
s2.at(i) = s2.at(i+1);
s2.at(i+1) = temp; } }

}

--------------------------------------------------

I'm not asking for anybody to write the code for me, just nudge me in the right direction?

More about : program

a b L Programming
November 2, 2010 7:43:41 AM

At a few places in the fuction you are assigning to or from "temp", which is a string, the destination being a character. The error is saying that you cannot assign a string to a char variable, and there is no way to automatically coerce the string to a char.

Just declare "temp" as char rather than string and the program will compile. (I'm not saying it will necessarily work, but it will compile without error.)
!