0

I was wondering if i could create a window using my existing project code. This is a school project. However, I have completed the actual coding part and just wanted to make the project fancier, so to say. Thank you so much in advance for all the support.

Here is the actual code in case it would be of any help. It's a bit lengthy so be warned :) Once again, thank you in advance

#define NOMINMAX
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <windows.h>
#include <Windows.h>
#include <chrono>
#include <string>
#include <fstream>
#include <iomanip>
#include <cstdlib>


using namespace std;


int key[3][3];
double inverted[3][3];
int store[1][3] = { 0 };
int conv[666];


int random1()
{
    unsigned long long int xRan;
    srand(time(NULL));

    xRan = rand() % 9999 + 1;

    return xRan;
}
int random2()
{
    unsigned long long int xRan;

    xRan = rand() % 9999 + 1;  

    return xRan;
}
int  random3()
{
    int xRan;

    xRan = rand() % 9999 + 1;

    return xRan;
}
void clear_screen(char fill = ' ') {
    COORD tl = { 0, 0 };
    CONSOLE_SCREEN_BUFFER_INFO s;
    HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
    GetConsoleScreenBufferInfo(console, &s);
    DWORD written, cells = s.dwSize.X * s.dwSize.Y;
    FillConsoleOutputCharacter(console, fill, cells, tl, &written);
    FillConsoleOutputAttribute(console, s.wAttributes, cells, tl, &written);
    SetConsoleCursorPosition(console, tl);
}
int convert(char letter) {
    int conv;
    conv = (int)letter;

    return conv;
}
void reverseMult(double inv[3][3], int decode[1][3])
{
    store[0][0] = decode[0][0] * inv[0][0] + decode[0][1] * inv[1][0] + decode[0][2] * inv[2][0] + 0.5;
    store[0][1] = decode[0][0] * inv[0][1] + decode[0][1] * inv[1][1] + decode[0][2] * inv[2][1] + 0.5;
    store[0][2] = decode[0][0] * inv[0][2] + decode[0][1] * inv[1][2] + decode[0][2] * inv[2][2] + 0.5;
}
void matrixMult(int q, int w, int e, int a[3][3])
{

    int A[1][3] = { q, w, e };
    int B[3][3] = {
        { a[0][0], a[0][1], a[0][2] },
        { a[1][0], a[1][1], a[1][2] },
        { a[2][0], a[2][1], a[2][2] }
    };


    store[0][0] = A[0][0] * B[0][0] + A[0][1] * B[1][0] + A[0][2] * B[2][0];
    store[0][1] = A[0][0] * B[0][1] + A[0][1] * B[1][1] + A[0][2] * B[2][1];
    store[0][2] = A[0][0] * B[0][2] + A[0][1] * B[1][2] + A[0][2] * B[2][2];

        //cout << store[0][0] << endl << store[0][1] << endl << store[0][2] << endl << endl;


}
char reverseConv(int x){

    char conv;
    conv = (char)x;

    return conv;
}
void inverse(int key[3][3], double det){

        int cofactor[3][3] = {
            { (key[1][1] * key[2][2] - key[1][2] * key[2][1]), -(key[1][0] * key[2][2] - key[1][2] * key[2][0]), (key[1][0] * key[2][1] - key[1][1] * key[2][0]) },
            { -(key[0][1] * key[2][2] - key[0][2] * key[2][1]), (key[0][0] * key[2][2] - key[0][2] * key[2][0]), -(key[0][0] * key[2][1] - key[0][1] * key[2][0]) },
            { (key[0][1] * key[1][2] - key[0][2] * key[1][1]), -(key[0][0] * key[1][2] - key[0][2] * key[1][0]), (key[0][0] * key[1][1] - key[0][1] * key[1][0]) }
        };

        for (int i = 0; i < 3; ++i)
        {
            for (int j = 0; j < 3; ++j)
            {
                inverted[i][j] =det * cofactor[i][j];
            }
        }
    } 

