Change switch statements to two or more functions, each [closed] - c++

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
For the 3 switch statements, I am to replace each switch statement with two or more function calls. This is to replace an "incomplete" in my beginning C++ class last semester so that I can get a loan and I've got no idea where to start.
I tried quite literally taking the switch statements and putting them into their own functions, of the switch statements themselves only, and, of course, that created numerous syntax errors (e.g. counter, random_number). I've no idea how to do it, how to return proper values to main() and get the program to communicate with other parts of the program (e.g. variables defined/initialized in main). As one can see, I'm just pretty lost here and would like some guidance toward figuring this out. I'm not asking anyone to do this for me, just some guidance (my knowledge on C++ is limited and there are time constraints).
// random.cpp : Defines entry point for the console application.
//
#include <iostream>
#include <iomanip>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
//random number generator prototypes
void randomize(void);
void randomize(int seed);
int random(void);
int random(int upper_bound);
int random(int upper_bound, int lower_bound);
int main()
{
int upper_bound = 999;
int lower_bound = 100;
int n_random_numbers = 1000;
randomize();
int counter_0 = 0;
int counter_1 = 0;
int counter_2 = 0;
int counter_3 = 0;
int counter_4 = 0;
int counter_5 = 0;
int counter_6 = 0;
int counter_7 = 0;
int counter_8 = 0;
int counter_9 = 0;
for(int counter = 1; counter <= n_random_numbers; counter++)
{
int random_number = random(upper_bound, lower_bound);
int digit_1 = random_number % 10; random_number = random_number / 10;
switch(digit_1)
{
case 0:
counter_0++;
break;
case 1:
counter_1++;
break;
case 2:
counter_2++;
break;
case 3:
counter_3++;
break;
case 4:
counter_4++;
break;
case 5:
counter_5++;
break;
case 6:
counter_6++;
break;
case 7:
counter_7++;
break;
case 8:
counter_8++;
break;
case 9:
counter_9++;
break;
}
int digit_2 = random_number % 10; random_number = random_number / 10;
switch(digit_2)
{
case 0:
counter_0++;
break;
case 1:
counter_1++;
break;
case 2:
counter_2++;
break;
case 3:
counter_3++;
break;
case 4:
counter_4++;
break;
case 5:
counter_5++;
break;
case 6:
counter_6++;
break;
case 7:
counter_7++;
break;
case 8:
counter_8++;
break;
case 9:
counter_9++;
break;
}
int digit_3 = random_number % 10; random_number = random_number / 10;
switch(digit_3)
{
case 0:
counter_0++;
break;
case 1:
counter_1++;
break;
case 2:
counter_2++;
break;
case 3:
counter_3++;
break;
case 4:
counter_4++;
break;
case 5:
counter_5++;
break;
case 6:
counter_6++;
break;
case 7:
counter_7++;
break;
case 8:
counter_8++;
break;
case 9:
counter_9++;
break;
}
}
cout << "0 occurs " << counter_0 << " times" << endl;
cout << "1 occurs " << counter_1 << " times" << endl;
cout << "2 occurs " << counter_2 << " times" << endl;
cout << "3 occurs " << counter_3 << " times" << endl;
cout << "4 occurs " << counter_4 << " times" << endl;
cout << "5 occurs " << counter_5 << " times" << endl;
cout << "6 occurs " << counter_6 << " times" << endl;
cout << "7 occurs " << counter_7 << " times" << endl;
cout << "8 occurs " << counter_8 << " times" << endl;
cout << "9 occurs " << counter_9 << " times" << endl;
system("pause");
return 0;
}
//random number generators
void randomize(void)
{
srand(unsigned(time(NULL)));
}
void randomize(int seed)
{
srand(unsigned(seed));
}
int random(void)
{
return rand();
}
int random(int upper_bound)
{
return rand() % (upper_bound + 1);
}
int random(int upper_bound, int lower_bound)
{
if(upper_bound < lower_bound)
{
int t = upper_bound;
upper_bound = lower_bound;
lower_bound = t;
}
int range = upper_bound - lower_bound + 1;
int number = rand() % range + lower_bound;
return number;
}
I added these functions, to see if it would work (Figure 1.1)
(I added counter, upper_bound, lower_bound, n_random_numbers because I couldn't figure out how to have the function read those variables from main(). I tried making them into a function, calling them in main, and calling them in my created functions, but that definitely didn't work. These added functions replace the switches in the original with function calls (see: Figure 1.2). It compiles, but the output returns that "0 occurs 0 times, 1 occurs 0 times, etc."
FIGURE 1.1
int switch1 (int switch_1)
{
int upper_bound = 999;
int lower_bound = 100;
int n_random_numbers = 1000;
int counter = 0;
randomize();
int counter_0 = 0;
int counter_1 = 0;
int counter_2 = 0;
int counter_3 = 0;
int counter_4 = 0;
int counter_5 = 0;
int counter_6 = 0;
int counter_7 = 0;
int counter_8 = 0;
int counter_9 = 0;
int random_number = random(upper_bound, lower_bound);
int digit_1 = random_number % 10; random_number = random_number / 10;
switch(digit_1)
{
case 0:
counter_0++;
break;
case 1:
counter_1++;
break;
case 2:
counter_2++;
break;
case 3:
counter_3++;
break;
case 4:
counter_4++;
break;
case 5:
counter_5++;
break;
case 6:
counter_6++;
break;
case 7:
counter_7++;
break;
case 8:
counter_8++;
break;
case 9:
counter_9++;
break;
}
}
int switch2 (int switch_2)
{
int upper_bound = 999;
int lower_bound = 100;
int n_random_numbers = 1000;
int counter = 0;
randomize();
int counter_0 = 0;
int counter_1 = 0;
int counter_2 = 0;
int counter_3 = 0;
int counter_4 = 0;
int counter_5 = 0;
int counter_6 = 0;
int counter_7 = 0;
int counter_8 = 0;
int counter_9 = 0;
int random_number = random(upper_bound, lower_bound);
int digit_2 = random_number % 10; random_number = random_number / 10;
switch(digit_2)
{
case 0:
counter_0++;
break;
case 1:
counter_1++;
break;
case 2:
counter_2++;
break;
case 3:
counter_3++;
break;
case 4:
counter_4++;
break;
case 5:
counter_5++;
break;
case 6:
counter_6++;
break;
case 7:
counter_7++;
break;
case 8:
counter_8++;
break;
case 9:
counter_9++;
break;
}
}
int switch3 (int switch_3)
{
int upper_bound = 999;
int lower_bound = 100;
int n_random_numbers = 1000;
int counter = 0;
randomize();
int counter_0 = 0;
int counter_1 = 0;
int counter_2 = 0;
int counter_3 = 0;
int counter_4 = 0;
int counter_5 = 0;
int counter_6 = 0;
int counter_7 = 0;
int counter_8 = 0;
int counter_9 = 0;
int random_number = random(upper_bound, lower_bound);
int digit_3 = random_number % 10; random_number = random_number / 10;
switch(digit_3)
{
case 0:
counter_0++;
break;
case 1:
counter_1++;
break;
case 2:
counter_2++;
break;
case 3:
counter_3++;
break;
case 4:
counter_4++;
break;
case 5:
counter_5++;
break;
case 6:
counter_6++;
break;
case 7:
counter_7++;
break;
case 8:
counter_8++;
break;
case 9:
counter_9++;
break;
}
}
FIGURE 1.2
for(int counter = 1; counter <= n_random_numbers; counter++)
{
int random_number = random(upper_bound, lower_bound);
int digit_1 = random_number % 10; random_number = random_number / 10;
int digit_2 = random_number % 10; random_number = random_number / 10;
int digit_3 = random_number % 10; random_number = random_number / 10;
switch1 (digit_1);
switch2 (digit_2);
switch3 (digit_3);
}

No switch needed:
int counters[10] = {};
for(int counter = 1; counter <= n_random_numbers; counter++)
{
int random_number = random(upper_bound, lower_bound);
int digit_1 = random_number % 10; random_number = random_number / 10;
++counters[digit_1];
int digit_2 = random_number % 10; random_number = random_number / 10;
++counters[digit_2];
int digit_3 = random_number % 10; random_number = random_number / 10;
++counters[digit_3];
}

Related

Number System in C++

I have a program that should translate values ​​from one number system to another, but I have a problem with "_itoa_s" writes that [Error] '_itoa_s' was not declared in this scope I tried to connect libraries <cstdlib> and <stdlib.h> I also tried replacing itoa with "snprintf" but it does not help in the compiler there are even more errors, please help me fix the error,
Here is the code:
#include <cstring>
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
setlocale(LC_ALL, "rus");
int in, iz, k, s = 0, p;
char str[255];
cout << "Enter the number system from which you want to translate" << endl;
cin >> iz;
cout << "Enter the number system to which we will translate" << endl;
cin >> in;
cout << "Enter the number" << endl;
cin >> str;
p = strlen(str) - 1;
for (int i = 0; i < str[i]; i++)
{
switch (str[i])
{
case 'a': k = 10; break;
case 'b': k = 11; break;
case 'c': k = 12; break;
case 'd': k = 13; break;
case 'e': k = 14; break;
case 'f': k = 15; break;
case '1': k = 1; break;
case '2': k = 2; break;
case '3': k = 3; break;
case '4': k = 4; break;
case '5': k = 5; break;
case '6': k = 6; break;
case '7': k = 7; break;
case '8': k = 8; break;
case '9': k = 9; break;
case '0': k = 0; break;
}
s = s + k * pow(iz, p);
p--;
}
char result[255];
_itoa_s(s, result, in);
cout << "The result of a translation from a radix " << iz << " to radix " << in << " = " << result;
return 0;
}
Here's an alternative that doesn't involve switch, pow or itoa:
// Notice, the index is the value of the digit.
const std::string digits[] = "0123456789abcdef";
//...
const size_t length = str.len;
// Note: iz is the radix for "input" conversion
int number = 0;
for (unsigned int i = 0; i < length; ++i)
{
const std::string::size_type position = digits.find(str[i]);
number = number * iz; // Shift the number by one digit.
number += position;
}
Notice: error handling, such as invalid digits, is left as an exercise for the OP. Invalid digits are not restricted to characters outside the set, but also digits whose index is greater than the conversion radix.
For ultimate understanding, single step through the code with pen and paper. :-)
You can use the table for converting to the target radix, in (I'd rather use a more descriptive variable name).
std::string translated_number;
while (number > 0)
{
const unsigned int index = number % output_radix; // Output_radix == 'in'
const char digit_character = digits[index];
translated_number.insert(0, 1, digit_character);
number = number / output_radix;
}

String gets messed up for unknown reason

I was trying to make a program to convert Roman numerals to arabic numeral, but for some reason when he gets to the function setNumeri, the input text gets messed up super badly even before it can even get to the conversion part.
The problem is the function setNumeri, you can ignore everything else, because I pasted everything to be sure not to leave anything out.
I apologize for the fact that some variables are in Italian, but it should be pretty straightforward.
Here's the code:
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
class IX{
public:
void setNumeri(string rom);
void setArabo();
int getArabo();
private:
int length;
int Arabo;
int Numeri[];
};
void IX::setNumeri(string rom){
length = rom.length();
for(int i = length - 1; i >= 0; i--) {
cout << rom.at(i) << endl;
switch (rom.at(i)){
case 'I':
Numeri[i] = 1;
break;
case 'V':
Numeri[i] = 5;
break;
case 'X':
Numeri[i] = 10;
break;
case 'L':
Numeri[i] = 50;
break;
case 'C':
Numeri[i] = 100;
break;
case 'D':
Numeri[i] = 500;
break;
case 'M':
Numeri[i] = 1000;
break;
}
}
}
void IX::setArabo(){
int counter = Numeri[length];
for(int i = length - 1; i >= 0; i--){
if(Numeri[i] == 0) {
counter = counter + Numeri[i];
} else if(Numeri[i] > Numeri[i-1]) {
counter = counter - Numeri[i-1];
i--;
} else if(Numeri[i] <= Numeri[i-1]) {
counter = counter + Numeri[i-1];
}
}
Arabo = counter;
}
int IX::getArabo(){
return Arabo;
}
int main(){
IX lol;
string hellothere;
cout << "Roman numeral:" << endl << endl;
cin >> hellothere;
lol.setNumeri(hellothere);
cout << lol.getArabo() << endl;
return 0;
}
When I input XIII, the output is:
I
I
□
-107362937
That last ominous number is the result of the conversion, while the first 3 characters (one is missing in action, and one is not recognised at all) are the output by string.at() that, as you can see, did a wonderful job getting the string characters right. Tried using string[] instead but no success.
What's even weirder is the fact that once I deleted the whole switch case string.at() gets the string right, it looks like the string gets somehow messed up during the switch part. The same happens using an if statement instead of the switch.
Thanks in advance.
try following code it is working perfectly fine.
#include <cstdlib>
#include <string>
using namespace std;
class IX{
public:
void setNumeri(string rom);
int getArabo();
private:
int length;
int Numeri[100];
};
void IX::setNumeri(string rom){
length = rom.length();
for (int i = 0; i < 100; i++)
{
Numeri[i]=-1;
}
for(int i = length - 1; i >= 0; i--) {
cout << rom.at(i) << endl;
switch (rom.at(i)){
case 'I':
Numeri[i] = 1;
break;
case 'V':
Numeri[i] = 5;
break;
case 'X':
Numeri[i] = 10;
break;
case 'L':
Numeri[i] = 50;
break;
case 'C':
Numeri[i] = 100;
break;
case 'D':
Numeri[i] = 500;
break;
case 'M':
Numeri[i] = 1000;
break;
}
}
}
int IX::getArabo(){
int sum=0;
for (int i = 0; i < 100; i++)
{
if (Numeri[i]==-1)
{
return sum;
}
else
{
sum+=Numeri[i];
}
}
}
int main(){
IX lol;
string hellothere;
cout << "Roman numeral:" << endl << endl;
cin >> hellothere;
lol.setNumeri(hellothere);
cout << lol.getArabo() << endl;
return 0;
}

Graph Data Structures using C++

I am making a console application that will print all the paths. But I am having a hard time thinking on how to display all paths from source to destination.
Here is my code:
#include<iostream>
using namespace std;
int arr[8][8] = {{50,30,45,120,0,7,0,0},{30,45,28,4,70,0,0,0},
{50,20,0,38,0,0,0,0},{0,4,30,0,52,0,3,0},{0,75,0,27,0,2,0,3},
{70,0,0,0,2,0,2,0},{0,80,0,73,0,2,0,0},{60,0,90,0,30,0,0,0}};
char vertex[8] = {'A','B','C','D','E','F','G','H'};
void displayPath()
{
system("cls");
int start, end;
char from, to;
cout << "From vertex: ";
cin >> from;
cout << "To: ";
cin >> to;
switch(from)
{
case 'a':case 'A': start = 0; break;
case 'b':case 'B': start = 1; break;
case 'c':case 'C': start = 2; break;
case 'd':case 'D': start = 3; break;
case 'e':case 'E': start = 4; break;
case 'f':case 'F': start = 5; break;
case 'g':case 'G': start = 6; break;
case 'h':case 'H': start = 7; break;
}
switch(to)
{
case 'a':case 'A': end = 0; break;
case 'b':case 'B': end = 1; break;
case 'c':case 'C': end = 2; break;
case 'd':case 'D': end = 3; break;
case 'e':case 'E': end = 4; break;
case 'f':case 'F': end = 5; break;
case 'g':case 'G': end = 6; break;
case 'h':case 'H': end = 7; break;
}
int temp = 0;
int current = start;
if(arr[start][end] > 0)
{
cout << vertex[start] << "->" << vertex[end];
}
else
cout << "No path";
}
void computeDistance()
{
system("cls");
int start,end;
char from, to;
cout << "From vertex: ";
cin >> from;
cout << "To: ";
cin >> to;
switch(from)
{
case 'a':case 'A': start = 0; break;
case 'b':case 'B': start = 1; break;
case 'c':case 'C': start = 2; break;
case 'd':case 'D': start = 3; break;
case 'e':case 'E': start = 4; break;
case 'f':case 'F': start = 5; break;
case 'g':case 'G': start = 6; break;
case 'h':case 'H': start = 7; break;
}
switch(to)
{
case 'a':case 'A': end = 0; break;
case 'b':case 'B': end = 1; break;
case 'c':case 'C': end = 2; break;
case 'd':case 'D': end = 3; break;
case 'e':case 'E': end = 4; break;
case 'f':case 'F': end = 5; break;
case 'g':case 'G': end = 6; break;
case 'h':case 'H': end = 7; break;
}
if(arr[start][end] > 0)
{
cout << arr[start][end] << " meters" << endl;
}
else
cout << "No path";
}
int main()
{
int choice;
cout << "Menu\n\n[1] Display Path\n[2] Compute Distance\n\nChoice: ";
cin >> choice;
switch(choice)
{
case 1: displayPath(); break;
case 2: computeDistance(); break;
}
system("pause");
return 0;
}
This program only gives the source to destination, not all the vertices passed through. This should be the sample output:
From: A
To: F
A -> B -> D -> F
Also, it should follow the concept of shortest path. And will give the total distance. I hope you could help me with this one. Thank you so much in advance.
First of all you need to implement some algorithm which will find shortest path.
Basically there are couple choices:
Dijkstra's algorithm
A* search algorithm
I don't see you have implemented anything which do such search.
The number of various paths between 2 nodes in graph is exponential. That means that there can be a lot of them.
I am not sure if your graph is always built of 8 nodes. In the following solution I have assumed that it is.
Having such a small data you can use very slow and not efficient solutions.
Example: Lets generate every possible permutation of nodes and check all which start from source and check them until they reach destination.
Getting shortest path after checking all paths is pretty easy - just count total distance of each path while checking them.
Some code:
#include <bits/stdc++.h>
using namespace std;
int Graph[8][8];
map <int, bool> used; // to not print same path multiple times
int valid_path(int src, int dest, vector<int>& path)
{
int pathHash=0;
if (path[0]!=src) return -1;
for (size_t i=1; i<path.size(); i++)
{
pathHash= pathHash*10+path[i]; // such solution works only because of small size of data
if (Graph[path[i-1]][path[i]]==0) return -1; // there is no edge between these nodes
if (path[i]==dest) break;
}
return pathHash;
}
void print_path (int dest, vector<int>& path)
{
for (size_t i=0; i<path.size(); i++)
{
cout << path[i] << " ";
if (path[i]==dest) break;
cout << " --> ";
}
}
void generate_paths(int src, int dest)
{
vector<int> perm {0, 1, 2, 3, 4, 5, 6, 7};
do
{
int pathHash= valid_path(src, dest, perm);
if (pathHash!=-1 && used[pathHash]==false)
{
used[pathHash]=true;
print_path(dest, perm);
cout << "\n";
}
} while (next_permutation(perm.begin(), perm.end()));
}
int main () {
for (size_t i=0; i<8; i++)
{
for (size_t j=0; j<8; j++)
{
cin >> Graph[i][j];
}
}
generate_paths(0, 7);
}
/* example input
0 1 0 1 0 1 0 0
0 0 1 0 0 0 0 0
0 0 0 1 0 0 0 0
0 1 0 0 1 0 0 0
0 0 0 0 0 1 0 0
0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0
*/

convert int to char without itoa() [closed]

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.

C++ Error: error LNK2019: unresolved external symbol

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.