Calc challenge

This is a discussion on Calc challenge within the C++ Programming forums, part of the General Programming Boards category; I have almost the exact code from the challenge. Code: #include <iostream> int multiply(int x,int y) { return x*y; } ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    465

    Calc challenge

    I have almost the exact code from the challenge.

    Code:
    #include <iostream>
    
        int multiply(int x,int y)
        {
        return x*y;
        }
        
        int divide(int x, int y)
        {
        return x/y;
        }
        
        int add(int x, int y)
        {
        return x+y;
        }
        
        int subtract(int x, int y)
        {
        return x-y;
        }
    
    using namespace std;
    
    int main()
    {
        char op='c';
        int x,y;
        while (op!=e)
        {
        cout<<"What operation would you like to perform add(+), subtract(-),divide(/),multiply(*), (e)exit?";
        cin>> op;
        switch(op)
        {
        case '+':
        cin>>x;
        cin>>y;
        cout<<x<<"+"<<y<<"="<<add(x,y)<<endl;
        break;
        
        case'-':
        cin>>x;
        cin>>y;
        cout<<x<<"-"<<y<<"="<<subtract(x,y)<<endl;
        break;
        
        case'/':
        cin>>x;
        cin>>y;
        cout<<x<<"/"<<y<<divide(x,y)<<endl;
        break;
        
        case'*':
        cin>>x;
        cin>>y;
        cout<<x<<"*"<<y<<multiply(x,y)<<endl;
        break;
        case'e':
        return 0;
        default:
        cout<<"sorry, try again."<<endl;
        }
        }
        
        return 0;
    }
    It says e is undeclared even though I filled in all the blanks on the challenge thing.
    Last edited by cerin; 02-06-2005 at 12:27 PM.
    My computer is awesome.

  2. #2
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    Hello,

    Didn't you mean:
    Code:
    while (op!='e')
    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    hehehe oops...... thanks
    My computer is awesome.

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    I added another cout, because I didn't know if it was working after I entered + so now it prompts you to enter the number.
    My finished product:

    Code:
    #include <iostream>
    
        int multiply(int x,int y)
        {
        return x*y;
        }
        
        int divide(int x, int y)
        {
        return x/y;
        }
        
        int add(int x, int y)
        {
        return x+y;
        }
        
        int subtract(int x, int y)
        {
        return x-y;
        }
    
    using namespace std;
    
    int main()
    {
        char op='c';
        int x,y;
        while (op!='e')
        {
        cout<<"What operation would you like to perform add(+), subtract(-),divide(/),multiply(*), (e)exit?";
        cin>> op;
        switch(op)
        {
        case '+':
        cout<<"Enter the numbers you would like to add."<<endl;
        cin>>x;
        cin>>y;
        cout<<x<<"+"<<y<<"="<<add(x,y)<<endl;
        break;
        
        case'-':
        cout<<"Enter the numbers you would like to subtract."<<endl;
        cin>>x;
        cin>>y;
        cout<<x<<"-"<<y<<"="<<subtract(x,y)<<endl;
        break;
        
        case'/':
        cout<<"Enter the numbers you would like to divide."<<endl;
        cin>>x;
        cin>>y;
        cout<<x<<"/"<<y<<divide(x,y)<<endl;
        break;
        
        case'*':
        cout<<"Enter the numbers you would like to multiply."<<endl;
        cin>>x;
        cin>>y;
        cout<<x<<"*"<<y<<multiply(x,y)<<endl;
        break;
        case'e':
        return 0;
        default:
        cout<<"sorry, try again."<<endl;
        }
        }
        
        return 0;
    }
    Another victory for me!
    My computer is awesome.

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    Just finished the array challenge to.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        int array[8];
        for (int x=0; x<8; x++)
        cin>>array[x];
        for (int x=0; x<8; x++)
        cout<<array[x];
        
        cin.get();
        cin.ignore();
       
    }
    Now what should I do?
    My computer is awesome.

  6. #6
    Registered User
    Join Date
    Feb 2005
    Posts
    59
    Code:
    #include <iostream>
    
        int multiply(int x,int y)
        {
        return x*y;
        }
        
        int divide(int x, int y)
        {
        return x/y;
        }
        
        int add(int x, int y)
        {
        return x+y;
        }
        
        int subtract(int x, int y)
        {
        return x-y;
        }
    
    using namespace std;
    
    int main()
    {
        char op='c';
        int x,y;
        while (op!='e')
        {
        cout<<"What operation would you like to perform add(+), subtract(-),divide(/),multiply(*), (e)exit?";
        cin>> op;
        switch(op)
        {
        case '+':
        cout<<"Enter the numbers you would like to add."<<endl;
        cin>>x;
        cin>>y;
        cout<<x<<"+"<<y<<"="<<add(x,y)<<endl;
        break;
        
        case'-':
        cout<<"Enter the numbers you would like to subtract."<<endl;
        cin>>x;
        cin>>y;
        cout<<x<<"-"<<y<<"="<<subtract(x,y)<<endl;
        break;
        
        case'/':
        cout<<"Enter the numbers you would like to divide."<<endl;
        cin>>x;
        cin>>y;
        cout<<x<<"/"<<y<<"="<<divide(x,y)<<endl;
        break;
        
        case'*':
        cout<<"Enter the numbers you would like to multiply."<<endl;
        cin>>x;
        cin>>y;
        cout<<x<<"*"<<y<<"="<<multiply(x,y)<<endl;
        break;
        case'e':
        return 0;
        default:
        cout<<"sorry, try again."<<endl;
        }
        }
        
        return 0;
    }
    Now when you multiply or divide it doesn't look like the second number is part of the answer. Otherwise excellent job.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calc age use Date of PC
    By miziri in forum C++ Programming
    Replies: 12
    Last Post: 03-18-2006, 04:01 AM
  2. Programming Challenge (for my school)
    By Ezerhorden in forum C++ Programming
    Replies: 2
    Last Post: 01-04-2006, 05:56 AM
  3. Challenge
    By Ikurik in forum Game Programming
    Replies: 1
    Last Post: 08-22-2003, 10:30 PM
  4. Requesting a challenge
    By RealityFusion in forum C++ Programming
    Replies: 8
    Last Post: 08-18-2003, 08:24 PM
  5. Basic Calc
    By Universe in forum C++ Programming
    Replies: 12
    Last Post: 07-17-2002, 10:07 PM

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