Whats wrong with my code?

I wrote this and i wanted to make it output the scores to a text file but i dont know whats wrong?

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
#include <iostream>
#include <string>
#include <ctime>
#include <random>
#include <windows.h>
#include <fstream>

using namespace std;

void save();

int score = 0;
int right = 0;
int wrong = 0;

int main()
{
    int X;
    int type;
    int score = 0;
    int right = 0;
    int wrong = 0;

    time_t T;
    time(&T);
    srand(T);

    while(true)
    {

    for(int R = 0; R < 100; ++R)
    {
        X = rand() % 10000;
    }
        cout << "               " << "Score: " << score << endl;
        cout << "\n";
        cout << X << endl;
        cin >> type;

        if(type == X)
        {
            cout << "\n";
            cout << "Good!" << endl;
            score += 1;
            right += 1;
            cout << "\n";
        }
        else if(type != X)
        {
            cout << "\n";
            cout << "Wrong!" << endl;
            score -= 1;
            wrong += 1;
            cout << "\n";
        }
        else if(WM_QUIT)
        {
            save();
        }
    }
}

void save()
{
    ofstream rns("DATA.txt");

    rns << score;
    rns << right;
    rns << wrong;

    rns.close();
}



ERRORS:

||=== Random Number writing, Debug ===|
C:\Users\Chay Hawk\Desktop\Random Number writing\main.cpp||In function 'void save()':|
C:\Users\Chay Hawk\Desktop\Random Number writing\main.cpp|68|error: reference to 'right' is ambiguous|
C:\Users\Chay Hawk\Desktop\Random Number writing\main.cpp|13|error: candidates are: int right|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\bits\ios_base.h|928|error: std::ios_base& std::right(std::ios_base&)|
C:\Users\Chay Hawk\Desktop\Random Number writing\main.cpp|68|error: reference to 'right' is ambiguous|
C:\Users\Chay Hawk\Desktop\Random Number writing\main.cpp|13|error: candidates are: int right|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\bits\ios_base.h|928|error: std::ios_base& std::right(std::ios_base&)|
||=== Build finished: 6 errors, 0 warnings ===|
I see a few things wrong:

First your compilation error:
right is declared as an int AND is declared in std::. This is the problem with using namespace std;. To avoid the conflict, rename right or replace line 8 with:
1
2
3
4
using std::cin;
using std::cout;
using std::ofstream;
using std::endl;


Next, you've declared score, right, and wrong globally and locally in main. In main() you are writing to the local variables and in save() you are reading the global variables. You have two options: delete the local definitions (easier), or delete the global definitions and pass the data into save as arguments (better).

Finally:
1
2
3
4
for(int R = 0; R < 100; ++R)
{
    X = rand() % 10000;
}

I'm guessing that you are trying to "increase randomness", however it doesn't actually increase the randomness by any degree. Just use X = rand()%10000;. It's faster and gives you a result that is just as random.
closed account (4z0M4iN6)
The mistake is: to declare score, right, and wrong local in main.

Save will always access the global defined variables, which values are 0.
Whether rns << right, is an error depends on compiler settings. Normally it would be a warning, or simply valid. The compiler warns you, that save will not take the variables, defined in main.

The error in line 13 means the same warning. You are lucky, that the compiler warned you. Otherwise you would wonder, why your program won't work.

No, you are more lucky, than you think. The compiler wouldn't have warned you. The mistake you made: "right" is some identifier (value or function) of std.
So it's an error.
Last edited on
Topic archived. No new replies allowed.