| | |
Need help comparing array to a string from text file.
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: May 2009
Posts: 14
Reputation:
Solved Threads: 0
//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:
//for (code[1] == code2[1]; counter++;
//or
//for (code[counter] == code2[counter]; counter++;
//This is what I have so far:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> #include <fstream> using namespace std; int main() { int counter; int counter2; int code[26]; string code2[26]; string word; int sWord; char letter; int i; bool quit = false; ifstream inNumData; ifstream inStrData; inNumData.open("code.txt"); inStrData.open("code2.txt"); for (counter = 0; counter < 25; counter++) { inNumData >> code[counter]; } for (counter2 = 0; counter2 < 25; counter2++) { inStrData >> code2[counter2]; } do { for (code[1] = code2[1]; counter++; } inNumData.close("code.txt"); inStrData.close("code2.txt"); cout << "A - Convert a word into secret code." << endl; cout << "B - Convert a secret code back into a word." << endl; cout << "C - Quit Program." << endl; cout << "Please enter your choice: "; cin >> letter; switch (letter) { case 'A': case 'a': cout << "Enter the word to be converted into secret code." << endl; cin >> word; cout << sWord << endl; break; case 'B': case 'b': cout << "Enter a secret code to be converted to the word." << endl; cin >> sWord; cout << word << endl; break; case 'C': case 'c': quit = true; } return 0; }
Last edited by John A; May 20th, 2009 at 3:05 am. Reason: removed [tex] tags
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
•
•
•
•
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
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."
•
•
Join Date: May 2009
Posts: 14
Reputation:
Solved Threads: 0
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?
•
•
•
•
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?
(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."
•
•
Join Date: May 2009
Posts: 14
Reputation:
Solved Threads: 0
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.
C++ Syntax (Toggle Plain Text)
do { while (code[0] == code2[0]); counter++; for (counter = 0; counter < 26; counter++; ) for (counter2 = 0; counter2 < 26; counter2++; ) }
Last edited by Ancient Dragon; May 20th, 2009 at 8:25 pm. Reason: add code tags -- for what its worth
You cannot compare a string directly with an integer, please take a look at the following example:
output will be:
C++ Syntax (Toggle Plain Text)
string s="1a3"; int arr[]={1,2,3}; for(int i=0; i<sizeof(arr)/sizeof(int); i++) { if((s[i]-'0')==arr[i]) cout << "equal" << endl; else cout << "not equal" << endl; }
C++ Syntax (Toggle Plain Text)
equal not equal 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."
![]() |
Similar Threads
- Extract part of text file into 2 dimensional string array (Java)
- find and replace string in a text file (C)
- How to copy floating point values from text file to array (C)
- Search string in a text file (C)
- Read in a string from a text file (C)
- New User (C)
Other Threads in the C++ Forum
- Previous Thread: templating question
- Next Thread: Creating a Picturebox at Runtime (windows forms applications)
| Thread Tools | Search this Thread |
api array arrays based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






