Post: C++ basic setup application
01-09-2014, 01:12 AM #1
Script
Banned
(adsbygoogle = window.adsbygoogle || []).push({}); I was browsing these forums and saw no one has posted much on C++ so I figured I would. Here's a basic C++ setup for a console application for those new people in programming.

You must login or register to view this content.

Sources


main.cpp

    #include "keyHandler.h"

int main()
{
SetConsoleTitle("Basic setup C++ application");
cout << "Welcome to Basic setup C++ application
Press A to say a
Press B to say b
Press C to say c" << endl;
keyHandler::runKeys();
return 0;
}



keyHandler.h

    #include <iostream>
#include <Windows.h>
#include <conio.h>

using namespace std;

// needs to be public so main.cpp can access it
class keyHandler {
public:
static void runKeys();
};



keyHandler.cpp

    #include "keyHandler.h"


// you are going to want to put your main code for the functions in here
// such as if on keypress for the letter a you will want to do it like this
// if (key == 'a' || key == 'A'Winky Winky
// {
// // run function/code here
// }


void keyHandler::runKeys()
{
char key; // need a char (1 character) key (A, B, C ect)
while(true)
{
// you want to pause every keypress to avoid app closing
key = _getch(); // _getch() is a "pause" and is from in conio
if (key == 'a' || key == 'A'Winky Winky
cout << "You pressed A" << endl;
else if (key == 'b' || key == 'B'Winky Winky
cout << "You pressed B" << endl;
else if (key == 'c' || key == 'C'Winky Winky
cout << "You pressed C" << endl;
else
cout << "This button isn't supported in this application!" << endl;
}
}




Now I know there are many other ways to do keypress, but I like this way as I use it in every C++ app I make.
Compiled with MinGW command line (using this command [g++ main.cpp keyHandler.cpp -o test_app.exe -static-libgcc -static-libstdc++ -lmingw32])
Last edited by Script ; 01-09-2014 at 01:14 AM.

The following 2 users say thank you to Script for this useful post:

ilasthope,

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo