Go Back   Computer Forums > General Computing > Programming
Click Here to Login
Join Computer forums Today


Reply
 
Thread Tools Search this Thread Display Modes
 
Old 06-20-2011, 09:26 AM   #11
7D8
In Runtime
 
7D8's Avatar
 
Join Date: Oct 2006
Posts: 209
Default Re: I need C++ help.

pseudo code is good. I personally don't use it, I like to just think of the problem abstractly then pin point a starting point and start test coding. Then I get to a point where I can say that I'm going to go down a certain path, and I either go back fix and comment my test code, or I keep rolling depending on the quality of the test code. A LOT of programmers do say to use pseudo code. Although I must admit that I've never actually witnessed anyone use it with my own two eyes.

also, instead of just using variables like num1 and num2, use an array instead. That way you can later loop through the array and perform a whole assortment of calculations. I'm sure there are even libraries that will do the calculations for you, just have to shove them in a for loop and distinguish between which calculation you want.
__________________

7D8 is offline   Reply With Quote
Old 06-20-2011, 11:09 AM   #12
Fully Optimized
 
strollin's Avatar
 
Join Date: Mar 2011
Location: USA
Posts: 1,938
Default Re: I need C++ help.

I don't write pseudo code either but I've been writing code for 30 years. For someone just starting, it helps them understand what it is they want the program to do without needing to worry about the details of C++ syntax and such. It's simply a tool.

