Hey! i am writing again with math problem"again" ^^
I made 2 c++ programs so far and they are calculator with "+" and calculator that does avarage.I put them thogether so it would be like if i press "e" i would get one calc and if i press "f" i get another..
#include<iostream>
usingnamespace std;
int main() {
int a,b,c,d,e,f;
cout<<"If you would like to x+y numbers press E"<<endl;
cout<<"If you want to get avarage of you numbers press F"<<endl;
//something is missing here...What should i type for "if pressed E do first thing thats "+" and if pressed F do avarage
//I used if(cin...
if(cin.get()=='e') do;
//
cout<<"Write first number:";
cin>>a;
cout<<"Write second number:";
cin>>b;
c=a+b;
cout<<"Your number is: "<< c <<endl;
//something is missing here...What should i type for "if pressed E do first thing so counting and if pressed F do avarage
cout<<"Write two numbers that you want to get avarage"<<endl;
cin>>a;
cin>>b;
c=(a + b)/2;
cout<<"Your number is: "<<c<<endl;
char z;
cin>> z;
return 0;
}
#include<iostream>
usingnamespace std;
int main()
{
int a,b,c,d,e,f;
cout<<"If you would like to x+y numbers press E"<<endl;
cout<<"If you want to get avarage of you numbers press F"<<endl;
char character;
character=cin.get();
if(character=='e')
{
cout<<"Write first number:";
cin>>a;
cout<<"Write second number:";
cin>>b;
c=a+b;
cout<<"Your number is: "<< c <<endl;
}
if(character=='f')
{
cout<<"Write two numbers that you want to get avarage"<<endl;
cin>>a;
cin>>b;
c=(a + b)/2;
cout<<"Your number is: "<<c<<endl;
}
system("pause");
return 0;
}
I think i didin't make a mistake. You get a character and you assign it to 'character' variable. Depending on what is value of 'character' program adds two values or count an average of two values.
using std::cout;
using std::cin;
using std::endl;
int a(0), b(0), c(0);
char again(0);
do
{
while(!(cin >> a))
{
cout << "error! That's not a number!" << endl;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
while(!(cin >> b))
{
cout << "error! That's not a number!" << endl;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
if (something == 'e')
{
// put code here
}
elseif (something == 'f')
{
// put code here
}
else
{
// error occurred. notify the user
}
cout << "continue? (y or n)" << endl;
while(!(cin >> again) || (again != 'y' || again != 'n'))
{
cout << "error! Invalid input!" << endl;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
} while(again == 'y');
I didn't test that but it is the general idea. It is up to you to finish. I think that template will work for you but you'll have to fill in some of the blanks and change it to suit your needs. You may want to make the check for y/n case insensitive. Good luck!