/*************************************************
Student: xxxxx
ID number: xxxxx
Instructor: Dr. Julstrom
Class: CSCI 301
Project 1
*************************************************/
#include <iostream>
#include <cmath>
#include <cctype>
using namespace std;
const int SIZE = 61;
int getinput(char []);
void compare(char [], char [], int, int);
/*************************************************
The purpose of this program is to have a user input
2 sets of different characters, either let it be just
plain characters or a sentence of a mixture of everything,
the program will collect the alphabetical character
and return a state whether the characters match or not
regardless of order.
*************************************************/
int main()
{
char sentence1[SIZE], sentence2[SIZE];
/*************************************************
a void type function compare is called passing 2
arrays and 2 integer as parameter. The parameter
getinput(sentence1) and getinput(sentence2) will
be evaluated separately.
*************************************************/
compare(sentence1, sentence2, getinput(sentence1), getinput(sentence2));
return 0;
}
int getinput(char sentence[])
{
/*************************************************
The sentence array will store any value input by
the user that is a alphabetical character and keep
track of how many time it is stored by the integer i.
the returning value after the user hits the enter key
is i.
*************************************************/
char ch;
int i = 0;
cout << "enter something => ";
cin.get(ch);
int count = 0;
while(ch >= ' ' && count < SIZE)
{
if (isalpha(ch))
{
sentence[i++] = ch;
}
cin.get(ch);
count++;
}
return i;
}
void compare(char sentence1[], char sentence2[], int i, int k)
{
/*************************************************
2 local character array is declared along with misc
counters: a and b will increment store1 and store2
while j will increment if a and b are incremented.
another set of counters are also declared to count
the arrays in the function parameter.
*************************************************/
char store1[SIZE];
char store2[SIZE];
int a = 0, b = 0, j = 0;
int count = 0;
int count2 = 0;
/*************************************************
The first test, if i and k is zero from the pass
function parameter, then the input from the user
is indeed an anagram.
*************************************************/
if(i == 0 && k == 0)
{
cout << "test 1" << endl; //test
cout << "The stuff you typed in is an anagram" << endl;
exit(1);
}
/*************************************************
while count(initially 0) is less than
i(integer passed by parameter);
*************************************************/
while( count < i)
{
if((count2 >= i && count == 0) && j == 0)
{
cout << "test 2" << endl; //test
cout << "The stuff you typed in is not an anagram" << endl;
exit(1);
}
if(sentence1[count] != sentence2[count2])
{
count2++;
}
else if(sentence1[count] == sentence2[count2])
{
store1[a] = sentence1[count];
store2[b] = sentence2[count2];
a++; b++; j++;
count++;
count2++;
}
}
int p = 0;
if(j == (i) && j == (k))
{
while(store1[p] == store2[p])
{
if(p == i && (store1[p] == store2[p]))
{
cout << "test 3" << endl; //test
cout << "The stuff you typed in is an anagram" << endl;
exit(1);
}
p++;
}
}
cout << "test 4" << endl; //test
cout << "The stuff you typed in is not an anagram" << endl;
}
When I put in the input
"op"
and
"ok"
it is not giving me the right answer. Any help will be greatly appreciated.
Oh yeah, the problem is in the compare function.