Use a variable as + - * or /
Hi, I am a beginner, in my function AskQuestion, i want to be able to type +, -, * or / and put that in an equation
PLEASE HELP!
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
|
#include <iostream>
#include <unistd.h>
#include <fstream>
#include <time.h>
#include <conio.h>
#include <windows.h>
#include<stdio.h>
#include<dos.h>
using namespace std;
void StartTimer (int time)
{
while(time>0)
{
Sleep(1000);
time--;
}
}
void AskQuestion (float x)
{
int one, two, answer;
srand( time(0));
one=rand()%500+1;
two=rand()%500+1;
cout<<"What is "<<one<<" "<<x<<" "<<two<<endl;
cin>>answer;
if(answer==one x two) {cout<<x<<endl;}
}
int main(){
system("COLOR B");
int one, two, answer;
AskQuestion()
}
|
'X' isn't a defined variable in AskQuestion. You can, however, use the '*' operator. Here is an example:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
void AskQuestion(/* not using anything here */) {
int one, two, answer;
srand(time(0));
one = rand()%500 + 1;
two = rand()%500 + 1;
std::cout << "What is " << one << " * " << two << std::endl;
std::cin >> answer;
if (answer == one * two)
std::cout << "Correct!" << std::endl;
else
std::cout << "Wrong..." << std::endl;
}
|
But when i want to enter the function i want to do this :
void AskQuestion(/); or
void AskQuestion(+);
You could use a
char to store the character, and switch based on it. Here is an example:
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
|
void AskQuestion(char op) {
int one, two, answer;
bool isCorrect = false;
one = rand() % 500 + 1;
two = rand() % 500 + 1;
std::cout << "What is " << one << " " << op << " " << two << "? ";
std::cin >> answer;
switch (op) {
case '+':
if (answer == one + two) isCorrect = true;
break;
case '-':
if (answer == one - two) isCorrect = true;
break;
case '*':
if (answer == one * two) isCorrect = true;
break;
case '/':
if (answer == one / two) isCorrect = true;
break;
}
if (isCorrect)
std::cout << "Correct!" << std::endl;
else
std::cout << "Wrong..." << std::endl;
}
int main() {
srand(time(0));
AskQuestion('+');
AskQuestion('*');
AskQuestion('-');
return 0;
}
|
Thank you so much!
Topic archived. No new replies allowed.