Thread: Puzzling problem

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    151

    Puzzling problem

    Code:
    #include <iostream>
    #include <cmath>
    
    using namespace std;
    
    void math()
    {
    	char line;
    	float number;
    	float answer;
    	char yn = 'y';
    
    	do{
       cout<<"enter what you want to do (m for math, o for math operations, s for square root, f for farenheit to celsius, c for celsius to farenheit)\n";
       cin>>line;
       if(line == 'o'){
    	   cout<<"x,/,+,-,^,s(press enter then enter the number)\n";
    	   answer = 0;
       }
      if(line == 's'){
    	   cout<<"enter a number: ";
    	   cin>>number;
    	   answer = sqrt(number);
      }
    if(line == 'm'){
    		float x;
            float y;
            char a;
    		float answer;
    		cout<<"enter a problem\n";
    	   cin>>x>>a>>y;
        switch (a){
    	case 'x':
    		answer = x*y;
    		break;
    	case '/':
    		answer = x/y;
    		break;
    	case '+':
    		answer = x+y;
    		break;
    	case '-':
    		answer = x-y;
    		break;
    	case '^':
    		answer = pow (x,y);
    		break;
    }
    }
       if(line == 'c'){
    	   float degrees;
    
    	   cout<<"enter degrees celsius\n";
    	   cin>> degrees;
    	   answer = 1.8*degrees+32;
       }
       if(line == 'f'){
    	   float degrees;
    
    	   cout<<"enter degrees farenheit\n";
    	   cin>> degrees;
    
    	   answer = (degrees-32)*1.8;
       }
       cout<<answer<<endl;
       cout<<"Do you want to calculate something else<y/n>\n";
       cin>>yn;
    	}while(yn == 'y');
    }
    Code:
    #include <iostream>
    #include "calc.h"
    
    using namespace std;
    
    int main()
    {
    	math();
    }
    my problem is when you do an equation (x,/,+,-,^) it gives you an error or the previous number, and this only happens when you press m at the beginning, no where else.

  2. #2
    Allways learning cs_student's Avatar
    Join Date
    Aug 2008
    Location
    ~/
    Posts
    39
    After the conditional to check for the 'm' char, you re-declare the "answer" variable.

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    151
    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. Totally puzzling IO problem
    By Vorok in forum C++ Programming
    Replies: 5
    Last Post: 01-06-2003, 07:10 PM