int main()
{
    while (1){
    cout << "Would you like to encrypt or decrypt?(e/d)\n " << endl;
    string ende;
    cin >> ende;

    clear_screen();

    if (ende == "e")
    {

        cout << "Please enter a name for the message: " << endl << endl;
        string file;
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        getline(cin, file);

        clear_screen();

        file += ".txt";
        ofstream encrypt;
        encrypt.open(file);


        string message;
        cout << "Please enter the message you would like to encrypt: " << endl << endl;
        //cin.ignore(numeric_limits<streamsize>::max(), '\n'); --- Not needed anymore, uncomment if you cannot input message
        getline(cin, message);
        if (message.length() % 3 != 0)
            message += ' ';
        if (message.length() % 3 != 0)
            message += ' ';

        clear_screen();

        for (int i = 0; i < message.length(); ++i)
        {
            conv[i] = convert(message[i]);

        }


        int det = 0;

        while (1){
            key[0][0] = random1();              //1
            key[0][1] = random2();              //2
            key[0][2] = random3();              //3
            key[1][0] = random1() * 13 / 7;     //4
            key[1][1] = random2() * 23 / 7;     //5
            key[1][2] = random3() * 33 / 7;     //6
            key[2][0] = random1() * 18 / 15;    //7
            key[2][1] = random2() * 18 / 12;    //8
            key[2][2] = random3() * 18 / 10;    //9


            det = key[0][0] * key[1][1] * key[2][2] + key[0][1] * key[1][2] * key[2][0] + key[0][2] * key[1][0] * key[2][1]
                - key[0][2] * key[1][1] * key[2][0] - key[0][0] * key[1][2] * key[2][1] - key[0][1] * key[1][0] * key[2][2];

            if (det != 0)
                break;
        }

        encrypt << key[0][0] << ' ' << key[0][1] << ' ' << key[0][2] << ' '
            << key[1][0] << ' ' << key[1][1] << ' ' << key[1][2] << ' '
            << key[2][0] << ' ' << key[2][1] << ' ' << key[2][2] << endl << endl;

        int a, b, c;
        int count = 0;

        for (int i = 0; i < (message.length)() / 3; ++i)
        {
            int counting = 0;
            a = conv[count];
            count++;
            b = conv[count];
            count++;
            c = conv[count];
            count++;


            matrixMult(a, b, c, key);


            encrypt << store[0][counting] << ' ';
            counting++;
            encrypt << store[0][counting] << ' ';
            counting++;
            encrypt << store[0][counting] << endl;

        }




        encrypt.close();

        Sleep(750);
        cout << "Your message has been encrypted." << endl;
        Sleep(750);
        cout << "Please check " << file << " for the encrypted message and key" << endl << endl;
        Sleep(750);
    }


    if (ende == "d")
    {


        cout << "Please enter the name of the file you would like to decrypt: " << endl << endl;

        string file;
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        getline(cin, file);

        clear_screen();

        file += ".txt";
        ifstream decrypt;
        decrypt.open(file);

        int key[3][3];

        for (int i = 0; i < 3; ++i)
        {
            for (int k = 0; k < 3; ++k){
                decrypt >> key[k][i];
            }
        }

        int det = 0;
        det = key[0][0] * key[1][1] * key[2][2] + key[0][1] * key[1][2] * key[2][0] + key[0][2] * key[1][0] * key[2][1] - key[0][2] * key[1][1] * key[2][0] - key[0][0] * key[1][2] * key[2][1] - key[0][1] * key[1][0] * key[2][2];

        double detInv = 1;
        detInv /= det;

        //double inv;

        inverse(key, detInv);

        int out[1][3];
        int count = 0;
        while (!decrypt.eof()){
            for (int i = 0; i < 3; ++i)
            {
                decrypt >> out[0][i];
            }

            reverseMult(inverted, out);

            count++;

            char a, b, c;

            a = reverseConv(store[0][0]);
            b = reverseConv(store[0][1]);
            c = reverseConv(store[0][2]);

            if (decrypt.eof())
                break;

            cout << a << b << c;

        }
    }
        cout << endl << endl;
        cout << "Would you like to continue?(y/n) ";
        char again;
        cin >> again;

        if (again != 'y')
            exit(0);
        clear_screen();
}
        return 0;
} 
7
  • I suggest you place the characters into an array. The index of the found character in the array can be assigned to conv. Your switch statement is a waste of coding lines. Commented Mar 5, 2015 at 16:00
  • An easy way to create a window is by using Qt. Commented Mar 5, 2015 at 16:49
  • Thank you, I did ended up doing int conv = (int)letter; Commented Mar 5, 2015 at 16:51
  • Thank you Deepfreeze, I will try it out Commented Mar 5, 2015 at 16:54
  • How would I transfer the visual studios project to Qt? And how would I then actually create the window? Commented Mar 5, 2015 at 18:09

2 Answers 2

2

Yes, you can convert your project to a Windowing application. You have two choices:

  • Use Windows (native) API
  • Use graphics framework

Windows API

The Windows API is the direct method for creating windows. However, it is a lot of code, lots of chances for defects to be injected. It is a good learning experience about how the Windowing system works. Get Petzold's book.

GUI Framework

There are a lot of GUI frameworks out there. These C++ frameworks have simplified the GUI and Widget creation, using object oriented programming. There are many out there, so search the internet for "GUI Framework C++ review".

A Different Programming Perspective

In your present project, the OS executes the program and statements are executed in order. A windowing system is based on event driven programming. In summary, your GUI is waiting for an event to occur.

A simple example for your project is a window with a single button. When the User clicks on the button, the Windowing system sends a message to the button event handler. The event handler is a function that will execute your code.

Sign up to request clarification or add additional context in comments.

1 Comment

That is a perfect explanation. Thank you!!
0

As Thomas said, yes you can migrate your code to a Windows application, by either going native with Win32 or by either using a C++ GUI Framework (QT, wxWindows, ...).

However, you will need to invest time to learn one of the solution. I would suggest to learn a C++ Framework, programming with low-level Win32 api is not very used today.

Although it's off-topic, I would suggest some improvements to your code.

First, you shouldn't use goto, and replace them by a while. You can replace

again:
    xRan = rand() % 9999 + 1;
    if (xRan <1)
        goto again;

By

do{
   xRan = rand() % 9999 + 1;
} while (xRan < 1);

Note that in this case, a goto or a while is useless as xRan will always be superior or equal to 1 (rand() always returns a positive value)

Also you can replace convert and reverseConv very long functions by a constant array of struct values (struct contains a int and a const char*). convert and reverseConvert functions would only parse the array to find a proper match.

6 Comments

I did replace convert and reverseConv with a simple convsion to and from the ascii numeric forms. Object oriented programming(with structs and classes and all) is a junior year thing. I tried to use structs in this program for the conversion, but couldn't figure it out. And thank you for the information about random always being positive. Also, what framework would you suggest?
Qt would be your best choice, as it is a framework widely used in the industry. Also if you want to change of programming language in the future, you won't lose the time you invested in it, as Qt has been ported to many languages. The only concern would be its license, it is a LGPL framework for the free version.
Would Qt be a good IDE to switch over to completely? That is, coming from Visual Studios Express 2013. Also, should I download Qt Creator, or just the plugin for VS?
I used the VS plugin and it did the job perfectly. Note that you need to use VS 2013 community edition (not Express edition) to be able to use this plugin.
ah okay. and is community different from express? or not really?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.