Converting Visual Basic 2010 code to C++ ?

It is for a guessing game, I have the Visual Basic code here;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
Public Class Form1
    Dim Numberhidden As Integer
    Dim Numberguess As Integer
    Dim myCounter As Integer = 0


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Numberhidden = Int(Rnd() * 99 + 1)
        Numberguess = InputBox("Please enter your first guess")
        Do While Numberhidden <> Numberguess And myCounter < 5
            If Numberhidden > Numberguess Then
                Numberguess = InputBox("Try a higher number, and enter your new guess")
            ElseIf Numberhidden < Numberguess Then
                Label1.Text = "Try a LOWER number!"
                Numberguess = InputBox("Try a lower number, and enter your new guess")
            Else
                Label1.Text = "Your GUESS is correct!"
                Exit Do
            End If
        Loop
        If Numberhidden = Numberguess Then
            Label1.Text = "You guessed it correct"
        Else
            Label1.Text = "You have run out of opportunites"
        End If


    End Sub

End Class


I'm new to C++ and want to convert it over, but I have no idea how to?
Please help, Thank you!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <cstdlib>
#include <iostream>

int main() {
    srand(time(NULL));
    int numberguess, numberhidden = rand() % 100 + 1, mycounter = 0;
    std::cout << "Enter your first guess: ";
    std::cin >> numberguess;
    
    while ( numberguess != numberhidden and mycounter < 5) {
        std::cout << ( numberhidden > numberguess ? "Try a higher number and enter new guess: ":
        "Try a lower number and enter new guess: " );
        std::cin >> numberguess;
    }
    
    if ( numberguess == numberhidden ) std::cout<<"Yur GUESS is correct!\n";
    else std::cout << "You have run out of opportunites\n";
    
    return 0;
}


Haven't compiled/tested it, but it looks syntactically correct to me
Neither version increments the mycounter. :)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <cstdlib>
#include <iostream>

int main() {
    srand(time(NULL));
    int numberguess, numberhidden = rand() % 100 + 1, mycounter = 0;
    std::cout << "Enter your first guess: ";
    std::cin >> numberguess;
    
    while ( numberguess != numberhidden and mycounter < 5) {
    		mycounter++;
    		std::cout << ( numberhidden > numberguess ? "Try a higher number and enter new guess: ":
    		"Try a lower number and enter new guess: " );
    		std::cin >> numberguess;
    }
    
    if ( numberguess == numberhidden ) std::cout<<"Your GUESS is correct!\n";
    else std::cout << "You have run out of opportunites\n";
    
    return 0;
}


counter incremented :)
Last edited on
Smac89's solution was good but it used the console. If you want forms (a real UI) then you could use MFC, though I have chosen to use Qt. To compile it you need to install the Qt libraries.

I've run this and tested it. It seems to work pretty well. We have button1, and label1 in the main window and a dialogue which asks for inputs.

Edit: I put a simplified version (which doesn't use an auto-generated ui header) below.
Last edited on
For those not using QtCreator to auto-generate the forms. I've made the same thing without that auto-generated file or the ui class:

main.cpp
1
2
3
4
5
6
7
8
9
10
#include "form1.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Form1 w;
    w.show();
    
    return a.exec();
}


form1.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <QMainWindow>
#include <QApplication>
#include <QLabel>
#include <QPushButton>
#include <QVBoxLayout>
#include <QWidget>

class Form1 : public QMainWindow
{
    Q_OBJECT

public:
    explicit Form1(QWidget *parent = 0);

private slots:
    void on_Button1_clicked();

private:
    QWidget*     centralWidget;
    QVBoxLayout* verticalLayout;
    QPushButton* Button1;
    QLabel*      label1;

    int Numberhidden;
    int Numberguess;
    int mycounter = 0;
};


form1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include "form1.h"
#include <QInputDialog>

Form1::Form1(QWidget *parent) : QMainWindow(parent) // Stuff to draw the window
{
    centralWidget  = new QWidget(this);
    verticalLayout = new QVBoxLayout(centralWidget);
    Button1        = new QPushButton(centralWidget);
    label1         = new QLabel(centralWidget);

    Button1->setObjectName(QString::fromUtf8("Button1"));
    Button1->setText(QApplication::translate("Form1", "Push me", 0, QApplication::UnicodeUTF8));

    verticalLayout->addWidget(Button1);
    verticalLayout->addWidget(label1);

    this->setCentralWidget(centralWidget);

    QMetaObject::connectSlotsByName(this);
}

