Thread: A calculator program problem?

  1. #1
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    Question A calculator program problem?

    Greetings;
    I am trying to total up this operation but for some reason my calcu;ator isn't calculating. Does anyone know why? Does anyone know MVC++ really well. My question is how do you use the debugger to debug the program and watch the program you created step-by-step?

    Here is my code
    Code:
    #include <iostream>		
    #include<string>
    #include <cassert>
    using namespace std;
    
    string apply(char operation);
    int factorial(int n);
    
    int op1, op2, total;
    char operation;
    
    int main()
    {
    
    	cout << "Please enter: \n"
    		 << "+ to add two numbers;\n"
    		 << "- to subtract two numbers:\n"
    		 <<	"* multiply two numbers;\n"
    		 <<	"/ divide two numbers.\n";
    	
    	cin >> op1 >> operation >> op2;
    	total = operation;
    	cout <<  op1 << operation << op2 << "=" << total <<endl;
    	return 0;
    
    }
    
    string apply(char operation)
    {
    	switch (operation)
    	{
    	
    	case '+': "op1 + op2";
    		break;
    	case '-': "op1 - op2";
    		break;
    	case '*': "op1 * op2";
    		break;
    	case '/': "op1 / op2";
    		break;
    	default:
    		cerr << "You selected an invalid operation!" << endl;
    		return " ";
    		
    	}
    }
    
    
    \A work in progress... pay no attention
    \int factorial(int n)
    \{
    \	assert( n >= 0);
    \	double product = 1;
    \	while ( n > 1 )
    \	{
    \		product *= n;
    \		n--;
    \	}
    \return product;
    \}

  2. #2
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    Ctrl+F10 = run to cursor
    F11 = step into
    F10 = step over
    Shift+F11 = step out

    Use the watch feature to see the values of your variables during runtime.

    You've set total to equal your char operation btw. This isn't right.
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  3. #3
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    The problem starts after the line
    Code:
    cin >> op1 >> operation >> op2;
    in main. The next line should invoke apply like this

    Code:
    total = apply(operation );
    And also should the apply-function look a little different. Change each case statement to return the result like

    Code:
            case '+': return (op1 + op2);
    		//break; 
    	case '-': return (op1 - op2);
    		//break; 
    	case '*': return (op1 * op2);
    		//break;
    	case '/': return op1 / op2;
    		//break;
    	default:
    		cerr << "You selected an invalid operation!" << endl;
    		return 0;//Cases little trouble because an arithmetric operation can return 0
    All break(s) are unnessesary in this case because the function exits after a return statement. And the last problem is..... I´ll leave it to you to find it

  4. #4
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378
    Code:
    total = apply(operation );
    This is giving this error:
    C:\Program Files\Microsoft Visual Studio\MyProjects\ch9sec51\calculate.cpp(34) : error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<
    char> >' (or there is no acceptable conversion)

    How do I fix this?

  5. #5
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    And the last problem is..... I´ll leave it to you to find it
    Hmm I hope you have tried.. anyway change

    Code:
    //string apply(char operation);
    int apply(char operation);
    Don´t forget to do the same thing on the function definition.

Popular pages Recent additions subscribe to a feed

Similar Threads

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