I'm trying to create a Rotor struct that holds arrays of characters in a specific sequence.
Rotor
struct Rotor
{
public:
char assignedRotor[26];
void setRotor(int rotor) {
switch(rotor) {
case(1):
assignedRotor = rotor1;
break;
case(2):
assignedRotor = rotor2;
break;
case(3):
assignedRotor = rotor3;
break;
};
}
private:
char rotor1[26] = {'E', 'K', 'M', 'F', 'L', 'G', 'D', 'Q', 'V', 'Z', 'N', 'T', 'O', 'W', 'Y', 'H', 'X', 'U', 'S', 'P', 'A', 'I', 'B', 'R', 'C', 'J'};
char rotor2[26] = {'A', 'J', 'D', 'K', 'S', 'I', 'R', 'U', 'X', 'B', 'L', 'H', 'W', 'T', 'M', 'C', 'Q', 'G', 'Z', 'N', 'P', 'Y', 'F', 'V', 'O', 'E'};
char rotor3[26] = {'B', 'D', 'F', 'H', 'J', 'L', 'C', 'P', 'R', 'T', 'X', 'V', 'Z', 'N', 'Y', 'E', 'I', 'W', 'G', 'A', 'K', 'M', 'U', 'S', 'Q', 'O'};
};
Displaying rotor
int main() {
Rotor rotor;
for(int i; i < 26; i++) cout << enig.rotor2.assignedRotor[i]
return 0;
}
Is my "initialization" or "displaying" incorrect? Or is it both?
In c++ if you are declaring any variable you have to initialize its value otherwise it will be a garbage value. In your case, you are just declaring int i
in the loop and variable i
will point to some garbage value. Which you can fix by initializing i = 0
in the loop.
int main()
{
Machine enig;
for(int i = 0; i < 26; i++)
{
cout << enig.rotor1.assignedRotor[i] << endl;
};
return 0;
}