Need Help on selecting functions ( would be appreicated )


i am a beginner in programming , i just want to make a program to train little kids in mathematics operations , the idea behind the code is that i want once you compile it you choose what operation the kid wants to practice based on pressing 1 , 2 , 3 & 4 ( Multiplication , addition , division , and subtraction ), and then if the answer of the result is right a " a very good" pops up , but if its wrong " please try again " until he answer it right *loop* . i tried doing it for countless hours doing void function , but i keep failing over and over .
any help in this would be really appreciated .

thanks in advance .

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
 #include <iostream>
#include <ctime>

double Multiplication(double a, double b);
void addition(double a, double b,double &m);
void division(double a, double b,double &d);
void subtraction(double a, double b,double &s);
double answer = 0 ;
using namespace std;

int main()
{

double a = (rand() % 10) + 1;
double b = (rand() % 10) + 1;

double i;
cout<<"\n\n This is a Program to teach Elementary Students how to do Mathmatical Operations \n\n";
cout<<"\n\nPress 1 for Multiplication , 2 for addition , 3 for division ,and 4 for subtraction\n\n";
 cin>>i;

if(i = 1)
cout<<Multiplication(a,b)<<endl;
else if (i = 2)
	cout<<addition(a,b,m)<<endl;
else if (i = 3)
	cout<<division(a,b,d)<<endl;
else if (i = 4)
	cout<<subtraction(a,b,s)<<endl;
return 0;

}


double Multiplication(double a, double b)
{
cout<<"\n\n Multiplication \n\n";
g=a*b;
cout<< "What is "<< a << " times " << b << " ? "<<endl;
cin>> answer;       
if(answer ==(a*b))
{
cout<< "Very Good!!\n"<<endl;
a= rand()%10;
b= rand()%10;
}
else{
cout<<"No, Please try again.\n"<<endl;
  }  
}
    return g;
}

