Need help comparing array to a string from text file.

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2009
Posts: 14
Reputation: kailisr is an unknown quantity at this point 
Solved Threads: 0
kailisr kailisr is offline Offline
Newbie Poster

Need help comparing array to a string from text file.

 
0
  #1
May 19th, 2009
//I am haing trouble creating this program. There is basically only one part that I can't figure out. I need to match up my array to my string. I need something that will force the first strings in each to equal eachother, then use a counter to match the rest. I know it needs to be a DO loop. Maybe something like:
//for (code[1] == code2[1]; counter++;
//or
//for (code[counter] == code2[counter]; counter++;
//This is what I have so far:
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. int counter;
  9. int counter2;
  10. int code[26];
  11. string code2[26];
  12. string word;
  13. int sWord;
  14. char letter;
  15. int i;
  16. bool quit = false;
  17.  
  18.  
  19.  
  20. ifstream inNumData;
  21. ifstream inStrData;
  22. inNumData.open("code.txt");
  23. inStrData.open("code2.txt");
  24.  
  25. for (counter = 0; counter < 25; counter++)
  26. {
  27. inNumData >> code[counter];
  28. }
  29. for (counter2 = 0; counter2 < 25; counter2++)
  30. {
  31. inStrData >> code2[counter2];
  32. }
  33. do
  34. {
  35. for (code[1] = code2[1];
  36. counter++;
  37. }
  38.  
  39. inNumData.close("code.txt");
  40. inStrData.close("code2.txt");
  41.  
  42. cout << "A - Convert a word into secret code." << endl;
  43. cout << "B - Convert a secret code back into a word." << endl;
  44. cout << "C - Quit Program." << endl;
  45. cout << "Please enter your choice: ";
  46. cin >> letter;
  47.  
  48. switch (letter)
  49. {
  50. case 'A':
  51. case 'a':
  52. cout << "Enter the word to be converted into secret code." << endl;
  53. cin >> word;
  54. cout << sWord << endl;
  55. break;
  56.  
  57. case 'B':
  58. case 'b':
  59. cout << "Enter a secret code to be converted to the word." << endl;
  60. cin >> sWord;
  61. cout << word << endl;
  62. break;
  63.  
  64.  
  65. case 'C':
  66. case 'c':
  67.  
  68. quit = true;
  69. }
  70. return 0;
  71. }
Last edited by John A; May 20th, 2009 at 3:05 am. Reason: removed [tex] tags
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Need help comparing array to a string from text file.

 
0
  #2
May 20th, 2009
> for (code[1] = code2[1];
Perhaps you could post something which actually compiles.

Then post something which is reasonably indented.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 14
Reputation: kailisr is an unknown quantity at this point 
Solved Threads: 0
kailisr kailisr is offline Offline
Newbie Poster

Re: Need help comparing array to a string from text file.

 
0
  #3
May 20th, 2009
I know it doesn't compile, I get errors because there is no argument. And I probab'ly would have been worried about that statements indentation if it worked.
If I knew how to finish this than I wouldn't need help and I wouldn't have asked for suggestions.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,362
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 241
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Need help comparing array to a string from text file.

 
0
  #4
May 20th, 2009
6.8.5 Iteration statements
Syntax
   iteration-statement:
      while ( expression ) statement
      do statement while ( expression ) ;
      for ( expressionopt ; expressionopt ; expressionopt ) statement
      for ( declaration expressionopt ; expressionopt ) statement
Last edited by Dave Sinkula; May 20th, 2009 at 3:23 pm.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: Need help comparing array to a string from text file.

 
0
  #5
May 20th, 2009
Originally Posted by Dave Sinkula View Post
6.8.5 Iteration statements
Syntax
   iteration-statement:
      while ( expression ) statement
      do statement while ( expression ) ;
      for ( expressionopt ; expressionopt ; expressionopt ) statement
      for ( declaration expressionopt ; expressionopt ) statement
According to the C++ standard it's:
6.5 Iteration Statements
iteration-statement:
    while ( condition ) statement
    do statement while ( expression ) ;
    for ( for-init-statement conditionopt ; expressionopt ) statement
for-init-statement:
    expression-statement
    simple-declaration
[ Note: a for-init-statement ends with a semicolon. —end note ]
Last edited by tux4life; May 20th, 2009 at 3:34 pm.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 14
Reputation: kailisr is an unknown quantity at this point 
Solved Threads: 0
kailisr kailisr is offline Offline
Newbie Poster

Re: Need help comparing array to a string from text file.

 
0
  #6
May 20th, 2009
I see what you're saying, that is the format of the do loop I was looking for. That is great! Thanks. The only other thing I am having a problem with is matching up my array with the string. I am trying to use "array == string", because = is out of the question. I know I want it to say "is equal to", but that reads as a compilation error. Do you know what the correct operator would be?
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: Need help comparing array to a string from text file.

 
0
  #7
May 20th, 2009
Originally Posted by kailisr View Post
I am having a problem with is matching up my array with the string. I am trying to use "array == string", because = is out of the question. I know I want it to say "is equal to", but that reads as a compilation error. Do you know what the correct operator would be?
Could you give us an example of the code you're trying to do this in?
(post the code where you have this problem with)
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 14
Reputation: kailisr is an unknown quantity at this point 
Solved Threads: 0
kailisr kailisr is offline Offline
Newbie Poster

Re: Need help comparing array to a string from text file.

 
0
  #8
May 20th, 2009
I know that is needs to be a do..while loop. I am also not sure if the second for loop is necessary. I can find that out after I realize the first while loop. I think "==" should work, but I get "there is no acceptable conversion" error, because this is an int array and a string.
  1. do
  2. {
  3. while (code[0] == code2[0]);
  4. counter++;
  5. for (counter = 0; counter < 26; counter++; )
  6. for (counter2 = 0; counter2 < 26; counter2++; )
  7. }
Last edited by Ancient Dragon; May 20th, 2009 at 8:25 pm. Reason: add code tags -- for what its worth
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 14
Reputation: kailisr is an unknown quantity at this point 
Solved Threads: 0
kailisr kailisr is offline Offline
Newbie Poster

Re: Need help comparing array to a string from text file.

 
0
  #9
May 20th, 2009
Now my other post, posts.
Last edited by kailisr; May 20th, 2009 at 5:54 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: Need help comparing array to a string from text file.

 
0
  #10
May 20th, 2009
You cannot compare a string directly with an integer, please take a look at the following example:
  1. string s="1a3";
  2. int arr[]={1,2,3};
  3.  
  4. for(int i=0; i<sizeof(arr)/sizeof(int); i++)
  5. {
  6. if((s[i]-'0')==arr[i])
  7. cout << "equal" << endl;
  8. else
  9. cout << "not equal" << endl;
  10. }
output will be:
  1. equal
  2. not equal
  3. equal
Last edited by tux4life; May 20th, 2009 at 5:57 pm.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
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