| | |
logic error that i cannot find... HELP?
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Jan 2009
Posts: 62
Reputation:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
/************************************************* 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.
Last edited by homeryansta; Jan 21st, 2009 at 1:14 am.
•
•
Join Date: Jan 2009
Posts: 62
Reputation:
Solved Threads: 0
•
•
•
•
>it is not giving me the right answer
and what is the right answer?? what is the program supposed to be doing?? The header doesn't say what it does and i don't want to check the code to find that out.
sorry, the code is suppose to get inputs from the user. It will then store the alphabetical character into an array ignoring all other character and compare to see if they have the same alphabetical characters in there. If it does, it will say
"it is an anagram"
if not, it'll say
"not and anagram"
example
"abc123" and "bac" are anagram
"kkk" and "bbb" are not anagram
>It will then store the alphabetical character into an array ignoring all other character
>compare to see if they have the same alphabetical characters in there.
so first you fetch the alphabets from the input and store it in an array and then check array for alphabets again?? it doesn't make sense to me
do you mean if the alphabets stored in array make any word it will say its an 'anagram'?
>compare to see if they have the same alphabetical characters in there.
so first you fetch the alphabets from the input and store it in an array and then check array for alphabets again?? it doesn't make sense to me
do you mean if the alphabets stored in array make any word it will say its an 'anagram'?
thanks
-chandra
-chandra
•
•
Join Date: Jan 2009
Posts: 62
Reputation:
Solved Threads: 0
•
•
•
•
>It will then store the alphabetical character into an array ignoring all other character
>compare to see if they have the same alphabetical characters in there.
so first you fetch the alphabets from the input and store it in an array and then check array for alphabets again?? it doesn't make sense to me
do you mean if the alphabets stored in array make any word it will say its an 'anagram'?
the user will be asked to enter a sentence(or anything)
then the user will be asked to enter another sentence.
It will then compare the 2 sentence to see if they have the same characters.
If my explanation doesn't do the job then here is the assignment, hope it is more clear.
TITLE
DETECTING ANAGRAMS
INTRODUCTION
Two or more strings are anagrams if they contain exactly the same letters, ignoring capitalization, punctuation, and spaces. For example, "Information superhighway" and "New utopia? Horrifying sham" are anagrams, as are "ab123ab" and "%%b b*aa". Note that two lines that contain no letters are anagrams.
DESCRIPTION
Write a program that reads two lines of input from the terminal and determines and reports if the lines are anagrams.
INPUT
Input lines will consist of letters, blanks, and punctuation, like this: "New utopia? Horrifying sham". The maximum length of an input line is 60 characters.
OUTPUT
The program will prompt for two input lines and will report whether or not two lines are anagrams.
ERRORS
Your program may assume that the input is as described; it need not detect any errors.
EXAMPLE
Several runs of the program might look like this:
csh> agm
Enter two lines that might be anagrams:
--> Eleven plus two
--> Twelve plus one
The two strings are anagrams.
csh> agm
Enter two lines that might be anagrams:
--> This is a string.
--> Another string
The two strings are NOT anagrams.
csh> agm
Enter two lines that might be anagrams:
--> Snooze alarms
--> Alas, no more Z's.
The two strings are anagrams.
Last edited by homeryansta; Jan 21st, 2009 at 1:51 am.
•
•
Join Date: Jan 2009
Posts: 62
Reputation:
Solved Threads: 0
the compare function compares the 2 inputs to see if they contain the same alphabetical characters ignoring numbers, punctuations, etc.
if they match up, it'll say: inputs are anagram.
if they don't match up, it'll say: inputs are not anagram.
when I type in "ok" for the first input and "op" for the second input, it tells me that the "inputs are anagram". It should be "inputs are not anagram".
I put in "test 1" to "test 4" to print onto the screen to see where the program is ending so I have an idea of where to look. It lead me to "test3" which is on line 140.
sorry for the confusion.
if they match up, it'll say: inputs are anagram.
if they don't match up, it'll say: inputs are not anagram.
when I type in "ok" for the first input and "op" for the second input, it tells me that the "inputs are anagram". It should be "inputs are not anagram".
I put in "test 1" to "test 4" to print onto the screen to see where the program is ending so I have an idea of where to look. It lead me to "test3" which is on line 140.
sorry for the confusion.
I would say you can use the following algorithm
1> convert both the arrays to all upper case or all lower case
2> sort both the arrays
3> compare the contents
4> at first mismatch print 'its not an anagram' and exit
5> else print 'its an anagram'
There will be many shorter one's but this is easy to implement.
1> convert both the arrays to all upper case or all lower case
2> sort both the arrays
3> compare the contents
4> at first mismatch print 'its not an anagram' and exit
5> else print 'its an anagram'
There will be many shorter one's but this is easy to implement.
Last edited by Agni; Jan 21st, 2009 at 2:19 am. Reason: small changes
thanks
-chandra
-chandra
•
•
Join Date: Jan 2009
Posts: 62
Reputation:
Solved Threads: 0
•
•
•
•
I would say you can use the following algorithm
1> convert both the arrays to all upper case or all lower case
2> sort both the arrays
3> compare the contents
4> at first mismatch print 'its not an anagram' and exit
5> else print 'its an anagram'
There will be many shorter one's but this is easy to implement.
![]() |
Similar Threads
- Logic error, simple stat prog (C++)
- small logic error (C++)
- Hardware Interrupts & 100% CPU usage (Windows NT / 2000 / XP)
- Stacks, Queues, and a Maze?? (C++)
- Magic Square - Error Message (C++)
- Rational Number Constructor (C++)
- Could someone please help me with this logic error?? (VB.NET)
- Program Compiles fine, but error upon Running (Java)
Other Threads in the C++ Forum
- Previous Thread: Problem to find smallest no divisible by all of the first 20 natural numbers
- Next Thread: what is wrong with my stupid program!?
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