OP - Think about what you want your calc to do. You need to ask the user if they want to add, subtract, multiply or divide. Once you know that, then you can prompt them for some numbers (I'd limit input to 2 numbers, 3 is more complicated). Once you have that info from the user, do the calculation and display the result. At that point do you want to exit the program or prompt them for input for another calculation?

When looping in a program, think of what needs to be done to set things up prior to entering the loop, then what needs to be done while in the loop, what will be the trigger to stop looping and finally what needs to be done after the loop. As tomek pointed out, your While loop needs work, if I am correct the (final == num1 + num2 + num3) will always evaluate to false which means you have an ifinite loop.

This code should compile and run but won't be very useful:

Code:
#include "stdafx.h" 
#include <iostream>
#include <cstdlib>

using namespace std;
int main() 
{
    double num1, num2, final; 

    cout << "This is my calculator. It follows the BEDMAS Rules. \n" ;              
    cout << "Enter the first number to add. \n" ;     
    cin >> num1 ;

    cout << "Enter the second number. \n" ;     
    cin >> num2 ;      

    final = num1 + num2;

    cout << num1 << " + "  << num2 << " = " << num3;      

    return 0; 
}
__________________

strollin is offline   Reply With Quote
Old 06-21-2011, 05:53 PM   #13
Baseband Member
 
Join Date: Jun 2011
Posts: 30
Default Re: I need C++ help.

Hey,
I cleaned the script completely and started over. I think I made it better but I have compiling errors that I can't understand. Can someone help me?
Code:
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <math.h>

using namespace std;

int main()
{
    int num1 ;
    int num2 ;
    int num3 ;
    int final ;
    for (;;) {
    do{
    cout << "This is my calculator. It follows the BEDMAS Rules. \n" ;
    cout << endl ;
    cout << "Enter two or three numbers. \n" ;
    cout << "Use * to multiply. \n" ;
    cout << "Use + to add. \n" ;
    cout << "Use - to subtract. \n" ;
    cout << "Use / to divide. \n" ;
    cout << "Enter 999 to exit the program. \n";
    cin >> final ;
    } while(final < '1'|| final > '16' && final != 'a');

    if(final == 'a') break ;
    switch (final) {

    case '1':
        cout << "Please enter a number! \n" ;
        cin >> num1 ;
        cout << "Please enter the next number! \n" ;
        cin >> num2 ;
        cout << "Please enter the final number! \n" ;
        cin >> num3 ;
        final = num1 + num2 + num3;
        cout << final ;
        cout << endl ;
        cout << "You just added "; cout << num1; cout << " to "; cout << num2; cout << " and added that to "; cout << num3;
        break ;
    case '2':
        cout << "Please enter a number! \n" ;
        cin >> num1 ;
        cout << "Please enter the next number! \n" ;
        cin >> num2 ;
        cout << "Please enter the final number! \n" ;
        cin >> num3 ;
        final = num1 + num2 - num3;
        cout << final ;
        cout << endl ;
        cout << "You just added "; cout << num1; cout << " to "; cout << num2; cout << " and subtracted "; cout << num3;
        break ;
            case '3':
        cout << "Please enter a number! \n" ;
        cin >> num1 ;
        cout << "Please enter the next number! \n" ;
        cin >> num2 ;
        cout << "Please enter the final number! \n" ;
        cin >> num3 ;
        final = num1 + num2 * num3;
        cout << final ;
        cout << endl ;
        cout << "You just added "; cout << num1; cout << " to "; cout << num2; cout << " and multiplied that by "; cout << num3;
        break ;
            case '4':
        cout << "Please enter a number! \n" ;
        cin >> num1 ;
        cout << "Please enter the next number! \n" ;
        cin >> num2 ;
        cout << "Please enter the final number! \n" ;
        cin >> num3 ;
        final = num1 + num2 / num3;
        cout << final ;
        cout << endl ;
        cout << "You just added "; cout << num1; cout << " to "; cout << num2; cout << " and divided that by "; cout << num3;
        break ;
            case '5':
        cout << "Please enter a number! \n" ;
        cin >> num1 ;
        cout << "Please enter the next number! \n" ;
        cin >> num2 ;
        cout << "Please enter the final number! \n" ;
        cin >> num3 ;
        final = num1 - num2 - num3;
        cout << final ;
        cout << endl ;
        cout << "You just subtracted "; cout << num1; cout << " by "; cout << num2; cout << " and subtracted that by "; cout << num3;
        break ;
            case '6':
        cout << "Please enter a number! \n" ;
        cin >> num1 ;
        cout << "Please enter the next number! \n" ;
        cin >> num2 ;
        cout << "Please enter the final number! \n" ;
        cin >> num3 ;
        final = num1 - num2 + num3;
        cout << final ;
        cout << endl ;
        cout << "You just subtracted "; cout << num1; cout << " by "; cout << num2; cout << " and added that to "; cout << num3;
        break ;
            case '7':
        cout << "Please enter a number! \n" ;
        cin >> num1 ;
        cout << "Please enter the next number! \n" ;
        cin >> num2 ;
        cout << "Please enter the final number! \n" ;
        cin >> num3 ;
        final = num1 - num2 * num3;
        cout << final ;
        cout << endl ;
        cout << "You just subtracted "; cout << num1; cout << " by "; cout << num2; cout << " and multiplied that by "; cout << num3;
        break ;
            case '8':
        cout << "Please enter a number! \n" ;
        cin >> num1 ;
        cout << "Please enter the next number! \n" ;
        cin >> num2 ;
        cout << "Please enter the final number! \n" ;
        cin >> num3 ;
        final = num1 - num2 / num3;
        cout << final ;
        cout << endl ;
        cout << "You just subtracted "; cout << num1; cout << " by "; cout << num2; cout << " and divided that by "; cout << num3;
        break ;
            case '9':
        cout << "Please enter a number! \n" ;
        cin >> num1 ;
        cout << "Please enter the next number! \n" ;
        cin >> num2 ;
        cout << "Please enter the final number! \n" ;
        cin >> num3 ;
        final = num1 * num2 * num3;
        cout << final ;
        cout << endl ;
        cout << "You just multipled "; cout << num1; cout << " by "; cout << num2; cout << " and multiplied that by "; cout << num3;
        break ;
            case '10':
        cout << "Please enter a number! \n" ;
        cin >> num1 ;
        cout << "Please enter the next number! \n" ;
        cin >> num2 ;
        cout << "Please enter the final number! \n" ;
        cin >> num3 ;
        final = num1 * num2 + num3;
        cout << final ;
        cout << endl ;
        cout << "You just multiplied "; cout << num1; cout << " by "; cout << num2; cout << " and added that to "; cout << num3;
        break ;
            case '11':
        cout << "Please enter a number! \n" ;
        cin >> num1 ;
        cout << "Please enter the next number! \n" ;
        cin >> num2 ;
        cout << "Please enter the final number! \n" ;
        cin >> num3 ;
        final = num1 * num2 - num3;
        cout << final ;
        cout << endl ;
        cout << "You just multiplied "; cout << num1; cout << " by "; cout << num2; cout << " and subtracted that by "; cout << num3;
        break ;
            case '12':
        cout << "Please enter a number! \n" ;
        cin >> num1 ;
        cout << "Please enter the next number! \n" ;
        cin >> num2 ;
        cout << "Please enter the final number! \n" ;
        cin >> num3 ;
        final = num1 * num2 / num3;
        cout << final ;
        cout << endl ;
        cout << "You just multiplied "; cout << num1; cout << " by "; cout << num2; cout << " and divided that by "; cout << num3;
        break ;
            case '13':
        cout << "Please enter a number! \n" ;
        cin >> num1 ;
        cout << "Please enter the next number! \n" ;
        cin >> num2 ;
        cout << "Please enter the final number! \n" ;
        cin >> num3 ;
        final = num1 / num2 / num3;
        cout << final ;
        cout << endl ;
        cout << "You just divided "; cout << num1; cout << " by "; cout << num2; cout << " and divided that by "; cout << num3;
        break ;
            case '14':
        cout << "Please enter a number! \n" ;
        cin >> num1 ;
        cout << "Please enter the next number! \n" ;
        cin >> num2 ;
        cout << "Please enter the final number! \n" ;
        cin >> num3 ;
        final = num1 / num2 + num3;
        cout << final ;
        cout << endl ;
        cout << "You just divided "; cout << num1; cout << " by "; cout << num2; cout << " and added that to "; cout << num3;
        break ;
            case '15':
        cout << "Please enter a number! \n" ;
        cin >> num1 ;
        cout << "Please enter the next number! \n" ;
        cin >> num2 ;
        cout << "Please enter the final number! \n" ;
        cin >> num3 ;
        final = num1 / num2 - num3;
        cout << final ;
        cout << endl ;
        cout << "You just divided "; cout << num1; cout << " by "; cout << num2; cout << " and subtracted that by "; cout << num3;
        break ;
            case '16':
        cout << "Please enter a number! \n" ;
        cin >> num1 ;
        cout << "Please enter the next number! \n" ;
        cin >> num2 ;
        cout << "Please enter the final number! \n" ;
        cin >> num3 ;
        final = num1 / num2 * num3;
        cout << final ;
        cout << endl ;
        cout << "You just divided "; cout << num1; cout << " by "; cout << num2; cout << " and multiplied that by "; cout << num3;
        break ;

    default:
        cout << "This is not a valid option... Please try again." ;
    } 
        int exit ;
        cin >> exit ;
        exit = 999 ;
            if(exit == 999)
                std::exit(EXIT_FAILURE);
}
return 0 ;
    
}
JacobP is offline   Reply With Quote
Old 06-22-2011, 09:11 AM   #14
7D8
In Runtime
 
7D8's Avatar
 
Join Date: Oct 2006
Posts: 209
Default Re: I need C++ help.

it looks like you are stuck in your do/while loop. Try to read out what you are saying:

Code:
//while final is less than a string that is 1 OR final is greater than a string that is 16 AND final is not equal to lowercase string a
I don't follow the logic. Furthermore, omit the single quotes in the while clause. The compiler may be allowing you to do this, but this doesn't mean that it will work.

I assume your calculator only goes up to 16, based on the switch statement?

What are you trying to do, explicitly. I know you are trying to build a calculator... but what is this code trying to do?

PS: Clean up your code. If you are looking for help, don't force me to read badly formatted code:

Code:
    // THIS IS BAD

    for (;;) {
    do{
    cout << "This is my calculator. It follows the BEDMAS Rules. \n" ;
    cout << endl ;
    cout << "Enter two or three numbers. \n" ;
    cout << "Use * to multiply. \n" ;
    cout << "Use + to add. \n" ;
    cout << "Use - to subtract. \n" ;
    cout << "Use / to divide. \n" ;
    cout << "Enter 999 to exit the program. \n";
    cin >> final ;
    } while(final < '1'|| final > '16' && final != 'a');

    if(final == 'a') break ;
    switch (final) {

    case '1':
        cout << "Please enter a number! \n" ;
        cin >> num1 ;
        cout << "Please enter the next number! \n" ;
Code:
    // THIS IS BETTER

    for (;;)
    {
        do{
        cout << "This is my calculator. It follows the BEDMAS Rules. \n" ;
        cout << endl ;
        cout << "Enter two or three numbers. \n" ;
        cout << "Use * to multiply. \n" ;
        cout << "Use + to add. \n" ;
        cout << "Use - to subtract. \n" ;
        cout << "Use / to divide. \n" ;
        cout << "Enter 999 to exit the program. \n";
        cin >> final ;
    }
    while(final < '1'|| final > '16' && final != 'a');

        if(final == 'a') break ;
        switch (final) {

        case '1':
            cout << "Please enter a number! \n" ;
            cin >> num1 ;
            cout << "Please enter the next number! \n" ;
.....
7D8 is offline   Reply With Quote
Old 06-22-2011, 10:34 AM   #15
Baseband Member
 
Join Date: Jun 2011
Posts: 30
Default Re: I need C++ help.

What would be an exit command during a loop? Would I have to place it outside the loop?
JacobP is offline   Reply With Quote
Old 06-22-2011, 10:50 AM   #16
7D8
In Runtime
 
7D8's Avatar
 
Join Date: Oct 2006
Posts: 209
Default Re: I need C++ help.

Quote:
Originally Posted by JacobP View Post
What would be an exit command during a loop? Would I have to place it outside the loop?
well first of all, a loop such as a for loop you would give it conditions to start with.

Code:
// EXAMPLE 1
// here we have an infinite loop. I wouldn't recommend using this as it may get confusing for others. It is basically a loop with no condition, which means it will never stop unless you add a condition inside (i.e., Example 2)

for(;;)
{
    // infinite loop
}
Code:
// EXAMPLE 2
// to set a condition for an infinite loop, you would do something like this:

int i(0);

for (;;)
{
    // check     
    if (i == 5)
    {
        break; // exit loop
    }

    i++; // increment counter
}
Code:
// EXAMPLE 3
// the better way of writing a for loop

int i;

for (i = 0; i < 5; i++)
{
    // add logic here
}
now look how clean Example 3 is. Lots of the code is written for you. If you look closely, you can see that the for loop takes 3 arguments.

using the break keyword, you can break out of the loop, alternatively, using the continue keyword, you can continue through the loop. For example:

Code:
int i = 0;

while (i < 10)
{
    if (i == 5)
    {
        break; // this will exit the loop if i is equal to 5
    }
    else
    {
        continue; // not necessary
    }

    i++; // increment the counter
}
7D8 is offline   Reply With Quote
Old 06-22-2011, 12:22 PM   #17
Fully Optimized
 
strollin's Avatar
 
Join Date: Mar 2011
Location: USA
Posts: 1,938
Default Re: I need C++ help.

A For loop is mostly used when you have a specific number of times you know you want to iterate the loop. A Do/While loop is used when you know you want to go thru the loop at least once but don't know how many other times after that. A While loop is similar to a Do/While but it may never get executed.

Here's a pretty good tutorial on the various loops: Loops

In your code, you are mixing numbers and chars, you need to treat them differently. Notice below how the input of which function the user wants is char but the numbers entered are double. BTW, it's not very useful for a calculator to only deal with integer numbers.

Code:
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <math.h>

using namespace std;

int main()
{
    double num1, num2, final;
    char choice;
    bool usrQuit=0;

    do {
        cout << "This is my calculator. It follows the BEDMAS Rules. \n" ;
        cout << endl ;
        
        cout << "Use * to multiply. \n" ;
        cout << "Use + to add. \n" ;
        cout << "Use - to subtract. \n" ;
        cout << "Use / to divide. \n" ;
        cout << "Enter q to exit the program. \n";
        cin >> choice;

        switch (choice) {

        case '+':
            cout << "Please enter a number! \n" ;
            cin >> num1 ;
            cout << "Please enter the 2nd number! \n" ;
            cin >> num2 ;
            
            final = num1 + num2;
            cout << final ;
            cout << endl ;
            cout << "You just added " << num1 << " to " << num2;
            break ;
        case '-':
            cout << "Please enter a number! \n" ;
            cin >> num1 ;
            cout << "Please enter the number to subtract. \n" ;
            cin >> num2 ;
            
            final = num1 - num2;
            cout << final ;
            cout << endl ;
            cout << "You just subtracted " << num2 <<  " from " << num1;
            break ;
        case '*':
            cout << "Please enter a number! \n" ;
            cin >> num1 ;
            cout << "Please enter the next number! \n" ;
            cin >> num2 ;
            
            final = num1 * num2;
            cout << final ;
            cout << endl ;
            cout << "You just multiplied " << num1 << " by " << num2; 
            break ;
        case '/':
            cout << "Please enter a number! \n" ;
            cin >> num1 ;
            cout << "Please enter the next number! \n" ;
            cin >> num2 ;
            
            final = num1 / num2;
            cout << final ;
            cout << endl ;
            cout << "You just divided " << num1 << " by " << num2; 
            break ;
        case 'q':
            usrQuit = 1;
            break;
        default:
            cout << "This is not a valid option... Please try again." ;
        } 

        cout << endl;

    } while (!usrQuit);

    return 0 ;
}
This compiles, runs and pretty much does what you want, but it has problems. For instance, what if someone enters a 0 (zero) for the second number while dividing? When you run this program, notice how user unfriendly it is since to enter + or * requires the user to use the shift key (unless they use the numeric keypad). Maybe instead of +, - , *, / you could use something like:

Quote:
cout << "Use 1 to multiply. \n" ;
cout << "Use 2 to add. \n" ;
cout << "Use 3 to subtract. \n" ;
cout << "Use 4 to divide. \n" ;
cout << "Enter 99 to exit the program. \n";
cin >> choice;
Notice that I left out the part about entering 2 or 3 numbers. You would need to add a bunch of logic to detect and account for whether the user entered 2 numbers or 3 numbers.
__________________

strollin is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



All times are GMT -5. The time now is 07:26 PM.


Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0