Program that searches for bugs in code?

Apr 10, 2013 at 3:31am
I know a compiler is made specifically for catching bugs but isnt there a program that searches through code and cathces bugs and other things in the code that compilers cant catch?

Also while im at it I have a few more questions.

Here is my code:

I was wondering if i should create my variables as static? When do you even use static variables?

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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <iostream>
#include <string>
#include "Player.h"
#include <fstream>
#include <time.h>
#include <cstdlib>

/*

int x = 800;
int *pointer = &x;

cout << *pointer << endl;

*/


using namespace std;

void Load(string &PN, int &M, int &XP, int &L)
{
    ifstream File_In;

    File_In.open("WMSF.txt");

    getline(File_In, PN);
    File_In >> M;
    File_In >> XP;
    File_In >> L;
}

void Save(string &PN, int &M, int &XP, int &L)
{
    ofstream File_Out;

    File_Out.open("WMSF.txt");

    File_Out << PN << endl;
    File_Out << M << endl;
    File_Out << XP << endl;
    File_Out << L << endl;
}

void stats(string &PN, int &M, int &XP, int &L)
{
    cout << "Your Stats\n" << endl;

    cout << "Name: " << PN << endl;
    cout << "Money $" << M << endl;
    cout << "Experience: " << XP << endl;
    cout << "Level: " << L << endl;
}

void Game(string &PN, int &M, int &XP, int &L)
{
    int choice = 0;
    int choice2 = 0;

    int r;
    srand(time(0));

    r = rand() % 15;

    cout << "MENU\n" << endl;

    cout << "1) Ship products" << endl;
    cout << "2) Stats" << endl;
    cin >> choice;

    if(choice == 1)
    {
        cout << "where are you shipping to" << endl;
        cout << "1) Chicago" << endl;
        cout << "2) Los Angeles" << endl;
        cout << "3) Tokyo" << endl;
        cin >> choice2;

        switch(choice2)
        {
            case 1:
                cout << "Delivering to chicago" << endl;

        }
    }
    if(choice == 2)
    {
        stats(PN, M, XP, L);
    }
}

int main()
{
    int choice = 0;
    int money = 0;
    int xp = 0;
    int level = 1;
    int distance = 0;
    string Player_Name;

    cout << "Warehouse Manager\n" << endl;

    cout << "1) New Game" << endl;
    cout << "2) Load Game" << endl;
    cin >> choice;

    cin.ignore(200, '\n');

    if(choice == 1)
    {
        cout << "Welcome, what is your name?" << endl;
        getline(cin, Player_Name);

        cout << "\nOk " << Player_Name << " Lets get some info before we start\n" << endl;
        cout << money << endl;

        money += 300;

        Save(Player_Name, money, xp, level);
        Game(Player_Name, money, xp, level);
    }
    if(choice == 2)
    {
        Load(Player_Name, money, xp, level);
        Game(Player_Name, money, xp, level);
    }
}
Apr 10, 2013 at 3:45am
closed account (3CXz8vqX)
L B is so going to be setting fire to this thread shortly.

A Debugger is a tool used to find bugs at run time. (Yes I'm sticking to my definition since the Op apparently agrees). Take a look at an IDE such as Code::Blocks for a GUI version of a Debugger. ^_^

Edit

static members are generally used inside classes (cplusplus tut), to stop things changing once something has been deleted or changed...(I think).
Last edited on Apr 10, 2013 at 3:50am
Apr 10, 2013 at 3:59am
closed account (D80DSL3A)
Static variables, in the context of local variables in functions, retain their value between function calls.
Use?
1
2
3
4
5
6
void f()
{
    static int callCount = 0;// initialized on 1st function call
    ++callCount;
    cout << "I have been called " << callCount << " times so far.";
}


Or to compare a value being passed to the previous value:
1
2
3
4
5
6
7
8
9
10
11
void f(int x)
{
    static int lastX = x;
  
    if( x > lastX) 
        cout << "x is greater than last time ";
    else
        cout << "x is less or equal to last value ";

    lastX = x;// for next time
}

The uses are endless.
Last edited on Apr 10, 2013 at 4:00am
Apr 10, 2013 at 5:25am
I know a compiler is made specifically for catching bugs


Compilers are made for compiling code. Debuggers are made specifically for debugging code.

but isnt there a program that searches through code and catches bugs and other things in the code that compilers cant catch?


google "C++ static code analysis"
Apr 10, 2013 at 6:20am
"C++ static code analysis"

Thank you thats exactly what i was looking for
Apr 10, 2013 at 7:49am
Debugger and if you use Linux (and Mac, can't remember if it is on that OS or not) Valgrind for memory leaks. As for compiling clang is supposed to be a great compiler. Now back on debuggers, get the latest GDB if you can as I believe since 7.4 they have added reverse debugger (never used it) which from my understanding will go to the crash and then go from the crash back through every line of code in reverse rather than having you sit through 100+ lines of code just to get where you think the bug is.

Just wanted to add that for consideration.
Topic archived. No new replies allowed.