Live Chat & Podcast at 1:00PM Eastern on Sunday!
There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
Search
Tag Cloud
access acer asus bios bsod computer crash desktop drive driver drivers error ethernet excel freeze gaming hard drive hardware hdmi internet laptop malware memory missing monitor motherboard network printer problem ram random registry router slow software sound trojan ubuntu 11.10 uninstall usb video virus vista wifi windows windows 7 windows 7 32 bit windows 7 64 bit windows xp wireless
Search
Search for:
Tech Support Guy Forums > Software & Hardware > Software Development >
Coding noob needs help with 4 function calculator

Reply  
Thread Tools
CodeNoob's Avatar
Junior Member with 3 posts.
 
Join Date: Sep 2006
Experience: Beginner
27-Sep-2006, 06:29 PM #1
Coding noob needs help with 4 function calculator
4 function calculator, add subtract multipy divide please help
i cannot get it to loop
here is my code please help:
#include <iostream>
using namespace std;
void showmenu();
float math();
float first,second;
float ans;

char op;
int main()
{
cout<< "Calculator\n ";
showmenu();

cin>>op;

while(op!='!')

{

cout<< "Please Enter The First Number\n";
cin>>first;
cout<< "\nEnter a Second Number\n";
cin>>second;
cout<<first
<<op
<<second
<<"=";
ans=math();

cin.get();

cin.get();
return 0;
}
}
void showmenu()
{
cout<<"Operations are as follows...\n"
<<"'*' Multiplication\n"
<<"'/' Division\n"
<<"'-' Subtraction\n"
<<"'+' Addition\n"
<<"Press '!' to exit\n"
<<"Please Enter An Operation\n";
}

float math()
{
float result;

if (op == '+') {
result=first+second;
cout<< result;
} else if (op == '-') {
result=first-second;
cout<< result;
} else if (op == '*') {
result=first*second;
cout<< result;
} else if (op == '/') {
if ((first == 0)&&(second==0)) {
cout << "Error: Divide by zero\n";
cout << " operation ignored\n";
} else
result=first/second;
cout<< result;
return result;

}
}

Last edited by CodeNoob; 27-Sep-2006 at 06:36 PM..
dquigley's Avatar
Computer Specs
Senior Member with 112 posts.
 
Join Date: Apr 2006
Location: Woodinville, WA
Experience: Advanced
27-Sep-2006, 10:38 PM #2
Hi welcome to TSG!

The primary problem is where you are placing the return 0 in the main() function. You have it placed inside the while { } statement. So the program will always exit without looping.

Good job on how you catch the divide by zero condition... you may want to consider using a switch statement instead of the if and else conditionals. Here is a code snippet that may get you thinking about making your code a bit more bullet proof, it uses another loop type and could be useful to you.

Code:
void menu()
{
    bool valid;
    do
    {
        cout<<"\nOperations are as follows...\n"
        <<"'*' Multiplication\n"
        <<"'/' Division\n"
        <<"'-' Subtraction\n"
        <<"'+' Addition\n"
        <<"Press '!' to exit\n"
        <<"Please Enter An Operation:";
        
        cin >> op;

        cout << "\n";

        switch (op)
        {
            case '*':
            case '/':
            case '-':
            case '+':
            case '!':
                {
                    valid = true;
                    break;
                }
            default:
                {
                valid = false;
                cout << "\nInvalid Operation!\n";
                }
        }
    } 
    while (!valid);
}
Keep at it.

Best,
Dan
Reply

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.
Thread Tools



Facebook Facebook Twitter Twitter TechGuy.tv TechGuy.tv Mobile TSG Mobile
You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 03:43 AM.
Copyright © 1996 - 2011 TechGuy, Inc. All rights reserved.

Powered by Cermak Technologies, Inc.