Random Math question

Hi, I am a begginer with c++ and i need help with this code,
i am trying to generate a random math question but there is always an error that i dont know how to fix.

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
#include <iostream>
#include <unistd.h>
#include <fstream>
#include <time.h>
#include <conio.h>
#include <windows.h>
#include<stdio.h>
#include<dos.h>
#include <thread>

using namespace std;

char *charStr;
int stringLength;

void genRandom() {
    static const char alphanum[] =
        " plus "
        " subtract "
        " times "
        " divided by ";

    for (int i = 0; i < stringLength; ++i) {
        charStr[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
    }

    charStr[stringLength] = 0;
}
    void StartTimer (int time)
    {
                while(time>0)
    {
        Sleep(1000);
        time--;
    }
    }
    void Question()
    {
        int one, two;
        srand( time(0));
        one=rand()%500+1;
        two=rand()%500+1;
        genRandom();
        cout<<"What is "<<one<<charStr<<two<<endl;
    }
    int main()
{
    system("COLOR B");
    int answer;
    int one, two;
    Question();
    cin>>answer;
    if(answer==one+two) {cout<<"Genius!"<<endl;}
}
hiya,

 
 if(answer==one+two) {cout<<"Genius!"<<endl;}


this will blow up, as you have not intialised 'one' and 'two'. It looks like you are under the assumption that your 'one' and 'two' variables in your Question() method are the same as those in your main(). This is not the case.

Also I don't think you need to include most of those headers.

edit: you don't seem to be calling StartTimer () either, so i'm not sure why that's there.

also on line 14 you declare 'stringLength' but never initialise this either.
Last edited on
Topic archived. No new replies allowed.