Math prob

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..

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
#include<iostream>
using namespace 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;
}


Ty for your help...
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
#include<iostream>
using namespace 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.
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
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
  }

  else if (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!
Ty guys.. i rather used mtweeman's idea couse i get it all... yours kempo it little bit harded to get it but ok.. ty anyway:) its working^^
Notice that mtweeman's solution doesn't handle bad input. You should get used to do error handling from the beginning. It's a very important habit.

Also notice that it uses a system call, which is A Bad Thing: http://www.cplusplus.com/forum/articles/11153/
I tried to use simple solution using most of the code given by walkeraki. To handle bad input you can use kempofighteri's code:

1
2
3
4
5
6
while(!(cin>>variable))
{
cout<<"Bad input."<<endl;
cin.clear(); //clear flags
cin.ignore( INT_MAX, '\n'); //remove bad data
}


I always do that, but it seems that waleraki is beginner, so i didn't want to complicate it so much.
Last edited on
Topic archived. No new replies allowed.