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: homeryansta is an unknown quantity at this point 
Solved Threads: 0
homeryansta homeryansta is offline Offline
Junior Poster in Training

logic error that i cannot find... HELP?

 
0
  #1
Jan 21st, 2009
  1. /*************************************************
  2. Student: xxxxx
  3. ID number: xxxxx
  4. Instructor: Dr. Julstrom
  5. Class: CSCI 301
  6. Project 1
  7. *************************************************/
  8.  
  9. #include <iostream>
  10. #include <cmath>
  11. #include <cctype>
  12. using namespace std;
  13.  
  14. const int SIZE = 61;
  15.  
  16. int getinput(char []);
  17. void compare(char [], char [], int, int);
  18.  
  19.  
  20. /*************************************************
  21. The purpose of this program is to have a user input
  22. 2 sets of different characters, either let it be just
  23. plain characters or a sentence of a mixture of everything,
  24. the program will collect the alphabetical character
  25. and return a state whether the characters match or not
  26. regardless of order.
  27. *************************************************/
  28. int main()
  29. {
  30. char sentence1[SIZE], sentence2[SIZE];
  31.  
  32.  
  33. /*************************************************
  34. a void type function compare is called passing 2
  35. arrays and 2 integer as parameter. The parameter
  36. getinput(sentence1) and getinput(sentence2) will
  37. be evaluated separately.
  38. *************************************************/
  39. compare(sentence1, sentence2, getinput(sentence1), getinput(sentence2));
  40.  
  41. return 0;
  42. }
  43.  
  44. int getinput(char sentence[])
  45. {
  46.  
  47.  
  48. /*************************************************
  49. The sentence array will store any value input by
  50. the user that is a alphabetical character and keep
  51. track of how many time it is stored by the integer i.
  52. the returning value after the user hits the enter key
  53. is i.
  54. *************************************************/
  55. char ch;
  56. int i = 0;
  57. cout << "enter something => ";
  58. cin.get(ch);
  59. int count = 0;
  60. while(ch >= ' ' && count < SIZE)
  61. {
  62. if (isalpha(ch))
  63. {
  64. sentence[i++] = ch;
  65. }
  66. cin.get(ch);
  67. count++;
  68. }
  69. return i;
  70. }
  71.  
  72. void compare(char sentence1[], char sentence2[], int i, int k)
  73. {
  74.  
  75.  
  76. /*************************************************
  77. 2 local character array is declared along with misc
  78. counters: a and b will increment store1 and store2
  79. while j will increment if a and b are incremented.
  80.  
  81. another set of counters are also declared to count
  82. the arrays in the function parameter.
  83. *************************************************/
  84. char store1[SIZE];
  85. char store2[SIZE];
  86. int a = 0, b = 0, j = 0;
  87. int count = 0;
  88. int count2 = 0;
  89.  
  90.  
  91. /*************************************************
  92. The first test, if i and k is zero from the pass
  93. function parameter, then the input from the user
  94. is indeed an anagram.
  95. *************************************************/
  96. if(i == 0 && k == 0)
  97. {
  98. cout << "test 1" << endl; //test
  99. cout << "The stuff you typed in is an anagram" << endl;
  100. exit(1);
  101. }
  102.  
  103.  
  104. /*************************************************
  105. while count(initially 0) is less than
  106. i(integer passed by parameter);
  107. *************************************************/
  108. while( count < i)
  109. {
  110.  
  111. if((count2 >= i && count == 0) && j == 0)
  112. {
  113. cout << "test 2" << endl; //test
  114. cout << "The stuff you typed in is not an anagram" << endl;
  115. exit(1);
  116. }
  117.  
  118.  
  119. if(sentence1[count] != sentence2[count2])
  120. {
  121. count2++;
  122. }
  123. else if(sentence1[count] == sentence2[count2])
  124. {
  125. store1[a] = sentence1[count];
  126. store2[b] = sentence2[count2];
  127. a++; b++; j++;
  128. count++;
  129. count2++;
  130. }
  131.  
  132. }
  133. int p = 0;
  134. if(j == (i) && j == (k))
  135. {
  136. while(store1[p] == store2[p])
  137. {
  138. if(p == i && (store1[p] == store2[p]))
  139. {
  140. cout << "test 3" << endl; //test
  141. cout << "The stuff you typed in is an anagram" << endl;
  142. exit(1);
  143. }
  144. p++;
  145. }
  146. }
  147. cout << "test 4" << endl; //test
  148. cout << "The stuff you typed in is not an anagram" << endl;
  149. }



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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 445
Reputation: Agni is a jewel in the rough Agni is a jewel in the rough Agni is a jewel in the rough 
Solved Threads: 69
Sponsor
Agni's Avatar
Agni Agni is offline Offline
Posting Pro in Training

Re: logic error that i cannot find... HELP?

 
0
  #2
Jan 21st, 2009
>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.
thanks
-chandra
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 62
Reputation: homeryansta is an unknown quantity at this point 
Solved Threads: 0
homeryansta homeryansta is offline Offline
Junior Poster in Training

Re: logic error that i cannot find... HELP?

 
0
  #3
Jan 21st, 2009
Originally Posted by Agni View Post
>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
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 445
Reputation: Agni is a jewel in the rough Agni is a jewel in the rough Agni is a jewel in the rough 
Solved Threads: 69
Sponsor
Agni's Avatar
Agni Agni is offline Offline
Posting Pro in Training

Re: logic error that i cannot find... HELP?

 
0
  #4
Jan 21st, 2009
>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'?
thanks
-chandra
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 62
Reputation: homeryansta is an unknown quantity at this point 
Solved Threads: 0
homeryansta homeryansta is offline Offline
Junior Poster in Training

Re: logic error that i cannot find... HELP?

 
0
  #5
Jan 21st, 2009
Originally Posted by Agni View Post
>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'?
sorry, didn't mean to confuse you.

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.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 570
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 93
Murtan Murtan is offline Offline
Posting Pro

Re: logic error that i cannot find... HELP?

 
0
  #6
Jan 21st, 2009
To help me and you think about it, write down in english what you think the compare function should be doing.

It will help me to understand what you intended and whether or not it will accomplish your goal, along with whether or not the function is doing what you think it does.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 62
Reputation: homeryansta is an unknown quantity at this point 
Solved Threads: 0
homeryansta homeryansta is offline Offline
Junior Poster in Training

Re: logic error that i cannot find... HELP?

 
0
  #7
Jan 21st, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 445
Reputation: Agni is a jewel in the rough Agni is a jewel in the rough Agni is a jewel in the rough 
Solved Threads: 69
Sponsor
Agni's Avatar
Agni Agni is offline Offline
Posting Pro in Training

Re: logic error that i cannot find... HELP?

 
1
  #8
Jan 21st, 2009
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.
Last edited by Agni; Jan 21st, 2009 at 2:19 am. Reason: small changes
thanks
-chandra
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 62
Reputation: homeryansta is an unknown quantity at this point 
Solved Threads: 0
homeryansta homeryansta is offline Offline
Junior Poster in Training

Re: logic error that i cannot find... HELP?

 
0
  #9
Jan 21st, 2009
Originally Posted by Agni View Post
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.
thanks agni, i'll try to implement it.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 62
Reputation: homeryansta is an unknown quantity at this point 
Solved Threads: 0
homeryansta homeryansta is offline Offline
Junior Poster in Training

Re: logic error that i cannot find... HELP?

 
0
  #10
Jan 21st, 2009
got it working!!! thanks for all the help. again, sorry for my poor typical american english
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC