#include<iostream>
#include <string>
usingnamespace 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;
#include<iostream>
#include <string>
#include <cctype> // <--- Added. For std::tolower().
usingnamespace 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.
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.