Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New Coder
    Join Date
    Sep 2009
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Need Some Suggestions

    I need to write a program that does four functions:

    -read a number
    -read an operator
    -read another number
    -then do the operation


    The program needs to work with integers doing four functions:

    -Add
    -Subtract
    -Divide
    -Multiply


    Other things required:

    -When the user enters C the result is cleared (user re-enters a number)
    -When the user enters X it closes the program.
    -All operations will follow with the enter key.
    -The program should prompt the user on what to do (including C and X)


    Any suggestions would be great.

    Edit:
    I want to use the least amount of code for this calculator (Like things clean)

  • #2
    Senior Coder TheShaner's Avatar
    Join Date
    Sep 2005
    Location
    Orlando, FL
    Posts
    1,126
    Thanks
    2
    Thanked 40 Times in 40 Posts
    Alright, so how about you start coding and we'll correct if need be. We don't do your homework, as per the rules and guidelines of these forums. We also can't guess the programming language you're using, so you need to make sure you note that in your posts.

    Suggestions:
    1. You will need a loop that will only terminate if the letter 'X' is entered
    2. Inside the loop, use a variable to track which step you're on. If step 1, you need a number, 'C', or 'X'; if step 2, an operator, 'C', or 'X'; if step 3, a number, 'C', or 'X'. Once step 3 is complete, you perform the operation, display the result, and reset back to step 1.
    3. The letter 'C' always resets you back to step 1 and the letter 'X' always exits the loop.

    -Shane

  • Users who have thanked TheShaner for this post:

    Recklein3 (09-20-2009)

  • #3
    New Coder
    Join Date
    Sep 2009
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts
    I'm doing this in C++, sorry for not mentioning in it in my original post.

    And I'm not asking any of you to do my homework.
    Just asking if you guys have any advice on how to tackle the future development at hand.

    I'll post the code as soon has a foundation is set.

    Thanks again.

  • #4
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    Try pseudo coding it. Figure out the steps you need to do in order to achieve what you want. Write them down. Then you can start to think what code you would have to write to do that.
    OracleGuy

  • Users who have thanked oracleguy for this post:

    Recklein3 (09-20-2009)

  • #5
    New Coder
    Join Date
    Sep 2009
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts
    So I Just planned out a little that needs to be done and started to add some code in.
    I know it's messy and has errors, but what do you think of the foundation?

    "Read side comments"

    Code:
    #include <iostream>
    
    using namespace std;
    
    void main ()
    
     
    
    {
        
    	
    	long double num1;       /* Good start with using long double num1 and num2 and char choice? */
    	long double num2;
        char choice;s
        
    	
    	for () /* I need to do a for statement */
    
        
    	do /* A do statement */
    
    	
    	{
       
    	cout << "Welcome to ******** Four Function Calculator (v1.2)" << endl;
        cout << "Please choose an option by inputting the number and pressing the enter key, press x to quit and c to clear." << endl;
    	cout << "1 : Addition" << endl;
    	cout << "2 : Subtraction" << endl;
    	cout << "3 : Multiplication" << endl;
    	cout << "4 : Division" << endl;
    
        cin >>choice;  /* Output choice */
        
    	} 
    	
    	while ( choice < '1' || choice > '4' && choice != 'q'); /* attempted a while with using the ****ch (case) , still don't know how to fully make that work */
        
    	if (choice == 'q') break;				/* Need to break and exit out of the program */
    
    	if (choice == 'c')                     /* I need to clear and restart the loop*/
    
       
    	switch (choice) 
    	
    	
               {
               
    
    		   case '1':
                    
    			    cout<< "Please enter a number" << endl;
                    
    				cin>>num;
                    
    				cout<< "Another number to be added" << endl;
                    
    				cin>>num2;
                    
    				cout<<num + num2;
                    
    				cout<< " " << endl;
                    
    				break;
              
    		   
    		   case '2':
                    
    			   
    			    cout<< "Please enter a number" << endl;
                    
    				cin>>num;
                    
    				cout<< "Another number to be subtracted" << endl;
                    
    				cin>>num2;
                    
    				cout<< num - num2;
                    
    				cout<< " " << endl;
                    
    				break;
               
    		   
    		   case '3':
                    
    			    cout<< "Please enter a number" << endl;
                    
    				cin>>num;
                    
    				cout<< "Another one to be multiplied" << endl;
                    
    				cin>>num2;
                    
    				cout<< num * num2;
                    
    				cout<< " " << endl;
                    
    				break;
              
    		   
    		   case '4':
                    
    			    cout<< "Please enter a number" << endl;
                    
    				cin>>num;
                    
    				cout<< "Another one to be divided" << endl;
                    
    				cin>>num2;
                    
    				cout<< num / num2;
                    
    				cout<< " " << endl;
                    
    				break;
              
              
                  
               default:
    			   cout<<"That is an incorrect operator, please enter another choice: ";
                        
                    
    	
    	}
    
    
    }
    Last edited by oracleguy; 09-20-2009 at 05:12 PM. Reason: please use code tags

  • #6
    New to the CF scene
    Join Date
    Sep 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Initially your requirement is
    -read a number
    -read an operator
    -read another number
    -then do the operation

    Why your coding is read the operator first before read the numbers? It does not fulfill the initialy requirement already. Get all the data using Cin first before using the switch function.

  • #7
    New Coder
    Join Date
    Sep 2009
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts
    lol , just noticed that.
    let me fix that
    post some more progress Sunday.

    thanks a lot

  • #8
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    I would get rid of the two if statements before the case, there is no reason to do it that way. Just put them as part of the switch statement.

    Also, you are duplicating a lot of code. Read the first number, ask for the operator and then just have a generic "Enter a second number" type prompt before you do the switch statement.

    Does that make sense?
    OracleGuy

  • Users who have thanked oracleguy for this post:

    Recklein3 (09-21-2009)

  • #9
    New Coder
    Join Date
    Sep 2009
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Sorry I didn't respond back, been busy all day.
    Didn't even get to look at C++ today, just Physics / Physics Lab and chores :/

    Ya, that makes sense.
    Def. working on it Monday.
    So I'll post and update the code.

    Thanks Again

  • #10
    New Coder
    Join Date
    Sep 2009
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Okay so I did what you said.
    Better now?

    Now since that's done, I'm confused on what I need to do for the for statement and get the switch working in the right manner.


    Code:
    #include <iostream>
    
    using namespace std;
    
    void main ()
    
    {
        
    long double num1;       
    long double num2;
    char choice;
        
    for () 
    
    do 
    
    {
       
    cout << "Welcome to Clifford's Four Function Calculator (v1.5)" << endl;
    
    cout << endl << endl << endl;
    
    cout << "Please enter a number: " << endl;
    
    cout << endl;
    
    cin>>num1;
    
    cout << "Please choose an option below and press the enter key to continue, press x to quit and c to clear." << endl;
    cout << "1 : Addition" << endl;
    cout << "2 : Subtraction" << endl;
    cout << "3 : Multiplication" << endl;
    cout << "4 : Division" << endl;
    
    cin >>choice;  
    
    cout << endl;
    
    cout << "Please enter another number: " << endl;
    
    cin>>num2;
    
    } 
    	
    while (choice < '1' || choice > '6' && choice != 'q'); 
        	 
    switch (choice) 
    	
    {
         
    
    		   case '1':	cout << num1 + num2;
                    
    						cout << " " << endl;
                    
    						break;
              
    		   
    		   case '2':	cout << num1 - num2;
                    
    						cout << " " << endl;
    					
    						break;
               
    		   
    		   case '3':	cout << num1 * num2;
                    
    						cout << " " << endl;
                    
    						break;
              
    		   
    		   case '4':	cout << num1 / num2;
    						
    						cout << " " << endl;
                    
    						break;
                  
    		   case '5':	cout << " " << endl;
    						
    						cin; 
    
    						break;
    
    
    		   case '6':	cout << "Shutting Down......" << endl;
    
    						break;
    		   
    		   
    		   default:		cout<< "That is an incorrect operator, please enter another choice: " <<endl;
                        
    	}
    
    
    }
    Last edited by Recklein3; 09-22-2009 at 10:43 PM.

  • #11
    New Coder
    Join Date
    Sep 2009
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Okay so I worked on it more over night and this is what stands....
    The only problem I have is that when you enter the operator wrong, it either asks again once you put another operator in or it dies completely...

    Due in an hour, be glad if someone can help


    #include <iostream>

    using namespace std;

    void main ()
    {
    int a=0;
    int b=0;
    int clear=1;
    int oper=0;
    int loop=0;
    char c;

    cout << "Welcome to Clifford's Four Function Calculator (v1.803)" <<endl;

    cout << endl;

    do{
    do
    {
    if (clear==1)
    {cout<< "Please enter a whole number: ";
    cin>>a;}
    oper=0;

    cout << endl;

    cout<< "Please enter an operator (+,-,*,/) or press X to turn off and C to clear information: ";
    do

    {

    clear=0;
    cin>>c;
    switch (c)

    {

    case '+':
    case '-':
    case '*':
    case '/':
    clear=0;
    break;
    case 'C':
    case 'c':
    clear=1;
    cout << "History cleared..." << endl;
    break;
    case 'X':
    case 'x':
    cout << "Shutting down..." << endl;
    exit (0);
    break;
    default:
    oper=1;
    cout << "That is an incorrect operator, please choose one of the following (+,-,*,/) or press X to turn off and C to clear information:: ";
    }
    }while (oper==1);
    }while (clear==1);
    cout << "Please enter another whole number: ";
    cin>>b;
    if(c=='/'&&b==0)
    cout << "Error: Trying to divide by zero." << endl;
    else
    {
    cout <<a<<""<<c<<""<<b<< "=";
    switch (c)
    {
    case '+':
    a=a+b;
    break;
    case '-':
    a=a-b;
    break;
    case '*':
    a=a*b;
    break;
    case '/':
    a=a/b;
    break;

    }
    cout << a << endl;
    }
    }while(loop==0);
    }


  •  

    Posting Permissions

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