0

To begin with I have decided to post my code here so everyone can understand the issues I have with the code and will be able to assist me better.

main.cpp

#include <sstream>  
#include <iostream>  
#include <cstdlib>  
#include <string>  
#include "validate.h"  
#include "main.h"  

using namespace std;

int main() {
    name = getName();
    score = quiz();
    cout << "\n\n";
    system("pause");
    return (0);
}

string getName() {
    cout << "Enter your name: ";
    getline(cin, name);
    val.set_item(name);
    valid = val.vName();
    if (valid) return name;
    else {
        cout << "Invalid name!\n\n";
        getName();
    }
}

int quiz() {
    for (int loop = 0; loop <= 10; loop++) {
        rand1 = rand() % 20 + 1;
        rand2 = rand() % 20 + 1;
        randop = rand() % 3;
        op = operators[randop];

        if (op == '*') ans = rand1 * rand2;
        else if (op = '+') ans = rand1 + rand2;
        else ans = rand1 - rand2;

        cout << "What is " << rand1 << op << rand2 << " ? ";
        getline(cin, input);
        val.set_item(input);
        valid = val.vInput();
        if (valid) {
            istringstream(input) >> inputint;
            if (ans == inputint) {
                cout << "Correct!\n\n";
                score++;
            }
            else cout << "Incorrect!\n\n";
        }
        else cout << "Incorrect!\n\n";
    }
    return score;
}

validate.h

#ifndef validate_h
#define validate_h

using namespace std;

class validate {
    string item;
    int len;
    bool check;
public:
    void set_item(string x);

    bool vInput() {
        len = item.size();
        for (int loop = 0; loop < len; loop++) {
            if (item[loop] == '-' && loop == 0) continue;
            check = isalnum(item[loop]);
            if (check) continue;
            else return false;
        }
        return true;
    }
    bool vName() { 
        len = item.size();
        for (int loop = 0; loop < len; loop++) {
            check = isalpha(item[loop]);
            if (check) continue;
            else return false; 
        }
        return true;
    }
};

void validate::set_item(string x) {
    item = x;
}

#endif

main.h

#ifndef main_h
#define main_h

string name;
string input;
string getName();

validate val;

bool valid;

int quiz();
int score;
int rand1;
int rand2;
int randop;
int ans;
int inputint;

const char operators[3] = { '-', '+', '*' };
char op;

#endif

Ok so my code compiles fine and goes through everything perfectly. It asks the name, it knows when it is invalid but here comes my first issue. When you enter an incorrect name it again prompts you to enter it. But it crashes when you enter it the second time. Here is a screenshot example.

crash issue

The program also does not generate random numbers. Its the same every single run. Here is a screenshot of that.

duplicating numbers

Any assistance with these issues would be greatly appreciated.

Also for the random issue. I looked up on the other questions and when I tried the fix "srand(time(NULL));" it tells me that srand is ambiguous.

  • Please limit yourself to one question per question. For you first question please provide a [mcve] and you should at least find out where the code is crashing by stepping through the code with your debugger. Your second question is a duplicate of: http://stackoverflow.com/questions/1108780/why-do-i-always-get-the-same-sequence-of-random-numbers-with-rand – NathanOliver Apr 07 '16 at 17:40
  • I tried some of these. The random thing "srand (time(NULL));" tells me that srand is ambigous so I can't do a thing. – Reece Ward Apr 07 '16 at 17:45
  • It may also be that the issues are linked in some way so I decided to express them all. – Reece Ward Apr 07 '16 at 17:45
  • You have an error here `else if (op = '+') `, it should be `==` – Barmak Shemirani Apr 07 '16 at 17:52
  • 1
    Why did you write the `getName` function in such a strange way (recursion)? Just write a simple `while` loop. – PaulMcKenzie Apr 07 '16 at 18:00
  • Didn't mention it in my answer below, but I completely agree with PaulMcKenzie. You should be running this in a while loop, not using getName in a recursive manner. – Geoherna Apr 07 '16 at 18:02
  • @ReeceWard Also, might as well get into good coding habits. If I wrote some sort of keyboard macro that enters an invalid name a few thousand times, your program blows out the stack due to `getName` being recursive. – PaulMcKenzie Apr 07 '16 at 18:10

1 Answers1

0

In regards to your application crashing when you call getName(), try flushing the cin buffer using:

cin.ignore();

or

cin.clear();

to clear the buffer state. I had a similar issue a while back which I believe that was the solution to. In regards to your random number, you havent initialized a random seed. Initialize a random seed prior to generating random numbers using:

srand (time(NULL));

Hope this helps.

EDIT:

Just saw your comment above. Try using the following instead for your random number seed:

srand(time(0));

Make sure you do #include <ctime> at the head of file for this solution.

Geoherna
  • 3,523
  • 1
  • 24
  • 39