CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2012
    Posts
    5

    Program With Incorrect Answers

    Hi everyone,

    I need some help with my C++ code. Here is the problem specification:

    "16. In this exercise, you are to create a program that adds, subtracts, multiplies, or divides two integers. The program will need to get a letter (A for addition, S for subtractions, M for multiplication, or D for division) and two integers from the user. If the user enters an invalid letter, the program should display an appropriate error message before the program ends. If the letter is A (or a), the program should calculate and display the sum of both integers. If the letter is S (or s), the program should display the difference between both integers. When calculating the difference, always subtract the smaller number from the larger one. If the letter is M (or m), the program should display the product of both integers. If the letter is D (or d), the program should divide both integers, always dividing the larger number by the smaller one."

    And here is the test data. I am posting the results from my desk-check table.

    operation first integer second integer answer
    A 10 20 30
    a 45 15 60
    S 65 50 15
    s 7 13 6
    G -1
    M 10 20 200
    d 45 15 3
    d 50 100 2

    Then, I transferred my program into a source file. Here it is:

    //Exercise16.cpp - display answer of two integers
    //Created/revised by <Patrick> on <October 7 2012>

    #include <iostream>
    using namespace std;

    int main()
    {
    //declare variables
    int firstInteger = 0;
    int secondInteger = 0;
    int answer = 0;
    char operation = ' ';

    //enter the operation
    cout << "Enter A (addition), S (subtraction), M (multiplication), D (division): ";
    cin >> operation;
    operation = toupper (operation);

    //determine whether the operation is valid
    if (operation != 'A' && operation != 'S' && operation != 'M' && operation != 'D')
    {
    answer = -1;
    cout << "Invalid operation: " << endl;
    }
    else
    {
    //enter the integers
    cout << "First integer: ";
    cin >> firstInteger;
    cout << "Second integer: ";
    cin >> secondInteger;

    //calculate the answer
    if (operation == 'A')
    answer = firstInteger + secondInteger;
    else
    if (operation == 'S')
    if (firstInteger > secondInteger)
    answer = firstInteger - secondInteger;
    else
    if (secondInteger > firstInteger)
    answer = secondInteger - firstInteger;
    else
    if (operation == 'M')
    answer = firstInteger * secondInteger;
    else
    if (operation == 'D')
    if (firstInteger > secondInteger)
    answer = firstInteger / secondInteger;
    else
    if (secondInteger > firstInteger)
    answer = secondInteger / firstInteger;
    //end if
    //end if
    //end if
    //end if
    //end if
    //end if
    //end if
    //end if
    }//end if

    //display the answer
    cout << "Answer: " << answer << endl;

    system ("pause");
    return 0;
    } //end of main function

    After putting in the data, everything worked fine, except the last two operations, which are M (multiplication) and D (division). All the answers for the last two operations essentially give me a 0.

    I would like to know where I went wrong. Hopefully, someone can help me figure out what is wrong.
    Please, respond to this thread as soon as possible.
    Last edited by ForTomorrow; October 7th, 2012 at 05:16 PM.

  2. #2
    Join Date
    Sep 2012
    Posts
    5

    Re: Program With Incorrect Answers

    Hi everyone,

    I need some help with my C++ code. Here is the problem specification:

    "16. In this exercise, you are to create a program that adds, subtracts, multiplies, or divides two integers. The program will need to get a letter (A for addition, S for subtractions, M for multiplication, or D for division) and two integers from the user. If the user enters an invalid letter, the program should display an appropriate error message before the program ends. If the letter is A (or a), the program should calculate and display the sum of both integers. If the letter is S (or s), the program should display the difference between both integers. When calculating the difference, always subtract the smaller number from the larger one. If the letter is M (or m), the program should display the product of both integers. If the letter is D (or d), the program should divide both integers, always dividing the larger number by the smaller one."

    And here is the test data. I am posting the results from my desk-check table.

    operation first integer second integer answer
    A 10 20 30
    a 45 15 60
    S 65 50 15
    s 7 13 6
    G -1
    M 10 20 200
    d 45 15 3
    d 50 100 2

    Then, I transferred my program into a source file. Here it is:



    Code:
    //Exercise16.cpp - display answer of two integers
    //Created/revised by <Patrick> on <October 7 2012>
    
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	//declare variables 
    	int firstInteger = 0;
    	int secondInteger = 0;
    	int answer = 0;
    	char operation = ' ';
    
    	//enter the operation
    	cout << "Enter A (addition), S (subtraction), M (multiplication), D (division): ";
    	cin >> operation;
    	operation = toupper (operation);
    
    	//determine whether the operation is valid
    	if (operation != 'A' && operation != 'S' && operation != 'M' && operation != 'D')
    	{
    	   answer = -1;
    	   cout << "Invalid operation: " << endl;
    	}
    	else
    	{
    		//enter the integers
    		cout << "First integer: ";
    	    cin >> firstInteger;
    	    cout << "Second integer: ";
    	    cin >> secondInteger;
    
    		//calculate the answer
            if (operation == 'A')
    	       answer = firstInteger + secondInteger;
    		else 
    		   if (operation == 'S')
    		      if (firstInteger > secondInteger)
    			     answer = firstInteger - secondInteger;
    		      else 
    			     if (secondInteger > firstInteger)
    				    answer = secondInteger - firstInteger;
    			     else 
    				    if (operation == 'M')
    				       answer =  firstInteger * secondInteger;
    			        else 
    					   if (operation == 'D')
    				          if (firstInteger > secondInteger)
    					         answer = firstInteger / secondInteger;
    					      else 
    						     if (secondInteger > firstInteger)
    						        answer = secondInteger / firstInteger;
    							 //end if
    	                      //end if
    	                   //end if
    	                //end if
    	             //end if
    	          //end if
    	       //end if
    	    //end if       
    	}//end if         
    	
    	//display the answer
        cout << "Answer: " << answer << endl;
    	
    	system ("pause");
    	return 0;
    }	//end of main function


    After putting in the data, everything worked fine, except the last two operations, which are M (multiplication) and D (division). All the answers for the last two operations essentially give me a 0.

    I would like to know where I went wrong. Hopefully, someone can help me figure out what is wrong.
    Please, respond to this thread as soon as possible.
    Last edited by ForTomorrow; October 7th, 2012 at 06:25 PM.

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Program With Incorrect Answers

    You have to debug your code step-by-step to see what goes wrong!
    Besides, your code indentations in the "//calculate the answer" block looks inproper that makes uderstanding your code very difficult!
    Victor Nijegorodov

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Program With Incorrect Answers

    It took maybe 3 seconds to find the problem using the debugger. Here's a clue. Indent your if statements properly, and use braces to control what falls under which if statement. When you do that, the problem will become immediately apparent.

    I have no idea what you're trying to accomplish by putting all those //end if comments in there, but without a closing brace, they mean nothing. The compiler is ignoring them. Also, if you properly indent your code, the end of statements will be apparent without the need for comments.
    Last edited by GCDEF; October 8th, 2012 at 08:14 AM.

  5. #5
    Join Date
    Jan 2009
    Posts
    596

    Re: Program With Incorrect Answers

    As the 'operation' variable is a char, you could replace that long list of if/else statements with a switch statement. This would make the code clearer.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured