Help - Search - Members - Calendar
Full Version: My Calculator Program
Sal's RuneScape Forum > Everything... Not RuneScape > Tech Talk > Programming & Web Development
Shade Pure
This is the project I am working on, I need help with the subtraction part so if somebody could help me, I would greatly appreciate it smile.gif.

Spoiler: Click to Toggle the Spoiler.
CODE
// Calculator
#include <cstdio>
#include <stdlib.h>
#include <iostream>
using namespace std;

int funcAdd(void){
    int accumulator = 0;
    for(;;){
            int value = 0;
            cout << "Enter next number: ";
            cin >> value;
            
            if (value < 0){
                      break;
            }
            
            accumulator = accumulator + value;
    }
    
    return accumulator;
}

int funcSub(void){
}

int funcMulti(void){
}

int funcDiv(void){
}

int funcSquare(void){
}

int main(int nNumberofArgs, char* pszArgs[]){
    string choice;
    cout << "Please choose from the following: " << endl;
    cout << "A: Addition\n"
         << "B: Subtraction\n"
         << "C: Multiplication\n"
         << "D: Division\n"
         << "E: Square" << endl;
    cin >> choice;
    char ch = choice[0];
    ch = toupper(ch);
    switch (ch){
           case 'A':
                int accumulatedValue;
                for(;;){
                        cout << "Enter next sequence" << endl;
                        accumulatedValue = funcAdd();
                        
                        if (accumulatedValue == 0){
                                             break;
                        }
                        
                        cout << "The total is "
                             << accumulatedValue
                             << "\n"
                             << endl;
                }
                
                cout << "Thank you" << endl;
                break;
           case 'B':
                funcSub();
                break;
           case 'C':
                funcMulti();
                break;
           case 'D':
                funcDiv();
                break;
           case 'E':
                funcSquare();
                break;
           case 'F':
                cout << "Please enter either A, B, C, D, or E." << endl;
                break;
           }
          
           system("PAUSE");
           return 0;
}


[Close]
Roy
For the subtraction I think you need to put the first number in the accumulator variable and do 'accumulator = accumulator - value'.
Shade Pure
I tried that but it kept giving me a negative answer.
Roy
QUOTE (Shade Pure @ Aug 30 2009, 06:21 PM) *
I tried that but it kept giving me a negative answer.

Did you switched around the terms in subtraction of the accumulator and Value vars?
Shade Pure
I think so, but I don't fully know what you mean... oh, I rewrote the main() part of my code so now it's a switch statement, I'll edit my post tomorrow.
cjgone
C right?
CODE
cin >> choice;
int a;
int b;
int total = 0;
cin>>a;
cin>>b;
// a and b are 2 numbers
//assuming choice = A,B,C,D, or E
total = total + (a + b)(choice=="A") + ( a -b )(choice=="B")(b > 0) + (a*b)(choice=="C") + (a/b)(choice=="D")(b!=0) + (a^b)(choice=="E");


Won't work in Java, but I'n pretty sure in C\C++ w\e true = 1 and false = 0.

Use an Enum data type with letters, to convert to numbers, then you can easily do the check if the letters fall in A,B,C,D,or E
Shade Pure
QUOTE (cjgone @ Aug 31 2009, 01:32 PM) *
C right?
CODE
cin >> choice;
int a;
int b;
int total = 0;
cin>>a;
cin>>b;
// a and b are 2 numbers
//assuming choice = A,B,C,D, or E
total = total + (a + b)(choice=="A") + ( a -b )(choice=="B")(b > 0) + (a*b)(choice=="C") + (a/b)(choice=="D")(b!=0) + (a^b)(choice=="E");


Won't work in Java, but I'n pretty sure in C\C++ w\e true = 1 and false = 0.

Use an Enum data type with letters, to convert to numbers, then you can easily do the check if the letters fall in A,B,C,D,or E


I rewrote my code using a switch statement because I think it will use less code and I think it looks cleaner.
cjgone
QUOTE (Shade Pure @ Aug 31 2009, 04:23 PM) *
QUOTE (cjgone @ Aug 31 2009, 01:32 PM) *
C right?
CODE
cin >> choice;
int a;
int b;
int total = 0;
cin>>a;
cin>>b;
// a and b are 2 numbers
//assuming choice = A,B,C,D, or E
total = total + (a + b)(choice=="A") + ( a -b )(choice=="B")(b > 0) + (a*b)(choice=="C") + (a/b)(choice=="D")(b!=0) + (a^b)(choice=="E");


Won't work in Java, but I'n pretty sure in C\C++ w\e true = 1 and false = 0.

Use an Enum data type with letters, to convert to numbers, then you can easily do the check if the letters fall in A,B,C,D,or E


I rewrote my code using a switch statement because I think it will use less code and I think it looks cleaner.



Might be better if you had 1000 different operations to perform. The above method is more optimal than using switch statements. I never found using unnecessary lines to make code look cleaner. And disregard the enum, you actually don't even need it in this case.

QUOTE
CODE
int funcAdd(void){
    int accumulator = 0;
    for(;;){
            int value = 0;
            cout << "Enter next number: ";
            cin >> value;
            
            if (value < 0){
                      break;
            }
            
            accumulator = accumulator + value;
    }

Enter text number shouldn't be in this specific function, outside 'cuz you use enter text everytime. Uneeded if statement which I optimized above, uneeded break.

You just need

accumulator = accumulator + value(value > 0);

You can avoid all these checks by just doing the above method but replace "a" with accumulator and total with accumulator.
Shade Pure
Wow, why the hell didn't I think about using (value > 0) and thanks cj, you have been a great help smile.gif, I never would've even thought of doing what you first posted.

EDIT: How would I be able to allow the user to do more than sequence though?
cjgone
QUOTE
EDIT: How would I be able to allow the user to do more than sequence though?


What are you specifically looking to do?
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.