I have a long stream of numbers that I need to be able to input all at once (basically copy and paste) into an array somehow. I need to do this so I can make every pair of those numbers to correspond to a letter.

For example, I need to be able to type in 45323456 and have it come out "dove", so 45 = d, 32 = o, and so on. I was trying to just use a string and then use a "for" loop and a set of arrays to do this, but I'm just getting gibberish. I've got the comparison part pretty well worked out; I just can't figure out how to segment each set of two into an array for future comparison.

Any help would be much appreciated. Thank you.

int codearray[50];
char phrasearray[50];
string phrase;
int j;

cout << "Please input your code without spaces: ";
cin >> phrase; 

int SizePhrase=phrase.size();

for(j=0; j<SizePhrase; ++j)
{
codearray[j] = phrase[j];
}

your comparison inputs a digit into 1 array slot. So instead of 45 in codearray[0], it becomes 4 in codearray[0] and 5 in codearray[1].

if you want to input each slot with 2 digits each, i would reccomend changing the algoritm this way:

leap j by 2 everytime the loop is completed
assign phrase[j] and phrase[j+1] to codearray[j], this way for each slot you'll get the values in the current value of j and the next value in the array.

Edited 5 Years Ago by Lyandor: n/a

Use structure to copy at one go without looping.
Something like this:

struct Phrase {
 char phrase[50];
};

Phrase codePhrase, inputPhrase;

cout << "Please input your code without spaces: ";
cin >> inputPhrase.phrase;

// Now copy it to your codePhrase
codePhrase = inputPhrase;
Comments
Worthless. Doesn't answer any question asked.

Lyandor,
Ok, how would you phrase that?

Like this?

#include<iostream>
#include<string>
using namespace std;
int main()
{

int codearray[200];
string phrase;

cout << "Please input your code without spaces: ";
cin >> phrase; 

int SizePhrase=phrase.size();

char phrasearray[200];
int j;

for(j=0; j<SizePhrase-1; ++j)
    {
	codearray[j] = phrase[j];
	codearray[j] = phrase[j+1];
	}

cout << "CODEARRAY " << codearray[0];

}

Or like this?

#include<iostream>
#include<string>
using namespace std;
int main()
{

int codearray[200];
string phrase;

cout << "Please input your code without spaces: ";
cin >> phrase; 

int SizePhrase=phrase.size();

char phrasearray[200];
int j;

for(j=0; j<SizePhrase-1; ++j)
    {
	codearray[j] = (phrase[j] + phrase[j+1]);
	}

cout << "CODEARRAY " << codearray[0];

}

Because in either case, the result I get from the final "cout << "CODEARRAY " << codearray[0]" is always incorrect. If I input 21, I get 49.

If you're taking two characters and turning them into the represented integer value, there's more to it than simply adding the two characters together:

#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>

using namespace std;

int main()
{
    string code;
    
    cout << "Enter a code: ";
    
    if (cin >> code) {
        vector<int> decoded;
        
        for (string::size_type i = 0; i < code.size(); i++) {
            int value = code[i] - '0';
            
            if (++i < code.size())
                value = 10 * value + (code[i] - '0');
                
            decoded.push_back(value);
        }
        
        copy(decoded.begin(), decoded.end(), ostream_iterator<int>(cout, "\n"));
    }
}

You could use a map to help decode the message.

#include <iostream>
#include <map>
using namespace std;

int main()
{
	map<string, char> cipher;
	cipher["45"] = 'd';
	cipher["32"] = 'o';
	cipher["34"] = 'v';
	cipher["56"] = 'e';

	string encoded = "45323456", decoded = "";

	for( unsigned int i = 0; i < encoded.length(); i+=2 )
		decoded += cipher[encoded.substr(i, 2)];

	cout << decoded << endl;

	return 0;
}

Ok, I'll be honest, you're going way over my head with vectors and maps. I'm still a basic-level C++ user. Is there a simpler way to do it? If not, I'll just have to do some major reading, or I may just abandon this hobby project.

Is there a simpler way to do it? If not, I'll just have to do some major reading, or I may just abandon this hobby project.

If those are your options, I'll go with no, there's not a simpler way to do it. If you choose to do some major reading then you're well suited to programming. If you choose to abandon the project, I'd suggest finding another hobby, because any project worth doing will push your limits. Quitting over so small a matter means you won't accomplish anything.

Edited 5 Years Ago by Narue: n/a

You can do it with a bunch of if/else statements like this

#include <iostream>
using namespace std;

int main()
{
	string encoded = "45323456", decoded = "", encodedPart;

	for( unsigned int i = 0; i < encoded.length(); i+=2 )
	{
		encodedPart = encoded.substr(i, 2);
		if( encodedPart == "45" )
			decoded += 'd';
		else if( encodedPart == "32" )
			decoded += 'o';
		else if( encodedPart == "34" )
			decoded += 'v';
		else if( encodedPart == "56" )
			decoded += 'e';
		else
			; //this does nothing but without it you will get an error since there is nothing
	}

	cout << decoded << endl;

	return 0;
}

I wouldn't really say the map is over your head since just an array but with custom indexes (some languages call them dictionaries because that is pretty much how they are used).

In map<string, char> the index is a string and what gets held at that index is a char.

Its almost exactly the same as if you had an array of characters except for it is indexed by a string and it is a dynamic array rather than a static one.

Okay, thanks, guys. I really appreciate it. I'm a Java guy, but I thought I might give C++ a try. I think I'll stick with Java, though. Thanks again!

This question has already been answered. Start a new discussion instead.