Thread: Please help me figure this out for class, important.

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    60

    Please help me figure this out for class, important.

    This is my assignment and it is due tomorrow. I am only doing it now because my other classes have me swamped. This is supposed to be my fall break. Anyways, I really do not understand this assignment, but what code i do have so far is included at the bottom. As far as I can tell she wants 5 functions... can someone please explain this stuff to me, I do not want it written for me, but please explain this to me like I am a beginner because I am.

    CSCI 1010 Programming Assignment 5

    A. Purpose- Write a program that uses functions, switch statement, type char data and a for loop.
    B. Due Date- Due by 8 p.m. on Wednesday 10/18/06

    C. Program Description – Name the program a5yourlastname.cpp
    Write a C++ Program that uses functions to solve simple mathematics problems. The main function should prompt for and input how many integer sets of data to process. The main function should use a loop that will prompt the user to input a single character (an uppercase A, S, M or D) followed by two real numbers. Use the switch statement (see pages 270- 274) to determine which character was entered and call one of the following 4 functions that the program must declare and define:
     Add if A was entered to add the two type double numbers and return their sum
     Subtract if S was entered to subtract the second number from the first and return the difference
     Multiply if M was entered to multiply the two numbers and return their product
     Divide if D was entered to divide the second number into the first and return the quotient. If the second number is 0 do not call the function but display a message that division by zero is not allowed.

    After the appropriate function is called and the answer is returned to the main function call a void function that is passed the 2 numbers, the symbol for the math operator and the answer. The function will display the answer as an equation:
    num1 symbol num2 = answer
    where num1 and num2 are displayed with one decimal place and the symbol is +, -, * or /.

    If the single character entered is not an A, S. M or D then do not prompt for the two numbers but display an appropriate error message.

    The program must be written to declare, define and call the four functions described above. It must also use the switch statement and process integer sets of data. A sample run of the program is below where the bolded numbers are the inputs typed as the program is running.

    How many problems to solve? 7
    Enter an A, S, M, or D: M
    Enter two real numbers to multiply: 2.2 3.1
    2.2 * 3.1 = 6.8

    Enter an A, S, M, or D: S
    Enter two real numbers to subtract: 5.6 3.1
    5.6 - 3.1 = 2.5

    Enter an A, S, M, or D: D
    Enter two real numbers to divide: 2.2 0
    Division by zero is not allowed

    Enter an A, S, M, or D: A
    Enter two real numbers to add: 6.51 2.11
    6.5 + 2.1 = 8.6

    Enter an A, S, M, or D: D
    Enter two real numbers to divide: 2.2 2.0
    2.2 / 1.0 = 1.1

    Enter an A, S, M, or D: P
    Must enter an A, S, M or D

    Enter an A, S, M, or D: A
    Enter two real numbers to add: 1.2 1.1
    1.2 + 1.1 = 2.3



    Code:
    #include<iostram>
    #include<iomanip>
    
    double add (double num1, double num2);
    //add num1 to num2
    
    int main()
    {
            double total;
            char letter;
            cout << "Enter a letter (A,S,M,D):" << endl;
            cin >> letter;
            cout << "Enter two numbers:" << endl;

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I am only doing it now because my other classes have me swamped.
    For the record, that's never been a good excuse.

    >can someone please explain this stuff to me
    It's very straightforward. The requirements pretty much spell out the implementation, so all you need to do is turn the words into code. Actually, you'll have an easier time mirroring the output:

    >How many problems to solve? 7
    This is clearly wanting you to use a loop. So add that to your code:
    Code:
    #include <iostream>
    
    int main()
    {
        int n;
    
        std::cout<<"How many problems to solve? ";
    
        if ( std::cin>> n ) {
            for ( int i = 0; i < n; i++ ) {
                // Do one problem
            }
        }
    }
    >Enter an A, S, M, or D: M
    This is where they want you to use a switch in the "Do one problem" part of the code. There are five cases, 'A', 'S', 'M', 'D', and default. The first four are your operation functions and the last is a catchall for bad input where you can show an error message.

    >Enter two real numbers to multiply: 2.2 3.1
    This is inside your operation functions. It's as simple as prompting for input, taking input, and doing a simple mathematical calculation.

    >2.2 * 3.1 = 6.8
    This is another function that you can call in each case of the switch or in the operation functions.

    I would be very surprised if you didn't meet the deadline for this assignment. It's short and easy, even by beginner standards.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    60
    Thank you for your help, but what is this std::
    she has not covered that yet.

  4. #4
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Has she covered 'using namespace std;'? You can put std:: on front of some common things or you can use namespaces.

    Code:
    using namespace std;
    
    [...]
    
    cout<< "WOW!";
    or
    Code:
    std::cout<< "WOW!";
    While namespaces are nice, std:: is better for large projects. Plus, it grows on you ... eventually. It did on me anyways.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    60
    she covered putting "using namespace std;" in the code, btu thats it, she hasnt covered putting "std" anywhere else.

  6. #6
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Throw the "using namespace std;" at the top then, and get rid of all the "std::"'s, and do as prelude says

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    60
    i dont get how the switch relates to the function. i know how to declare a function and function definition, but do i need to put an if statement in each definition? if's dont require if elses right?


    I am only in this class because I am re-taking it to raise my grade. 4 1/2 years ago i chose this as a major and changed my mind. I just want to keep a good gpa now.
    Last edited by WinterInChicago; 10-17-2006 at 01:44 PM.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Have you read your book or course notes for an example of a switch statement?

    switch ( letter ) case 'A': doAdd(); break;
    Kinda thing.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    60
    Quote Originally Posted by Salem
    Have you read your book or course notes for an example of a switch statement?

    switch ( letter ) case 'A': doAdd(); break;
    Kinda thing.

    I have, she did not give us a simple function program to write before this one. Of course this may be simple to you.

    So do I just write:

    Code:
    switch(letter)
         {
          case 'A' : total = add (n1, n2);
    in that case, i dont see how it calls the function.

    err.. im lost

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    60
    am i on the right track?


    Code:
    using namespace std;
    
    #include<iostram>
    #include<iomanip>
    
    
    
    double add (double num1, double num2);
    //add num1 to num2
    
    double multiply (double num1, double num2);
    //multiply num1 and num2
    
    double subtract (double num1, double num2);
    //subtract num2 from num1
    
    double divide (double num1, double num2);
    //Divide num1 by num2
    
    
    int main()
    {
    
            int l;
            double total;
            char letter;
    
            cout << "How many times would you like to run the program?" << endl;
            cin >> l;
            for (int i=1; i<=l; i++)
                    {
                    cout << "Enter a letter A, S, M, or D:" <<endl;
                    cin >> letter;
                    switch(letter)
                    {
                      case 'A' : total = add (n1, n2);
                      break;
                      case 'S' : total = subtract (n1, n2);
                      break;
                      case 'M' : total = multiply (n1, n2);
                      break;
                      case 'D' : total = divide (n1, n2);
                      break;
                    }
    
    double add (double num1, double num2)
            {
            double sum;
            sum = num1 + num2;
            return sum;
            }
    
    double multiply (double num1, double num2)
            {
            double ans;
            ans = num1 * num2;
            return ans;
            }
    
    double subtract (double num1, double num2)
            {
            double ans2;
            ans = num1 - num2
            return ans2;
            }
    
    double divide (double num1, double num2)
            {
            double ans3;
            ans = num1 / num2;
            return ans3;
            }
    Last edited by WinterInChicago; 10-17-2006 at 02:13 PM.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Yes, you're getting there!

    Combine your last two posts.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Throw the "using namespace std;" at the top then, and get rid of all the "std::"'s, and do as prelude says
    How about do as Prelude says and not throw a using directive at the top? That feature was only meant as a crutch for moving older code to standard implementations, not as a convenience for new code.

    Any yoyo who recommends it is flirting with danger...because I'll slap them silly.
    My best code is written with the delete key.

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    60
    so i addedin what looked like correct switch statements.

    i still have to put in

    displayit (n1, n2, total, '+') in the int main

    and the

    void displayit (double num1, double num2, double ans, char symbol)

    I think the last 2 in the void are incorrect, I was writing notes instead of actual code.

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    60
    Quote Originally Posted by Prelude
    >Throw the "using namespace std;" at the top then, and get rid of all the "std::"'s, and do as prelude says
    How about do as Prelude says and not throw a using directive at the top? That feature was only meant as a crutch for moving older code to standard implementations, not as a convenience for new code.

    Any yoyo who recommends it is flirting with danger...because I'll slap them silly.

    id rather stay away from code that the prof hasnt covered, thank you tho

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    60
    on the assignment it says enter two numbers to multiply, enter two numbers to divide. that would be an if statement right? or can i put a cout and cin in the switch? what im saying is, they hit 'A'. the program will know they want to add so it asks "enter 2 numbers to add" ...so do you suggest an if statement for that.


    oh and I emailed the professor what code I had so far to ask for help.... she told us we should if we needed.

    turns out shes out of the office all break long. isnt it grand that she made us do work over our fall break and she wasnt even there to help us?
    Last edited by WinterInChicago; 10-17-2006 at 02:39 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 3 dimensional figure volume
    By thekautz in forum C++ Programming
    Replies: 2
    Last Post: 01-20-2009, 05:22 PM
  2. trying to figure out someone's code for a plugin
    By paulpars in forum C++ Programming
    Replies: 4
    Last Post: 07-20-2006, 10:57 AM
  3. newb to C, cant figure something out.
    By Phate4219 in forum C Programming
    Replies: 16
    Last Post: 03-06-2006, 01:47 AM
  4. What Is Important In Making Games?
    By Krak in forum C++ Programming
    Replies: 6
    Last Post: 05-07-2004, 08:12 PM
  5. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM