below is c++ program for decimal to hexadecimal conversion!!
// decimal to hexadecimal conversion
#include <iostream>
#include <math.h>
#include <string>
using namespace std;
string d2h(int n) {
string s, t;
int i = 0;
while (n >= 1) {
t[i] = n % 16;
switch (t[i]) {
case 10:
t[i] = 'A';
break;
case 11:
t[i] = 'B';
break;
case 12:
t[i] = 'C';
break;
case 13:
t[i] = 'D';
break;
case 14:
t[i] = 'E';
break;
case 15:
t[i] = 'F';
break;
default:
break;
}
s[i] = t[i];
n /= 16;
i++;
}
return s;
}
int main() {
int n;
cin >> n;
cout << d2h(n);
return 0;
}
I am getting nothing as output!!
PS C:\Users\anmol\Desktop\c++projectwork> g++ .\dec2hexadec.cpp
PS C:\Users\anmol\Desktop\c++projectwork> ./a
479
PS C:\Users\anmol\Desktop\c++projectwork>
What should i change??
You are not allocating space in s or t for memory. Use s.push_back() instead of s[i].
You'll probably need to reverse the string as well.
t[i] = n % 16; This does not assign the character '0', but rather the number 0. Why don't you just create 16 cases in your switch statement? Or use the ASCII quirk '0' + (n % 16).
// decimal to hexadecimal conversion
#include <iostream>
#include <math.h>
#include <string>
using namespace std;
string d2h(int n) {
string s, t;
string Sign;
int i = 0;
// n is equal to 0? <0 or >0?
if (n == 0){
return "0";
}
if (n < 0){
Sign = "-";
n *= -1;
}
while (n ) {
t.push_back(n % 16) ;
switch (t[i]) {
case 0:
t[i] = '0';
break;
case 1:
t[i] = '1';
break;
case 2:
t[i] = '2';
break;
case 3:
t[i] = '3';
break;
case 4:
t[i] = '4';
break;
case 5:
t[i] = '5';
break;
case 6:
t[i] = '6';
break;
case 7:
t[i] = '7';
break;
case 8:
t[i] = '8';
break;
case 9:
t[i] = '9';
break;
case 10:
t[i] = 'A';
break;
case 11:
t[i] = 'B';
break;
case 12:
t[i] = 'C';
break;
case 13:
t[i] = 'D';
break;
case 14:
t[i] = 'E';
break;
case 15:
t[i] = 'F';
break;
default:
break;
}
s.push_back(t[i]);
n /= 16;
i++;
}
reverse(s.begin(), s.end());
return Sign+s;
}
int main() {
int n;
cin >> n;
cout << d2h(n);
return 0;
}
Related
I ran this code and entered the values "P", "Q" and "101" and it seems to start hanging. I would like to know what kind of coding and thinking do some of you use to help optimize your code to make it run as quickly as possible and the changes you would make to my code. I believe the main problem lies in the "toSequencePos" sub routine as it takes too long for it to be carried out.
Thanks in advance.
[Edit] This code is in answer to the BIO 2011 which can be found here (Question 1 A)
#include <iostream>
using namespace std;
int toNumber(char letter1)
{
long long int position;
switch (letter1)
{
case 'A': position = 1; break;
case 'B': position = 2; break;
case 'C': position = 3; break;
case 'D': position = 4; break;
case 'E': position = 5; break;
case 'F': position = 6; break;
case 'G': position = 7; break;
case 'H': position = 8; break;
case 'I': position = 9; break;
case 'J': position = 10; break;
case 'K': position = 11; break;
case 'L': position = 12; break;
case 'M': position = 13; break;
case 'N': position = 14; break;
case 'O': position = 15; break;
case 'P': position = 16; break;
case 'Q': position = 17; break;
case 'R': position = 18; break;
case 'S': position = 19; break;
case 'T': position = 20; break;
case 'U': position = 21; break;
case 'V': position = 22; break;
case 'W': position = 23; break;
case 'X': position = 24; break;
case 'Y': position = 25; break;
case 'Z': position = 26; break;
default: position = 0; break;
}
return position;
}
int toLetter(long long int finalPosition)
{
char letter;
switch (finalPosition)
{
case 1: letter = 'A'; break;
case 2: letter = 'B'; break;
case 3: letter = 'C'; break;
case 4: letter = 'D'; break;
case 5: letter = 'E'; break;
case 6: letter = 'F'; break;
case 7: letter = 'G'; break;
case 8: letter = 'H'; break;
case 9: letter = 'I'; break;
case 10: letter = 'J'; break;
case 11: letter = 'K'; break;
case 12: letter = 'L'; break;
case 13: letter = 'M'; break;
case 14: letter = 'N'; break;
case 15: letter = 'O'; break;
case 16: letter = 'P'; break;
case 17: letter = 'Q'; break;
case 18: letter = 'R'; break;
case 19: letter = 'S'; break;
case 20: letter = 'T'; break;
case 21: letter = 'U'; break;
case 22: letter = 'V'; break;
case 23: letter = 'W'; break;
case 24: letter = 'X'; break;
case 25: letter = 'Y'; break;
case 26: letter = 'Z'; break;
}
return letter;
}
int toSequencePos(long long int n1, long long int letterPos1, long long int letterPos2)
{
long long int finalPosition = 0;
for(long long int x = 1; x <= n1 - 2; x++)
{
finalPosition = letterPos1 + letterPos2;
letterPos1 = letterPos2;
letterPos2 = finalPosition;
}
while (finalPosition > 26)
{
finalPosition = finalPosition - 26;
}
return finalPosition;
}
int main()
{
char letter1;
char letter2;
long long int letterPos1 = 0;
long long int letterPos2 = 0;
long long int sequenceLetterPos = 0;
long long int n1;
char finalAnswer;
cout << "Please enter your first letter: ";
cin >> letter1;
letterPos1 = toNumber(letter1);
cout << "Please enter your second letter: ";
cin >> letter2;
letterPos2 = toNumber(letter2);
cout << "Please enter the position number that you wish to find in this sequence";
cin >> n1;
sequenceLetterPos = toSequencePos(n1, letterPos1, letterPos2);
finalAnswer = toLetter(sequenceLetterPos);
cout << "The letter in position " << n1 << " is " << finalAnswer << endl;
}
Here are some optimisation points
int toNumber(char letter1)
{
long long int position = lettter1 - 'A' + 1;
return position;
}
int toLetter(long long int finalPosition)
{
char letter = 'A' + finalPosition - 1;
return letter;
}
in toSequencePos function
// instead of following thing
while (finalPosition > 26)
{
finalPosition = finalPosition - 26;
}
// use following
finalPosition = finalPosition % 26;
UPDATE::
Above solution will not work if the system's character encoding scheme is somewhat non-sequential. i.e. EBCDIC, BCD (character encoding) etc.
in that case, you have to maintain one hash-map & one array. Hash-map contains char as key & int as values. Arrays of char will suffice for this matter. Following given such a encoding independent implementation
#include <stdio.h>
#include <string.h>
#define NUMBER_OF_CHARACTER 26
char int2charMapping[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
//
// suppose we spare 8 bit for character representation
//
unsigned int char2IntMapping[256];
void initiateTheChar2IntMapping()
{
memset(char2IntMapping, 0, sizeof(char2IntMapping));
char2IntMapping['A'] = 1;
char2IntMapping['B'] = 2;
char2IntMapping['C'] = 3;
char2IntMapping['D'] = 4;
char2IntMapping['E'] = 5;
char2IntMapping['F'] = 6;
char2IntMapping['G'] = 7;
char2IntMapping['H'] = 8;
char2IntMapping['I'] = 9;
char2IntMapping['J'] = 10;
char2IntMapping['K'] = 11;
char2IntMapping['L'] = 12;
char2IntMapping['M'] = 13;
char2IntMapping['N'] = 14;
char2IntMapping['O'] = 15;
char2IntMapping['P'] = 16;
char2IntMapping['Q'] = 17;
char2IntMapping['R'] = 18;
char2IntMapping['S'] = 19;
char2IntMapping['T'] = 20;
char2IntMapping['U'] = 21;
char2IntMapping['V'] = 22;
char2IntMapping['W'] = 23;
char2IntMapping['X'] = 24;
char2IntMapping['Y'] = 25;
char2IntMapping['Z'] = 26;
}
char toLetter(long long int index)
{
if(index > 0 && index < 27)
{
return int2charMapping[index-1];
}
return '\0';
}
int toNumber(char letter)
{
return char2IntMapping[letter];
}
int main()
{
//initiate the mapping
initiateTheChar2IntMapping();
printf("--> %c\n", toLetter(26));
printf("~~> %d\n", toNumber('A'));
return 0;
}
you're essentially mapping letters to numbers of your own choosing. you could use a map data structure to do this, but much more efficiently. HTH
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I want my function to read one char[] and split it into 2 arrays. Both arrays would be one big number, first array is for that number int value and second one is for double values. I must use 2 arrays because this number can be binary, decimal, octal or hex.
void Read(int place[], int &size, int &type, int placedot[], int &sizedot, int &typedot)
{
char reader[limit];
cin >> reader;
size = 1;
while (reader[size] == '.' || size != strlen(reader))
{
size++;
cout << "LOL";
}
cin >> type;
cout << size;
typedot = type;
for (int i = 0; i<size; i++)
{
switch (reader[i])
{
case '0': place[i] = 0; break;
case '1': place[i] = 1; break;
case '2': place[i] = 2; break;
case '3': place[i] = 3; break;
case '4': place[i] = 4; break;
case '5': place[i] = 5; break;
case '6': place[i] = 6; break;
case '7': place[i] = 7; break;
case '8': place[i] = 8; break;
case '9': place[i] = 9; break;
case 'A': place[i] = 10; break;
case 'B': place[i] = 11; break;
case 'C': place[i] = 12; break;
case 'D': place[i] = 13; break;
case 'E': place[i] = 14; break;
case 'F': place[i] = 15; break;
}
}
}
#include <iostream>
#include <string>
int main()
{
std::string str = "3423432432.32445654576654578978905";
std::string strInt;
std::string strDec;
int i = 0;
while('.' != str[i])
strInt += str[i++];
for(++i; i < str.length(); i++)
strDec += str[i];
std::cout << strInt << std::endl << strDec << std::endl;
// now you have the integer part and the decimal part as strings so convert each of them to int
std::cout << std::endl;
return 0;
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
i have a program and i should convert int to char into it
but i cant use itoa() because site's judge don't support it
so i wrote this:
xt[i]= rmn+'0';
but i get this error :
Runtime error: Illegal file open (/dev/tty)
How should i convert this?
My code is this : (for palsquare question of USACO)
/*
ID: sa.13781
PROG: palsquare
LANG: C++
*/
#include <iostream>
#include <fstream>
using namespace std;
ofstream fout("palsquare.out");
int tool(char xt[])//CORRECT
{
int p = 0;
while (xt[p] != 0)
p++;
return p;
}
void prt(char xt[])//CORRECT
{
int p = 0;
while (xt[p] != 0)
{
fout << xt[p];
p++;
}
}
void mabna(int a, char xt[], int mab)
{
int ex = 1, tavan = 0, rmn, n;
for (; ex <= a; ex *= mab)
tavan++;
for (int i = tavan - 1; a != 0; i--, a /= mab)
{
rmn = a % mab;
switch (rmn)
{
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
xt[i] = rmn + '0';
break;
case 10:
xt[i] = 'A';
break;
case 11:
xt[i] = 'B';
break;
case 12:
xt[i] = 'C';
break;
case 13:
xt[i] = 'D';
break;
case 14:
xt[i] = 'E';
break;
case 15:
xt[i] = 'F';
break;
case 16:
xt[i] = 'G';
break;
case 17:
xt[i] = 'H';
break;
case 18:
xt[i] = 'I';
break;
case 19:
xt[i] = 'J';
break;
}
}
}
bool mirror(char *xt)//CORRECT
{
int p = 0;
int n = tool(xt);
for (int i = 0; i < (n / 2); i++)
if (xt[i] == xt[n - i - 1])
p++;
if (p == (n / 2))
return true;
return false;
}
void calc(int mab) //CORRECT
{
for (int i = 1; i <= 300; i++)
{
char p[10] = {0}, p2[10] = {0};
mabna(i * i, p2, mab);
if ( mirror(p2) == true )
{
mabna(i, p, mab);
prt(p);
fout << " ";
prt(p2);
fout << "\n";
}
}
}
int main()
{
ifstream fin("palsquare.in");
int mab;
fin >> mab;
calc(mab);
return 0;
}
Can you use sprintf()?
char s[16];
int x = 15;
sprintf(s, "%d", x);
If your int value is single digit, you can just cast it with (char)
If your int value is more than one digit, you can never expect a single char to hold it. char can only hold single character.
int num = 7;
char ch = (num + 48); //same effect as + '0'
cout << ch << endl;
OUTPUT: 7
If your int value is more than single digit. You need char* or char[] to hold the converted data. For example:
int num = 123987;
int len = 6;
int idx = len-1;;
char ch[20];
while(num > 0)
{
ch[idx] = (num%10) + 48;
num /= 10;
idx --;
}
for(int x=0; x<len; x++)
cout << "OUTPUT in char: " << ch[x];
OUTPUT in char: 123987
This is not a perfectly good way to handle this kind of situation, but it may gives you what you asked for.
Ok, so this program has been shortened to make it easier to read. There would obviously be more case statements to go along with the amount of variables there are. My question is why does my program get an error every time it runs. It is fine when it compiles but not when I run it.
#include <iostream>
#include <fstream>
#include <cmath>
#include <string>
using namespace std;
int main()
{
string thearray[22960];
char bigfor[10];
int arrayvar = 0;
int finishedarray;
int a;
int b;
int c;
int d;
int e;
int f;
int g;
int h;
int i;
int j;
// Constants
int variable1 = 0;
int variable2 = 1;
int variable3 = 2;
int variable4 = 3;
int variable5 = 4;
int variable6 = 5;
int variable7 = 6;
int variable8 = 7;
int variable9 = 8;
int variable10 = 9;
for (a = 0; a < 36; a++)
{
switch (a)
{
case 0:
bigfor[variable1] = '0';
break;
case 1:
bigfor[variable1] = '1';
break;
case 2:
bigfor[variable1] = '2';
break;
case 3:
bigfor[variable1] = '3';
break;
case 4:
bigfor[variable1] = '4';
break;
case 5:
bigfor[variable1] = '5';
break;
case 6:
bigfor[variable1] = '6';
break;
case 7:
bigfor[variable1] = '7';
break;
case 8:
bigfor[variable1] = '8';
break;
case 9:
bigfor[variable1] = '9';
break;
}
thearray[arrayvar] = bigfor[variable1] + bigfor[variable2] + bigfor[variable4] +
bigfor[variable5] + bigfor[variable6] + bigfor[variable7] +
bigfor[variable8] + bigfor[variable9] + bigfor[variable10];
arrayvar = arrayvar + 1;
}
finishedarray = arrayvar + 1;
ofstream myfile;
myfile.open("codes.txt");
for (arrayvar = 0; a < finishedarray; a++)
{
myfile << thearray[arrayvar] << endl;
}
myfile.close();
return 0;
}
One problem is that you are reading uninitialized values here:
thearray[arrayvar] = bigfor[variable1]+ bigfor[variable2] + bigfor[variable4] +
bigfor[variable5] + bigfor[variable6] + bigfor[variable7] + bigfor[variable8] +
bigfor[variable9] + bigfor[variable10];
This is undefined behaviour. At this point in the program, only bigfor[variable1] is initialized.
Whatever you are trying to do, I have a feeling there is a simpler way to do it.
The problem was the addition you were doing. The addition will only add the integer-encoded values of the ASCII characters, and result in an integer. This will cause an error because you can't assign an integer to a std::string.
In addition to #juanchopanza's answer, your code can be improved substantially. For example, instead of listing out the indices manually, you can do this instead within your for loop:
std::string bigfor;
for (int a = 0; a < 36; ++a)
{
bigfor += (a + 48);
}
std::ofstream myfile("codes.txt");
for (int i = 0; i < bigfor.size(); ++i)
{
myfile << bigfor[i] << std::endl;
}
I looked through the other questions that also contained these errors. I was not able to find a solution from those questions. Some answers said certain functions weren't implemented but my functions are. I only get the title error when I try to run the program from the VS2012 command line. When I run the program from the IDE, I have no errors and everything is fine.
Visual Studio 2012 Project
TTH.zip
Imgur
Capture_CMD.png
useTTH.cpp
#include <iostream>
#include <string>
#include "tth.h"
using namespace std;
int main()
{
string message;
string choice;
do
{
cout << "Please enter a message: ";
getline(cin, message);
tth tthObject;
tthObject.getMessage(message);
tthObject.encryptMessageOnlyLetters();
cout << endl;
cout << "Encrypt another message? (Yes/No)" << endl;
cout << "> ";
getline(cin, choice);
cout << endl;
}
while(choice.substr(0, 1) == "Y" || choice.substr(0, 1) == "y");
system("PAUSE");
return 0;
}
tth.h
#include <string>
#include <vector>
using namespace std;
class tth
{
public:
tth();
void getMessage(string messageP);
void setMessageOnlyLettersNumberOf16Blocks();
void encryptMessageOnlyLetters();
void messageOnlyLettersToFourByFourLetters();
void matchLetterToNumber();
void calculateAndShowRunningTotalFourByFourNumbers();
void shiftRowsAccordingly();
void calculateAndShowRunningTotalTemp();
void matchNumberToLetter();
void showFourLetterHashNumbers();
private:
string message;
vector<char> messageOnlyLetters;
int runningTotal[4];
char fourLetterHash[4];
int messageLength;
int messageOnlyLettersNumberOf16Blocks;
char fourByFourLetters[4][4];
int fourByFourNumbers[4][4];
int temp[4][4];
};
tth.cpp
#include <iostream>
#include <string>
#include <cctype>
#include "tth.h"
//--------------------------------------------------------------------------------------------------------------
// tth()
//--------------------------------------------------------------------------------------------------------------
// Constructor for the tth class.
// Initializes the elements of the private integer array 'runningTotal' to 0.
// Initializes the elements of the private char array 'fourLetterHash' to 'A'.
//--------------------------------------------------------------------------------------------------------------
tth::tth()
{
for(int i = 0; i < 4; i++)
{
runningTotal[i] = 0;
fourLetterHash[i] = 'A';
}
}
//--------------------------------------------------------------------------------------------------------------
// getMessage(string messageP)
//--------------------------------------------------------------------------------------------------------------
// A public function of the tth class.
// Sets the value of the private string variable 'message' to the value of the string 'messageP' parameter.
// Gives the private char vector 'messageOnlyLetters' only letter elements of the 'message' variable.
//--------------------------------------------------------------------------------------------------------------
void tth::getMessage(string messageP)
{
message = messageP;
messageLength = message.length();
for(int i = 0; i < messageLength; i++)
{
if(isalpha(message[i]))
{
messageOnlyLetters.push_back(message[i]);
}
}
}
//--------------------------------------------------------------------------------------------------------------
// setMessageOnlyLettersNumberOf16Blocks()
//--------------------------------------------------------------------------------------------------------------
// A public function of the tth class.
// Pads the messageOnlyLetters vector with a number 'A' elements equal to modulo 16 of the size of the vector.
// Sets the private int variable 'messageOnlyLettersNumberOf16Blocks' to the quotient of the vector size and 16.
//--------------------------------------------------------------------------------------------------------------
void tth::setMessageOnlyLettersNumberOf16Blocks()
{
int messageOnlyLettersModulo = messageOnlyLetters.size() % 16;
while(messageOnlyLettersModulo != 0)
{
messageOnlyLetters.push_back('A');
messageOnlyLettersModulo = messageOnlyLetters.size() % 16;
}
messageOnlyLettersNumberOf16Blocks = messageOnlyLetters.size() / 16;
}
//--------------------------------------------------------------------------------------------------------------
// encryptMessageOnlyLetters()
//--------------------------------------------------------------------------------------------------------------
// A public function of the tth class.
// Executes the 'setMessageOnlyLettersNumberOf16Blocks()' once.
// Executes other functions of the tth class a number of times equal to 'messageOnlyLettersNumberOf16Blocks'.
//--------------------------------------------------------------------------------------------------------------
void tth::encryptMessageOnlyLetters()
{
setMessageOnlyLettersNumberOf16Blocks();
for (int i = 0; i < messageOnlyLettersNumberOf16Blocks; i++)
{
messageOnlyLettersToFourByFourLetters();
matchLetterToNumber();
calculateAndShowRunningTotalFourByFourNumbers();
cout << endl;
shiftRowsAccordingly();
calculateAndShowRunningTotalTemp();
cout << endl;
matchNumberToLetter();
showFourLetterHashNumbers();
cout << endl;
}
}
//--------------------------------------------------------------------------------------------------------------
// messageOnlyLettersToFourByFourLetters()
//--------------------------------------------------------------------------------------------------------------
// A public function of the tth class.
// Sets the elements of the private char 2D array 'fourByFourLetters' to the elements of 'messageOnlyLetters'.
// Reduces the size of a non-empty messageOnlyLetters by 16 elements.
//--------------------------------------------------------------------------------------------------------------
void tth::messageOnlyLettersToFourByFourLetters()
{
int count = 0;
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 4; j++)
{
fourByFourLetters[i][j] = messageOnlyLetters[count];
count++;
}
}
if(!(messageOnlyLetters.empty()))
{
messageOnlyLetters.erase(messageOnlyLetters.begin(), messageOnlyLetters.begin() + 16);
}
}
//--------------------------------------------------------------------------------------------------------------
// matchLetterToNumber()
//--------------------------------------------------------------------------------------------------------------
// A public function of the tth class.
// Sets the elements of the private int 2D array 'fourByFourNumbers' to a number 0 - 25.
// The number is determined by the ASCII value of a 'fourByFourLetters' element.
//--------------------------------------------------------------------------------------------------------------
void tth::matchLetterToNumber()
{
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 4; j++)
{
switch(static_cast<int>(fourByFourLetters[i][j]))
{
case 65:
case 97:
fourByFourNumbers[i][j] = 0;
break;
case 66:
case 98:
fourByFourNumbers[i][j] = 1;
break;
case 67:
case 99:
fourByFourNumbers[i][j] = 2;
break;
case 68:
case 100:
fourByFourNumbers[i][j] = 3;
break;
case 69:
case 101:
fourByFourNumbers[i][j] = 4;
break;
case 70:
case 102:
fourByFourNumbers[i][j] = 5;
break;
case 71:
case 103:
fourByFourNumbers[i][j] = 6;
break;
case 72:
case 104:
fourByFourNumbers[i][j] = 7;
break;
case 73:
case 105:
fourByFourNumbers[i][j] = 8;
break;
case 74:
case 106:
fourByFourNumbers[i][j] = 9;
break;
case 75:
case 107:
fourByFourNumbers[i][j] = 10;
break;
case 76:
case 108:
fourByFourNumbers[i][j] = 11;
break;
case 77:
case 109:
fourByFourNumbers[i][j] = 12;
break;
case 78:
case 110:
fourByFourNumbers[i][j] = 13;
break;
case 79:
case 111:
fourByFourNumbers[i][j] = 14;
break;
case 80:
case 112:
fourByFourNumbers[i][j] = 15;
break;
case 81:
case 113:
fourByFourNumbers[i][j] = 16;
break;
case 82:
case 114:
fourByFourNumbers[i][j] = 17;
break;
case 83:
case 115:
fourByFourNumbers[i][j] = 18;
break;
case 84:
case 116:
fourByFourNumbers[i][j] = 19;
break;
case 85:
case 117:
fourByFourNumbers[i][j] = 20;
break;
case 86:
case 118:
fourByFourNumbers[i][j] = 21;
break;
case 87:
case 119:
fourByFourNumbers[i][j] = 22;
break;
case 88:
case 120:
fourByFourNumbers[i][j] = 23;
break;
case 89:
case 121:
fourByFourNumbers[i][j] = 24;
break;
case 90:
case 122:
fourByFourNumbers[i][j] = 25;
break;
default:
cout << "Hmmmm";
}
}
}
}
//--------------------------------------------------------------------------------------------------------------
// calculateAndShowRunningTotalFourByFourNumbers()
//--------------------------------------------------------------------------------------------------------------
// A public function of the tth class.
// Calculates and sets the elements of 'runningTotal' using an accumulator with 'fourByFourNumbers'.
// Displays a command line message with each element of 'runningTotal'.
//--------------------------------------------------------------------------------------------------------------
void tth::calculateAndShowRunningTotalFourByFourNumbers()
{
cout << endl;
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 4; j++)
{
runningTotal[i] = runningTotal[i] + fourByFourNumbers[j][i];
}
runningTotal[i] = runningTotal[i] % 26;
cout << runningTotal[i] << " ";
}
}
//--------------------------------------------------------------------------------------------------------------
// shiftRowsAccordingly()
//--------------------------------------------------------------------------------------------------------------
// A public function of the tth class.
// Sets the elements of the private int 2D array 'temp' to a rearranged version of 'fourByFourNumbers'.
// Elements of rows 1 - 3 are shifted in a circular pattern.
// Row 1 elements are shifted to the left by 1 index. Row 2 elements are shifted to the left by 2 indicies.
// Row 3 elements are shifted to the left by 3 indicies. Row 4 elements are swapped: 1 <> 4 and 2 <> 3.
//--------------------------------------------------------------------------------------------------------------
void tth::shiftRowsAccordingly()
{
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 4; j++)
{
if(i == 0)
{
if(j == 3)
{
temp[i][j] = fourByFourNumbers[i][j - 3];
}
else
{
temp[i][j] = fourByFourNumbers[i][j + 1];
}
}
else if(i == 1)
{
if(j == 2)
{
temp[i][j] = fourByFourNumbers[i][j - 2];
}
else if(j == 3)
{
temp[i][j] = fourByFourNumbers[i][j - 2];
}
else
{
temp[i][j] = fourByFourNumbers[i][j + 2];
}
}
else if(i == 2)
{
if(j == 1)
{
temp[i][j] = fourByFourNumbers[i][j - 1];
}
else if(j == 2)
{
temp[i][j] = fourByFourNumbers[i][j - 1];
}
else if(j == 3)
{
temp[i][j] = fourByFourNumbers[i][j - 1];
}
else
{
temp[i][j] = fourByFourNumbers[i][j + 3];
}
}
else
{
if(j == 1)
{
temp[i][j] = fourByFourNumbers[i][j + 1];
}
else if(j == 2)
{
temp[i][j] = fourByFourNumbers[i][j - 1];
}
else if(j == 3)
{
temp[i][j] = fourByFourNumbers[i][j - 3];
}
else
{
temp[i][j] = fourByFourNumbers[i][j + 3];
}
}
}
}
}
//--------------------------------------------------------------------------------------------------------------
// calculateAndShowRunningTotalTemp()
//--------------------------------------------------------------------------------------------------------------
// A public function of the tth class.
// Calculates and sets the elements of 'runningTotal' using an accumulator with 'temp'.
// Displays a command line message with each element of 'runningTotal'.
//--------------------------------------------------------------------------------------------------------------
void tth::calculateAndShowRunningTotalTemp()
{
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 4; j++)
{
runningTotal[i] = runningTotal[i] + temp[j][i];
}
runningTotal[i] = runningTotal[i] % 26;
cout << runningTotal[i] << " ";
}
}
//--------------------------------------------------------------------------------------------------------------
// matchNumberToLetter()
//--------------------------------------------------------------------------------------------------------------
// A public function of the tth class.
// Sets the elements of the private char 2D array 'fourLetterHash' to a letter A - Z.
// The letter is determined by the value of a 'runningTotal' element.
//--------------------------------------------------------------------------------------------------------------
void tth::matchNumberToLetter()
{
for(int i = 0; i < 4; i++)
{
switch(runningTotal[i])
{
case 0:
fourLetterHash[i] = 'A';
break;
case 1:
fourLetterHash[i] = 'B';
break;
case 2:
fourLetterHash[i] = 'C';
break;
case 3:
fourLetterHash[i] = 'D';
break;
case 4:
fourLetterHash[i] = 'E';
break;
case 5:
fourLetterHash[i] = 'F';
break;
case 6:
fourLetterHash[i] = 'G';
break;
case 7:
fourLetterHash[i] = 'H';
break;
case 8:
fourLetterHash[i] = 'I';
break;
case 9:
fourLetterHash[i] = 'J';
break;
case 10:
fourLetterHash[i] = 'K';
break;
case 11:
fourLetterHash[i] = 'L';
break;
case 12:
fourLetterHash[i] = 'M';
break;
case 13:
fourLetterHash[i] = 'N';
break;
case 14:
fourLetterHash[i] = 'O';
break;
case 15:
fourLetterHash[i] = 'P';
break;
case 16:
fourLetterHash[i] = 'Q';
break;
case 17:
fourLetterHash[i] = 'R';
break;
case 18:
fourLetterHash[i] = 'S';
break;
case 19:
fourLetterHash[i] = 'T';
break;
case 20:
fourLetterHash[i] = 'U';
break;
case 21:
fourLetterHash[i] = 'V';
break;
case 22:
fourLetterHash[i] = 'W';
break;
case 23:
fourLetterHash[i] = 'X';
break;
case 24:
fourLetterHash[i] = 'Y';
break;
case 25:
fourLetterHash[i] = 'Z';
break;
default:
cout << "Hmmmm";
}
}
}
//--------------------------------------------------------------------------------------------------------------
// showFourLetterHashNumbers()
//--------------------------------------------------------------------------------------------------------------
// A public function of the tth class.
// Displays a command line message with each element of 'fourLetterHash'.
//--------------------------------------------------------------------------------------------------------------
void tth::showFourLetterHashNumbers()
{
for(int i = 0; i < 4; i++)
{
cout << fourLetterHash[i] << " ";
}
}
EDIT
Gah. I put so much effort into this post just to find out it was a simple problem with command line compiling. Thanks for the answers.
Answer
Use the command below to compile the definition file and main program file.
cl tth.cpp useTTH.cpp
You should compile both tth.cpp and useTTH.cpp
with command line:
cl tth.cpp useTTH.cpp
You forgot to include all your source files in your compiler command. As tth.cpp is not included, it cannot find the symbols corresponding to the functions/variables defined there.