In progress of developing a sort of plaza. error in the main() function

closed account (367kGNh0)
Why won't My program start? It keeps saying this: "no match for operator'==' in answer 'a' "

(and for simplicity I removed the other functions.)
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
#include<iostream>
#include <string>
using namespace std;


string answer;


void CalcFunc()
{
	
	
	
}

void ChatFunc()
{
	
}

void AreaCalcFunc()
{
	string Jole;
	double x;
	double h;
	double u;
	
	cout<<"Welcome to the area finder, what shape would you like to find the area of?"<<endl;
	
	cin>> Jole;
	if (Jole == "square")
	{cout<<"Please enter the value of one side."<< endl;
	
	cin>> x;
	
	cout<< "The area is "<< x * x;}
	
	if (Jole == "triangle")
	{cout<<"Please enter the value of the height."<<endl;
	
	cin>> h;
	
	cout<<"and Now the base length"<<endl;
	
	cin>> u;
	
	
	cout<< "The area is "<< 0.5*(h * u);}
	}

void QuizFunc()
{
	
	
}


int main() {
	
	cout<<"What would you like to do?";
	
	cin>> answer;
	
	if(answer == 'a')
	{AreaCalcFunc();}
	
	else{cout<<"sorry I cannot do that.";}
	
	return 0;
}

 


UPDATE, error solved, I needed to turn the variable I declared in the beginning (string answer;) into char answer;

Hope this helps other beginners like me :)
Last edited on
Hello Rascake,

You may have solved one problem, but there are others.

The relevant code reworked with comments:
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
#include<iostream>
#include <string>
#include <cctype>  // <--- Added. For std::tolower().

using namespace std;  // <--- Best not to use.


string answer;  // <--- Best not to use here. Better placed in "main", but in the end wrong type.

void AreaCalcFunc()
{
	string Jole;
	double x;
	double h;
	double u;

	cout << "\n Welcome to the area finder, what shape would you like to find the area of?" << endl;
	// <--- Could use a menu here.

int main()
{
	char answer{};

	do
	{
		cout << "\n What would you like to do?\n";
		std::cout << "\n a. First menu choice.";
		std::cout << "\n b. Second menu choice.";
		std::cout << "\n c. Third menu choice.";
		std::cout << "\n Enter choice: ";
		cin >> answer;
		
		answer = std::tolower(answer);

		if (answer != 'a' && answer != 'b' && answer != 'c')
			std::cout << "\n Invalid input. Try again.\n" << std::endl;

	} while (answer != 'a' && answer != 'b' && answer != 'c');

	if (answer == 'a')
		AreaCalcFunc();

	else
		cout << "sorry I cannot do that.";

	return 0;
}

It is best not to use global variables as they are available to the whole file and can be changed anywhere which can be hard to track down and fix. Variables like this are better put in the functions that need them. Either passed to the function or defined in the function.

In the function you are expecting the user to enter something without a prompt and hoping the user spells the word correctly and use the correct word. A menu of choices would be nice then you could either use the choice or use a switch to turn the choice into a word. I would just use a menu choice of one character or int.

The concept of the menu in "main" I usually put in a function and only return a valid choice. You can also use this concept in the function.

If you are finished with the question, be sure to put a green check on the post to let everyone know that you are finished.

Hope that helps,

Andy
closed account (367kGNh0)
Thank you for the advice Andy, but will using namespace std really harm my program so much?
Hello Rascake,

No it will not hurt the program, but will cause problems when you name a variable or function for your program that is in the standard name space the compiler will not know which one to use and most likely generate an error.

Instead of teaching what is in the standard name space most institutions of higher learning tend to take the easy way in the first programming class leaving what should be covered for later classes or for when you leave school.

There is good advice available here and no one is being paid to tell you the wrong way to do your code just what works best.

I found that after about a week to a week and a half of typing "std::" that I just do it without thinking now. Also starting with "std::cin", std::cout", "std::endl", "std::string" as kind of the basics learning what else is in the standard name space that should be qualified became easier. Much easier this way than having to learn this all at once.

Hope that helps,

Andy
closed account (367kGNh0)
I see, I will give it consideration.
Topic archived. No new replies allowed.