void addition(double a, double b,double &m)
{

cout<<"\n\n addition \n\n";
for (int x=0 ; x!=1 ; x){
{  
cout<< "What is "<< a << " plus " << b << " ? "<<endl;
cin>> answer;       
if(answer ==(a+b))
{
cout<< "Very Good!!\n"<<endl;
a= rand()%10;
b= rand()%10;
}
else
cout<<"No, Please try again.\n"<<endl;
}
}

void division(double a, double b,double &d)
{

cout<<"\n\n division \n\n";
for (int x=0 ; x!=1 ; x){
{  
cout<< "What is "<< a << " divided by " << b << " ? "<<endl;
cin>> answer;       
if(answer ==(a/b))
{
cout<< "Very Good!!\n"<<endl;
a= rand()%10;
b= rand()%10;
}
else
cout<<"No, Please try again.\n"<<endl;
  
}
}
void subtraction(double a, double b,double &s)
{
cout<<"\n\n subtraction \n\n";
for (int x=0 ; x!=1 ; x){
{  
cout<< "What is "<< a << " minus " << b << " ? "<<endl;
cin>> answer;       
if(answer ==(a-b))
{
cout<< "Very Good!!\n"<<endl;
a= rand()%10;
b= rand()%10;
}
else
cout<<"No, Please try again.\n"<<endl;

}
}
in your code lines 22-30 there are a few of these

if(i = 1)

elsewhere you correctly use == so i presume these are typo's.

in main() you try to pass m,d,s respectively to your subroutines, but these variables do not exist in main() or its parent scope (global), furthermore they aren't used by the routines that you are passing them to, so they are all redundant anyway.

Also, sort out your indentation, well laid out code can make many errors obvious and should be adopted as normal programming practice from day one.

thanks a lot for your contribution jaybob66 , yet i am still confused with the error that comes with the code i just have adjusted .


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
#include <iostream>
#include <ctime>

void Multiplication(double a, double b,double &l);
void addition(double a, double b,double &m);
void division(double a, double b,double &d);
void subtraction(double a, double b,double &s);
double answer = 0 ;
using namespace std;

int main()
{

double a = (rand() % 10) + 1;
double b = (rand() % 10) + 1;
double i;
double l,m,d,s ;
cout<<"\n\n This is a Program to teach Elementary Students how to do Mathmatical Operations \n\n";
cout<<"\n\nPress 1 for Multiplication , 2 for addition , 3 for division ,and 4 for subtraction\n\n";
 cin>>i;

if(i == 1)
cout<<Multiplication(a,b,l)<<endl;
else if (i = 2)
	cout<<addition(a,b,m)<<endl;
else if (i = 3)
	cout<<division(a,b,d)<<endl;
else if (i = 4)
	cout<<subtraction(a,b,s)<<endl;
return 0;

}


void Multiplication(double a, double b,double &l)
{
cout<<"\n\n Multiplication \n\n";

cout<< "What is "<< a << " times " << b << " ? "<<endl;
cin>> answer;       
if(answer ==(a*b))
{
cout<< "Very Good!!\n"<<endl;
a= rand()%10;
b= rand()%10;
}
else{
cout<<"No, Please try again.\n"<<endl;
  }  
}
    

void addition(double a, double b,double &m)
{
cout<<"\n\n addition \n\n";
for (int x=0 ; x!=1 ; x){ 
cout<< "What is "<< a << " plus " << b << " ? "<<endl;
cin>> answer;       
if(answer ==(a+b))
{
cout<< "Very Good!!\n"<<endl;
a= rand()%10;
b= rand()%10;
}
else
cout<<"No, Please try again.\n"<<endl;
}
}

void division(double a, double b,double &d)
{

cout<<"\n\n division \n\n";
for (int x=0 ; x!=1 ; x){
 
cout<< "What is "<< a << " divided by " << b << " ? "<<endl;
cin>> answer;       
if(answer ==(a/b))
{
cout<< "Very Good!!\n"<<endl;
a= rand()%10;
b= rand()%10;
}
else
cout<<"No, Please try again.\n"<<endl; 
}
}

void subtraction(double a, double b,double &s)
{
cout<<"\n\n subtraction \n\n";
for (int x=0 ; x!=1 ; x){
{  
cout<< "What is "<< a << " minus " << b << " ? "<<endl;
cin>> answer;       
if(answer ==(a-b))
{
cout<< "Very Good!!\n"<<endl;
a= rand()%10;
b= rand()%10;
}
else
cout<<"No, Please try again.\n"<<endl;

}
}
i have wrote again your program . check it and if any trouble feel free to ask

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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142

#include <iostream>
//#include<ctime>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
bool ans=false;
void add()
{
    float x;
    float a= rand()%100;
    float b=rand()%100;
    cout<< "value of a--------------------"<<a<<endl<<"value of b--------------------"<<b<<endl;
    cout<<"enter your answer \n";
    cin>>x;

    if(x==(a+b))
    {
        cout<<"\nyour ans true\n";
        ans =true;
    }
    else
    {
        cout<<"\n wrong answer. TRY AGAIN\n";
    }

}

void sub()
{
    float x;
    float a= rand()%100;
    float b=rand()%100;
    cout<< "value of a--------------------"<<a<<endl<<"value of b--------------------"<<b<<endl;
    cout<<"enter your answer \n";
    cin>>x;

    if(x==(a-b))
    {
        cout<<"\nyour ans true\n";
        ans =true;
    }
    else
    {
        cout<<"\n wrong answer. TRY AGAIN\n";
    }

}


void mul()
{
    float x;
    float a= rand()%100;
    float b=rand()%100;
    cout<< "value of a--------------------"<<a<<endl<<"value of b--------------------"<<b<<endl;
    cout<<"enter your answer \n";
    cin>>x;

    if(x==(a*b))
    {
        cout<<"\nyour ans true\n";
        ans =true;
    }
    else
    {
        cout<<"\n wrong answer. TRY AGAIN\n";
    }

}

void divide()
{
    float x;
    float a= rand()%100;
    float b=rand()%100+1;
    cout<< "value of a--------------------"<<a<<endl<<"value of b--------------------"<<b<<endl;
    cout<<"enter your answer \n";
    cin>>x;

    if(x==(a/b))
    {
        cout<<"\nyour ans true\n";
        ans =true;
    }
    else
    {
        cout<<"\n wrong answer. TRY AGAIN\n";
    }

}



int main()
{

while(1)
{
    cout<<"elementry maths\n";
    cout<<"1 add \n";
    cout<<"2 sub \n";
    cout<<"3 mul \n";
    cout<<"4 divide\n";
    cout<<"5 exit \n";
     cout<<"enter your choice\n ";
int choice;
cin>>choice;

ans=false;


while(ans==false)
{

switch(choice)
{

    case 1:add();
    break;
    case 2:sub();
    break;
    case 3:mul();
    break;
    case 4:divide();
    break;
    case 5:
        cout<< "EXIT";
        exit(0);
}

}

}
    return 0;
}





your == problem also occurs at line 24,26,28

if you have an error message from the compiler always post it with your code so we can see what it is complaining about.
Topic archived. No new replies allowed.