void Form1::on_Button1_clicked() // This is the primary function you provided
{
    Numberhidden = rand() % 100 + 1;
    Numberguess  = QInputDialog::getInteger(this, "", "Please enter your first guess", 50, 1, 100);

    while (Numberhidden != Numberguess && mycounter < 5)
    {
        if (Numberhidden > Numberguess)
            Numberguess = QInputDialog::getInteger(this, "", "Try a higher number, and enter your new guess", 0, 1, 100);
        else if (Numberhidden < Numberguess)
            Numberguess = QInputDialog::getInteger(this, "", "Try a lower number, and enter your new guess", 0, 1, 100);

        ++mycounter;
    }

    if (Numberhidden == Numberguess)
        label1->setText("You guessed it correct");
    else
        label1->setText("You have run out of opportunites");
}
Last edited on
Or in one file!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <QMainWindow>
#include <QApplication>
#include <QLabel>
#include <QPushButton>
#include <QVBoxLayout>
#include <QWidget>
#include <QInputDialog>

class Form1 : public QMainWindow
{
    Q_OBJECT

public:
    explicit Form1(QWidget *parent = 0) : QMainWindow(parent) // Stuff to draw the window
    {
        centralWidget  = new QWidget(this);
        verticalLayout = new QVBoxLayout(centralWidget);
        Button1        = new QPushButton(centralWidget);
        label1         = new QLabel(centralWidget);

        Button1->setObjectName(QString::fromUtf8("Button1"));
        Button1->setText(QApplication::translate("Form1", "Push me", 0, QApplication::UnicodeUTF8));

        verticalLayout->addWidget(Button1);
        verticalLayout->addWidget(label1);

        this->setCentralWidget(centralWidget);

        QMetaObject::connectSlotsByName(this);
    }

private slots:
    void on_Button1_clicked()
    {
        Numberhidden = rand() % 100 + 1;
        Numberguess  = QInputDialog::getInteger(this, "", "Please enter your first guess", 50, 1, 100);

        while (Numberhidden != Numberguess && mycounter < 5)
        {
            if (Numberhidden > Numberguess)
                Numberguess = QInputDialog::getInteger(this, "", "Try a higher number, and enter your new guess", 0, 1, 100);
            else if (Numberhidden < Numberguess)
                Numberguess = QInputDialog::getInteger(this, "", "Try a lower number, and enter your new guess", 0, 1, 100);

            ++mycounter;
        }

        if (Numberhidden == Numberguess)
            label1->setText("You guessed it correct");
        else
            label1->setText("You have run out of opportunites");
    }

private:
    QWidget*     centralWidget;
    QVBoxLayout* verticalLayout;
    QPushButton* Button1;
    QLabel*      label1;

    int Numberhidden;
    int Numberguess;
    int mycounter = 0;
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Form1 w;
    w.show();
    
    return a.exec();
}
Last edited on
@Stewbond

After installing qtcreator, I compiled the program with the following command:
g++ -c -m64 -pipe -O2 -Wall -W -D_REENTRANT -fPIE -std=c++11 -DQT_NO_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I/usr/share/qt5/mkspecs/linux-g++-64 -I. -I. -I/usr/include/qt5 -I/usr/include/qt5/QtWidgets -I/usr/include/qt5/QtGui -I/usr/include/qt5/QtCore -I. -o guessGUI.o guessGUI.cpp

And this is the error messages I get:
1
2
3
4
5
6
guessGUI.cpp: In constructor ‘Form1::Form1(QWidget*)’:
guessGUI.cpp:22:73: error: ‘UnicodeUTF8’ is not a member of ‘QApplication’
guessGUI.cpp: In member function ‘void Form1::on_Button1_clicked()’:
guessGUI.cpp:36:24: error: ‘getInteger’ is not a member of ‘QInputDialog’
guessGUI.cpp:41:31: error: ‘getInteger’ is not a member of ‘QInputDialog’
guessGUI.cpp:43:31: error: ‘getInteger’ is not a member of ‘QInputDialog’


How do I fix these?
Try qt4
$ g++ $(pkg-config --cflags Qt{Core,Gui}) -c -std=c++11 foo.cpp
#expanded version
$ g++ -DQT_SHARED -I/usr/include/qt4 -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I/usr/include/qt4/QtCore -c -std=c++11 foo.cpp
Last edited on
Topic archived. No new replies allowed.