need help on an assignment please!

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
/*
November 14,2013
COP 2000 assignment #4
*/

#include <cstdlib>
#include <iostream>
#include <cmath>

using namespace std;
void intRand (int &);
void showMenu();




int main ()
{

   float guess, ANS;
   int choice;
     const int ADD_CHOICE = 1,
             SUB_CHOICE = 2,
             MULTI_CHOICE = 3,
             QUIT_CHOICE = 4;
   

   do
   showMenu();
   
   while (choice < ADD_CHOICE || choice > QUIT_CHOICE);
      {
         cout << "Please enter a valid menu choice: ";
         cin >> choice;
      }
      if (choice != QUIT_CHOICE)
      {
      cout << "What is your answer? ";
      }
      cin >> guess;
switch (choice)
{
case 1:
cout << "r1 + r2" << "= ? \n";
cin>> (guess);
if (guess = ANS) cout<< "Great job!";
else { cout << ANS;
break;
case 2:
cout << ("r1 - r2") << "= ? \n";
cin>> ANS;
if (ANS = guess); cout<< "Great job!";
break;
case 3:
cout << ("r1 * r2") << "= ? \n";
cin>> ANS;
if (ANS = r1 * r2) cout<< "Great job!";
case 4:
return 0;
}

   void showMenu()
   
   {
   	
   cout << "\n\t\tMENU OF OPERATIONS \n\n "
   << " 1. Addition.\n";
   << " 2. Subtraction.\n";
   << "\n 3. Multiplication.\n";
   << "\n 4. Quit the Program.\n\n"
   << " Enter the number of the operation to try (1-4):";
   
   
   }
   void intRand()
   {
     
r1 = rand() % 11 + 2;
r2 = rand() % 11 + 2;
   
   }
}
return 0;
}




can someone explain why on line 57(bold) its saying


In function 'int main()':
[Error] cannot convert 'const char [8]' to 'float' in assignment


what am I doing wrong?


thanks!
Last edited on
In all if() " = " must be changed to " == " and see after that.
if I change that it now says "Invalid operands of types 'float' and 'const char[8]' to binary 'operator =='
You have to declare r1 and r2

-" == " must be present in those ifs !!!
- In switch are also some errors: case 3: without break;
- case 4: ?!
- an else with erroneous {.

So, please study if and switch.
Last edited on
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
November 14,2013
COP 2000 assignment #4
*/

#include <cstdlib>
#include <iostream>
#include <cmath>

using namespace std;
void intRand (int &);
void showMenu();




int main ()
{

   float guess, ANS;
   int choice;
     const int ADD_CHOICE = 1,
             SUB_CHOICE = 2,
             MULTI_CHOICE = 3,
             QUIT_CHOICE = 4;
   

   do
   showMenu();
   
   while (choice < ADD_CHOICE || choice > QUIT_CHOICE);
      {
         cout << "Please enter a valid menu choice: ";
         cin >> choice;
      }
      if (choice != QUIT_CHOICE)
      {
      cout << "What is your answer? ";
      }
      cin >> guess;
switch (choice)
{
case 1:
cout << "r1 + r2" << "= ? \n";
cin>> (guess);
if (guess == ANS) cout << "Great job!";
else cout << ANS;
break;

case 2:
cout << ("r1 - r2") << "= ? \n";
cin>> ANS;
if (ANS == guess); cout << "Great job!";
break;

case 3:
cout << ("r1 * r2") << "= ? \n";
cin>> ANS;
if (ANS == "r1 * r2"); cout << "Great job!";
break;

case 4:
exit(0);	

return 0;
}

{
   void showMenu()
   
   {
   	
   cout << "\n\t\tMENU OF OPERATIONS \n\n "
   << " 1. Addition.\n";
   << " 2. Subtraction.\n";
   << "\n 3. Multiplication.\n";
   << "\n 4. Quit the Program.\n\n"
   << " Enter the number of the operation to try (1-4):";
   
   
   }
   void intRand()
   
     
{
r1 = rand() % 11 + 2;
r2 = rand() % 11 + 2;
}   
   


}


return 0;
}


I fixed the == error and my other mistakes you listed. I am still getting these errors.




59 12 [Error] invalid operands of types 'float' and 'const char [8]' to binary
'operator=='
71 4 [Error] a function-definition is not allowed here before '{' token
Try to understand all the changes and modify them as you wish.

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
/*
November 14,2013
COP 2000 assignment #4
*/

#include <cstdlib>
#include <iostream>
#include <ctime>

using namespace std;
void intRand (int *);
void showMenu();




int main ()
{

	float guess, ANS;
	int r[2];
	int choice = 0;
    int ADD_CHOICE = 1,
        QUIT_CHOICE = 4;
             
	srand((unsigned int)time((time_t *)NULL));//need for rand()

	showMenu();
	
	while (choice < ADD_CHOICE || choice > QUIT_CHOICE)
    {
        cout << "Please enter a valid menu choice: ";
        cin >> choice;
    } 
    
    if (choice == QUIT_CHOICE)
		return 0;
	cout << "What is your answer for ";
    
    intRand(r);
    
//	cout << r[0] << "   " << r[1] << '\n'; // test if rand() works

	switch (choice)
	{
		case 1:
			ANS = r[0] + r[1];
			cout << "r1 + r2" << "= ? ";
			cin>> guess;
			if (guess == ANS)
				cout << "Great job!";
			else
				cout << ANS << '\n';
			break;

		case 2:
			ANS = r[0] - r[1];
			cout << ("r1 - r2") << "= ? ";
			cin>> guess;
			if (ANS == guess)
				cout << "Great job!";
			else
				cout << ANS << '\n';
			break;

		case 3:
			ANS = r[0] * r[1];
			cout << ("r1 * r2") << "= ? ";
			cin>> guess;
			if (ANS == guess)
				cout << "Great job!";
			else
				cout << ANS << '\n';
			break;
	}
	
	return 0;
}


void showMenu()
{
	cout << "\n\t\tMENU OF OPERATIONS \n\n"
	<< " 1. Addition.\n"
	<< "\n 2. Subtraction.\n"
	<< "\n 3. Multiplication.\n"
	<< "\n 4. Quit the Program.\n\n";
}
   
void intRand(int *r)   
{
	r[0] = rand() % 11 + 2;
	r[1] = rand() % 11 + 2;
}
		MENU OF OPERATIONS 

 1. Addition.

 2. Subtraction.

 3. Multiplication.

 4. Quit the Program.

Please enter a valid menu choice: 1
What is your answer for r1 + r2= ? 12
16
Topic archived. No new replies allowed.