I was just wondering how I could convert my C++ console application that calculates the area of a circle to a GUI-based application where the user could click on a calculate button and see the results. I hear talk about this model-controller-view method but I'm not sure if I'm misunderstanding it. Like I don't see how you actually separate the user interface design from code logic, since you're essentially coding using a GUI toolkit which seems you'd be working along side the programming logic. Anyhow, my code is below.
// Calculate the area of a circle
// Michael E. Walker
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
usingnamespace std;
int main() {
constdouble pi = 3.14159;
cout << "Please enter the radius of the circle. ";
string input;
cin >> input;
double radius;
stringstream(input) >> radius;
double result = 2*pi*radius*radius;
cout << "The area of the circle is " << result << ". Would you like to save it? Enter y or n." << endl;
char save;
cin >> save;
if (save == 'y' || save == 'Y') {
cout << "Please enter a file name to save the results to: " << endl;
char file[100]; // Couldn't use C++'s string datatype since ofstream's constructor wouldn't allow it.
cin >> file;
ofstream my_file(file);
if (!my_file.is_open()) {
cerr << "An unknown error occured while opening the file." << endl;
return -1;
}
my_file << "The result of the computation is " << result;
my_file.close();
}
return 0;
}
I was just wondering how I could convert my C++ console application that calculates the area of a circle to a GUI-based application where the user could click on a calculate button and see the results.
Which GUI library are you planning to use?
I don't see how you actually separate the user interface design from code logic, since you're essentially coding using a GUI toolkit which seems you'd be working along side the programming logic.
It depends on the program, for a program in which the graphics are the most important thing you can first write the graphical part, then the contents. If the logic is more important, you can first design all the internal part with a console interface and then build a GUI from that. -Your case is the latter-
Well let's say I use the Windows API to convert my application to GUI-based. I could of course look up a tutorial on this to become more familiar with it, but where would be the best place to start in the conversion from console-based to GUI? And do you think the Windows API is a good way to do it? I am on Windows.
Yeah I see what you mean, looks like a lot more code since it's a different programming approach. I've been coding in standard C++ for quite a while (about a year now so I'm familiar with the language), but when it comes to GUI-based apps that's totally new to me and something I've recently started looking at. I may take a look around at GUI libraries to see which one looks best. I also need to take into account that I'm blind, so I'm thinking that learning the APIs is the best way to go rather than trying to learn a visual form designer, although the express editions of Visual Studio have interfaced well with my JAWS for Windows screen reading software. But yes I do see a difference in the way that you'd need a WinMain() procedure to handle the events, and that that's the Windows equivalent to main() in general.
Bazzy, now I see what the whole MVC thing is that I was talking about earlier. Before, no I didn't understand what it was but I found an article on Microsoft's website that explains how they separate U.I. design from programming/business logic. It looks like just a kind of fancy moduler approach to programming where you can test each in parts.
Anyhow, what approach do you personally take to GUI design? I went to a Windows API tutorial and found that that method is rather complex. What's your opinion of using the .NET framework with C++ for programming? I know it's not the standard C++ but do you think it's the easiest?
C++/CLI (the C++ like language used with .Net) is cumbersome to use.
If you find WinAPI GUI hard to use try some other libraries ( eg: Qt, GTK+, wxWidgets )
But anyway, I suggest you first learning a bit the library by making useless HelloWorld GUIs, When you are more confident with the library you can try to make more useful programs
Yeah that sounds like a good idea. I've seen the API for QT before and it looked pretty nice to learn, such as straight forward names and easy to use, plus I like the fact that it's cross-platform for when I do come productive. I'm majoring in Information Systems at my university so I'm thinking that should help me become productive too.