• Coding
  • [C++]making a GUI for my program.

Hello,this is my first topic in the programming section, i started to learn programming and i am still a beginner so this is one of my first programs, the programs is to calculate the price of a car rental based on which car you choose and the number of kilometers passed by the car,i know that the program is maybe not very well polished but it's one of my first tries.so here is the program...
#include <iostream>
using namespace std;
int car,carp;
double lesthn100,morethn100,price,KM;
int main ()
{
    cout<<"1: $500 \n\n2: $600 \n\n3: $700 \n\nPlease enter the number of the car: ";
    cin>>car;
    if (car == 1)
    carp = 500;
    else if (car == 2)
    carp = 600;
    else if (car == 3)
    carp =700;
    else {cout<<"invalid number!";
    cin.get();
    cin.get();
    return 0;
    }
    cout<<"\nPlease enter number of KM: ";
    cin>>KM;
    if (KM <= 100){
    price = (KM*0.25)+carp;
    cout<<"\nthe price is: $"<<price;
    }
    else if (KM > 100) {
        lesthn100 = 100*0.25;
        morethn100 = (KM-100)*0.15;
        price = lesthn100+morethn100+carp;
        cout<<"the price is: $"<<price;
    }
        cin.get();
        cin.get();
        return 0;
}
so what you think ? finally now i am thinking of creating a GUI for the program so if you can give me any help a good link or a tool . thanks (although after googeling i found that it's hard to make a GUI for an already written code.)
If you want something C++ to start with, you can check Qt.
C++ is not very easy for beginners. C#, Java, or Python (if you have the luxury of the choice) may be better options to do a little GUI. Do continue learning C++ though, and use it as a text exercise language till your comfy enough with it to do more interesting stuff (GUI on any of the available platforms using Qt, GTK, Win32 API, MFC, X Windows...)
I agree with arithma. You should maybe think of starting with an easier language. There is no "beginner-perfect" language. It depends on what you want to do and your personal preferences. I would strongly recommend Python, which makes programming really fun while allowing you to create really cool things in no time.

As for the C++ code you posted, I reviewed it and here are my remarks. I should say that I'm not a C++ developer, I code mainly in C; so some things might be different between the two languages. If I'm mistaken in what I'm writing, by all means somebody please correct me.

l3-l4: Don't declare your variables there. You should be declaring them inside the main function.

l8-l15: Try to use a switch() statement instead of nested if/else if/else. It makes the code cleaner.

l26: You can simply replace this by "else"; no need to add the condition.

l27: You don't need to declare a variable if its value is not going to change. You can replace it by the value directly, or use a preprocessor macro.

General: review your identation and add comments. It's true that this particular piece of code is small and comprehensible, but it wouldn't hurt to take up good practice from the start.

I have rewrote your code by incorporating the remarks above. Here it is. Make sure to read it and understand it, and understand why those changes make the code better.
#include <iostream>
#define FIRST100KM_PRICE 100*0.25
using namespace std;

int main (){

    int car,carp;
    double lesthn100,morethn100,price,KM;

    // Picks a car category.
    cout<<"1: $500 \n\n2: $600 \n\n3: $700 \n\nPlease enter the number of the car: ";
    cin>>car;

    switch (car){

        case 1: carp = 500; break;
        case 2: carp = 600; break;
        case 3: carp = 700; break;
        default:    cout<<"invalid number!";cin.get();return 0;
    }

    // Caluclates price based on KM. 
    cout<<"\nPlease enter number of KM: ";
    cin>>KM;

    if (KM <= 100){
        price = (KM*0.25)+carp;
        cout<<"\nthe price is: $"<<price;
    }
    else {
        morethn100 = (KM-100)*0.15;
        price = FIRST100KM_PRICE + morethn100 + carp;
        cout<<"the price is: $"<<price;
    }

    cin.get();
    return 0;
}
rahmu wrotel27: You don't need to declare a variable if it's value is not going to change. You can replace it by the value directly, or use a preprocessor macro.
Macros are definitely the realm of #define, for constants it's easier to stick with const.

I think it's better C++ style to use const instead of #define. The compiler can deal with a const variable, ensuring type safety and scoping rules apply. Also, it's visible to the debugger. A #define is global and type-less.

It's a C-style thing to do #define, although const exists in C as well. I'm really not sure why this is true, I even do it in my C programs.

I third the Python suggestion!
thanks all for you reply,so i am going to start programming with Python,and @rahmu thank u for your advices and notations and for the code you posted, but your code crashes in debugging mode(well it's not a real crash it's that the program close before you can read "invalid number!" and before you can read the price) so i added couple of commands to fix that.(the double cin.get before return 0)and you declared "lesthn100"without using it
#include <iostream>
#define FIRST100KM_PRICE 100*0.25
using namespace std;

int main (){

    int car,carp;
    double morethn100,price,KM;

    // Picks a car category.
    cout<<"1: $500 \n\n2: $600 \n\n3: $700 \n\nPlease enter the number of the car: ";
    cin>>car;

    switch (car){

        case 1: carp = 500; break;
        case 2: carp = 600; break;
        case 3: carp = 700; break;
        default:    cout<<"invalid number!";cin.get();cin.get();return 0;
    }

    // Caluclates price based on KM.
    cout<<"\nPlease enter number of KM: ";
    cin>>KM;

    if (KM <= 100){
        price = (KM*0.25)+carp;
        cout<<"\nthe price is: $"<<price;
    }
    else {
        morethn100 = (KM-100)*0.15;
        price = FIRST100KM_PRICE + morethn100 + carp;
        cout<<"the price is: $"<<price;
    }

    cin.get();
    cin.get();
    return 0;
}
It's a C-style thing to do #define, although const exists in C as well. I'm really not sure why this is true, I even do it in my C programs.
I bet it's a history thing. C and C++ are evolving, and C sometimes takes what it can from the C++ realm.
After reading about it, it's true that C++ discourages the use of #define.

I tend to use it a lot in C. To define FLAGS, or RETURN_CODE. They're also great to do something like that.
int array[MAX_SIZE]
@rahmu: They borrowed the concept from C++ back to C for a reason.