I need C++ help.

JacobP

Baseband Member
Messages
30
This is just the start of my calculator I'm trying to make but it's not turning out well. This is just the begening and I'm going to keep going on later but I used this script. If you want to try the output put it in Visual Studios(If you have it).

Please help me!

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

using namespace std;

int main()
{
    cout << "This is my calculator. It follows the BEDMAS Rules. \n" ;
    signed float number1 ;
    signed float number2 ;
    signed float number3 ;

    signed float finalequation ;

    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" ;
    cin.get();

    if(number1 + number2 + number3 = finalequation)
        cout << "You just added"; cout << number1; cout << " to "; cout << number2; cout << "to"; cout << number3; cout << "!";

    cin.get();
    return 0;
}
 
Your program as written won't work for several reasons. First of all, cin.get(); will only pull one character from the input buffer and you didn't assign it to any variable so it is being thrown away. You need to do something like cin >> number1; to get a number assigned to the variable number1, cin >> number2; to get a number into number2 and so forth. Your If test won't work as written either, for one thing to test equality you need to use ==, not =.

Her's a link to a sample C++ calculator that may give you some ideas for your own calculator program: C++ calculator - C++ - Source Code | DreamInCode.net
 
I changed this but it's still not working.
Code:
#include "stdafx.h"
#include <iostream>

using namespace std;

int main()
{
    cout << "This is my calculator. It follows the BEDMAS Rules. \n" ;
    int number1 ;
    int number2 ;
    int number3 ;

    int finalequation ;

    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" ;
    
    cin >> number1 + number2 + number3 = finalequation ;
    cout << "You just added"; cout << number1; cout << " to "; cout << number2; cout << "to"; cout << number3; cout << "!";

    return 0;
}
 
You need to break up the cin to multiple statements. I'd actually be surprised if it compiled as written. It should look something like this:
Code:
cin >> number1;
cin >> number2;
cin >> number3;
finalequation  = number1 + number2 + number3;
 
You need to break up the cin to multiple statements. I'd actually be surprised if it compiled as written. It should look something like this:
Code:
cin >> number1;
cin >> number2;
cin >> number3;
finalequation  = number1 + number2 + number3;

I see, thank you very much. +reped.

Also, does this have to be an if statement?
 
If you want to do anything else beside have the user enter 3 numbers which you add then display the result then youo most likely will want to use a switch statement. Did you look at the example calculator that I linked to in my post above?
 
Yea but I can't seem to do this correctly. Can you try and help me from where I am right now?
Code:
#include "stdafx.h"
#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
    cout << "This is my calculator. It follows the BEDMAS Rules. \n" ;
    int num1 ;
    int num2 ;
    int num3 ;

    int final ;

    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" ;
    
    cin >> final ;
    cin >> num1 ;
    cin >> num2 ;
    cin >> num3 ;
    while(final == num1 + num2 + num3) ;
    final = num1 + num2 + num3 ;
    cout << "You just added"; cout << num1; cout << " to "; cout << num2; cout << "to"; cout << num3; cout << "!";

    cin.get();
    return 0;
}
 
your while loop isn't looping through anything. Its basically saying:

while final equals num1 plus num2 plus num3 .... and then it terminates. (sorry, don't have a way to test the code right now)
 
Why don't you start by writing what is referred to a pseudo-code, a series of statements describing what you want the program to do before you starting writing the code? You need some kind of a road map to follow. If you were writing a paper you would probably write down an outline, same type of thing.
 
Back
Top Bottom