//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:

#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;
	}

Recommended Answers

All 17 Replies

> for (code[1] = code2[1];
Perhaps you could post something which actually compiles.

Then post something which is reasonably indented.

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.

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.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:

[U]6.5 Iteration Statements[/U]
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 ]

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?

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)

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.

do
{
while (code[0] == code2[0]);
counter++;
for (counter = 0; counter < 26; counter++; )
for (counter2 = 0; counter2 < 26; counter2++; )
}

Now my other post, posts.

You cannot compare a string directly with an integer, please take a look at the following example:

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;
}

output will be:

equal
not equal
equal

I understand that. That isn't something I can impliment into the code I was hoping for. If there isn't a way to direction compare/match an int to a char; is there a way to do what I overall want? Maybe, once the console input occurs, "1 2 3" or such it can look into my string and retrieve what "is equal to" those digits? Or visa versa, " a b c", get those equivelent in the int array? Maybe using a "cin.get()", to find the proper string or int.
I appreciate all your help so far, I would have ben fighting with that for a long time.

I understand that. That isn't something I can impliment into the code I was hoping for. If there isn't a way to direction compare/match an int to a char; is there a way to do what I overall want? Maybe, once the console input occurs, "1 2 3" or such it can look into my string and retrieve what "is equal to" those digits? Or visa versa, " a b c", get those equivelent in the int array? Maybe using a "cin.get()", to find the proper string or int.
I appreciate all your help so far, I would have ben fighting with that for a long time.

The example I provided demonstrates how you could implement it ...
The one I provided was for digits, sorry my mistake, but you can adapt it so you can also compare a whole integer to a string :)

No, wait!!
I found a better option: convert the integer to a string using stringstreams and compare those two strings :)

int a = 50341;
stringstream ss;
string s, t("50341");
ss << a; // put the number in the stream
ss >> s; // take the number as a string out the stream
if(s==t) cout << "The integer's value is equal to the string's value." << endl;

don't forget to include sstream !

Lost my previous post, again.

//Okay. Sounds reasonable. I'm going to start inputting this into my code. I am pretty sure it is going to take me a while because I am not sure what every statement there does. lol. Appreciate the help. Can I ask you more questions if I can't figure it out beyond this point?

//It is going to be harder than I thought. I need to trade out the declared strings and array from your code and convert it to mine?

>I am pretty sure it is going to take me a while because I am not sure what every statement there does.
What lines of my code don't you understand?

int a = 50341; //declare const a=50341
stringstream ss; ///declare stringstream
string s, t("50341"); //declare strings
ss << a; // put the number in the stream
ss >> s; // take the number as a string out the stream
if(s==t) cout << "The integer's value is equal to the string's value." << endl; //output

Not sure why I need to create a const. My overall goal is to retrieve multiple answers from a string/array created from a text file once console input is received. Such as:
cin << word; //"aword" (input word from user)
cout >> sWord; //"17892" (not random numbers)

int a = 50341; //declare const a=50341
Not sure why I need to create a const.

Can you refer me to the place where I mentioned that?
I don't think so as I didn't say you had to declare it as a const :P

BTW, post using code tags.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.