Why is my program not working ?

This is a discussion on Why is my program not working ? within the C++ Programming forums, part of the General Programming Boards category; Code: here my program: #include<iostream> using namespace std; int main() { //Declare Variables char operation = ' '; int num1 ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Location
    Seattle Washington
    Posts
    20

    Why is my program not working ?

    Code:
    here my program:
    
    #include<iostream>
    
    using namespace std;
    
    int main()
    {
    	//Declare Variables 
    	char operation = ' ';
    	int num1 = 0;
    	int num2 = 0;
    	int answer = 0;
    
    	//Input Items
    	cout<<"Enter A(Add),S(Subtract),M(Multiply) or D(Divide):";
    	cin>>operation;
    	operation = toupper(operation);
    	cout<<endl;
    	cout<<"Enter the first number:";
    	cin>>num1;
    	cout<<"Enter the second number:";
    	cin>>num2;
    	cout<<endl;
    
    	//Calculate and Display Output
    
    	switch(operation)
    	{
    	case 'A':
    		answer=num1+num2;
    		cout<<"Answer:"<<answer<<endl;
    		break;
    	case 'S':
    		answer=num1-num2;
    		cout<<"Answer:"<<answer<<endl;
    		break;
    	case 'M':
    		answer=num1*num2;
    		cout<<"Answer:"<<answer<<endl;
    		break;
    	case 'D':
    		answer=num1/num2;
    		cout<<"Answer:"<<answer<<endl;
    		break;
    	default:
    		cout<<"Error"<<endl;
    	}	//end switch
    
    	system("PAUSE");
    	return 0;
    }	//End of Program
    1. i want it to display an error message if any letter other than a,s,d,m is entered( it shouldnt ask for two numbers)

    2. for my case 's' , i want it to swap the number(subtracting second number from first number) but only if the first number is larger or equal to the second number.

    3. for my case 'd'. i want it to divide larger number by smaller number always.

    *** i have read my text book over and over again and i cant seem to figure it out** please help me =]

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    seems like you want stuff to be done only IF certain conditions are met.

    Cprogramming.com Tutorial: If Statements

    1.
    Code:
    if(operation == 'a' || operation == 's' ......................)
    {
    //do this
    }
    else
    //do that
    2.
    Code:
    answer = num1-num2;
    if(answer < 0)
       answer = answer*-1;
    3.
    Code:
    if(num2 != 0 && num1 > num2)
    {
    answer = num1/num2;
    }
    else if(num1 != 0 && num1 < num2)
    {
    answer = num2/num1;
    }
    else
    //assign w/e value to answer...since division by 0 is not allowed
    btw i suggest you ask for the numbers first, then ask operation...that you can evade the first question all together and have the switch decide if the operation is legal... ie:

    Code:
    #include<iostream>
    
    using namespace std;
    
    int main()
    {
    	//Declare Variables 
    	char operation = ' ';
    	int num1 = 0;
    	int num2 = 0;
    	int answer = 0;
    
    	//Input Items
    
    	cout<<"Enter the first number:";
    	cin>>num1;
    	cout<<"Enter the second number:";
    	cin>>num2;
    	cout<<endl;
    
    	cout<<"Enter A(Add),S(Subtract),M(Multiply) or D(Divide):";
    	cin>>operation;
    	operation = toupper(operation);
    
    	cout<<endl;
    	//Calculate and Display Output
    
    	switch(operation)
    	{
    	case 'A':
    		answer=num1+num2;
    		cout<<"Answer:"<<answer<<endl;
    		break;
    	case 'S':
    		answer=num1-num2;
                     if(answer < 0)
                             answer = answer*-1;
    		cout<<"Answer:"<<answer<<endl;
    		break;
    	case 'M':
    		answer=num1*num2;
    		cout<<"Answer:"<<answer<<endl;
    		break;
    	case 'D':
                            if(num2 != 0 && num1 > num2)
                                {
                                        answer = num1/num2;
                                       
    		cout<<"Answer:"<<answer<<endl;
                                  }
                                else if(num1 != 0 && num1 < num2)
                               {
                                   answer = num2/num1;
                     
    		cout<<"Answer:"<<answer<<endl;
                                }
                      else
    		cout<<"Divide by 0"<<answer<<endl;
    		break;
    	default:
    		cout<<"Error"<<endl;
    	}	//end switch
    
    	system("PAUSE");
    	return 0;
    }	//End of Program
    Last edited by rodrigorules; 02-28-2010 at 03:18 AM.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Location
    Seattle Washington
    Posts
    20
    how would you make ask for the a , s , m , d first before it goes to ask for the num1 and num2 ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program stopping working
    By SterlingM in forum C++ Programming
    Replies: 24
    Last Post: 10-17-2009, 02:38 PM
  2. Replies: 5
    Last Post: 02-02-2003, 09:56 AM
  3. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM
  4. Simple Program not working right (in C)
    By DruzeTito in forum C Programming
    Replies: 5
    Last Post: 06-01-2002, 10:14 PM
  5. Program ive been working on called ChatMate
    By dirkduck in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 01-23-2002, 08:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21