Line 20
cout << b * b * b "\n";
should be replaced with this:
cout << b * b * b << "\n";
No, you don't have to do that. It runs perfectly on Dev-C++.
None.
It worked fine the one way but then I tried it again from your version then added DC_BK's stuff, then the other fix and it didn't work, even when I went back to the original. Yes I'm using Dev C++ And how is 5
2 = 125? Isn't 5x5=25?? <.<
It should be:
Where "b" equals the number inputed on command.
None.
// While loop example
#include <iostream>
using namespace std;
int main ()
{
int a,b,c;
a = 1;
cout << "Go and type in the number 1" "\n";
cin>> c;
cin.ignore();
while (a==1)
if (c==1) {
cout << "Input a number to be cubed." "\n";
cin>> b;
cout << b * b << "\n";
cin.get();
cout << "Press 1 if you wish to go again" "\n";
cin>> c;
cin.get();
}
else if (c > 1, c < 1) {
return 0;
}
}
With this code, note I modified some of it, you have to press 1 then enter to then enter a number that you want to have cubed. It doesn't loop like it should. Either it doesn't work, or I am doing something wrong.
None.
I said a number cubed, not squared.
None.
// While loop example
#include <iostream>
using namespace std;
int main ()
{
int a,b,c;
a = 1;
cout << "Go" "\n";
cin>> c;
cin.ignore();
while (a==1)
if (c==1) {
cout << "Input a number to be cubed." "\n";
cin>> b;
cout << b * b * b; "\n";
cin.get();
cout << "Press 1 if you wish to go again" "\n";
cin>> c;
cin.get();
}
else if (c > 1, c < 1) {
return 0;
}
}
A quick program I made to demonstrate.
Fix'd a possible error in my hasty coding, try it again.
None.
Can you explain what does what in the code. Its more basic then I originally thought it would be to do calculator functions, or at least one of them. I would like to make a calculator that adds, subtracts, multiplies, and divides. I modified the code once more just to mess around with it because I wanted to see if it could find pi but it didn't do any decimal points. Is there some other code that is needed to do this? How do I make options? You have it so that when you press "1" and then "enter" you can then start and input the number that you want to be cubed. I would like it so that when you select on the the functions by "+, -, *, /" and then enter, it switches to that function. I think that this will be a simple first project based on the code you have provided.
None.
Can you explain what does what in the code. Its more basic then I originally thought it would be to do calculator functions, or at least one of them. I would like to make a calculator that adds, subtracts, multiplies, and divides. I modified the code once more just to mess around with it because I wanted to see if it could find pi but it didn't do any decimal points. Is there some other code that is needed to do this? How do I make options? You have it so that when you press "1" and then "enter" you can then start and input the number that you want to be cubed. I would like it so that when you select on the the functions by "+, -, *, /" and then enter, it switches to that function. I think that this will be a simple first project based on the code you have provided.
Post the code that you used for the pi thing? You probably made the variable the wrong type or something.
Also on options, if I understand you correctly you want to have an easy way to have different options on an input field. As far as I know there is no way to do this. You just have to process the input and decide which function to use or to return an error of some sort.
I can't really give you a specific c++ example, I use c mostly, but here is a general example:
// a = whatever input you had
// var3 = final answer
// var1 and var2 = number inputs
if(a == 1) // there may be something more you have to do here
var3 = var1 + var2;
else if(a ==2)
var3 = var1 - var2;
else
cout << "Invalid Input"; // this might be wrong, i don't use c++
None.
You can't use decimals with 'int". They're a different variable type called "
float" and I would suggest you not use them for now.
Here:
// While loop example
#include <iostream>
using namespace std;
int main ()
{
int b, c = 1;
cout << "Go" << endl;
while (true) //whatever is in the parenthesis must evaluate to true for this to continue looping. refer to boolean algebra.
if (c==1) //check for command value (value of c)
{
cout << "Input a number to be cubed." << endl;
cin >> b; //get b
cout << b * b * b << endl; //cube b
cout << "Press 1 if you wish to go again" << endl;
cin >> c; //get c
}
else if (c != 1) //quit if c is not equal to 1
{
return 0;
}
}
Post has been edited 3 time(s), last time on May 25 2008, 9:06 pm by cheeze.
None.
I want to do some of this on my own but with help here and there. I want to make a calculator that does whole numbers and that can be switched throughout the process by hitting either + - * - or ^. I know how to manually set up the equation but I don't know how to code it.
Addition " + "
cout << a + b ; "\n";
Subtraction " - "
cout << a - b ; "\n";
Multiplication " 8 "
cout << a 8 b ; "\n";
Division " / "
cout << a / b ; "\n";
Exponent " ^ "
I'm not sure how to set this up for every situation. I want it to be something like Enter A, then Enter B and what ever B is equal to that will be your exponent. EX: A = 3 B = 4 Equation 3 * 3 * 3 * 3 How would I increase the amount of times B is? Like if B = 1 then A = Answer. If B = 2 A * A = Answer. If B = 3 A * A * A = Answer. I'm pretty sure you don't have to make every possible equation for each outcome.
None.
For now, don't worry about exponents (they're a little trickier) and don't worry about using "+" for addition or "-" for subtraction. Just substitute with integers, see if those work. If it does, it's just a quick change to strings.
None.
Exponent " ^ "
I'm not sure how to set this up for every situation. I want it to be something like Enter A, then Enter B and what ever B is equal to that will be your exponent. EX: A = 3 B = 4 Equation 3 * 3 * 3 * 3 How would I increase the amount of times B is? Like if B = 1 then A = Answer. If B = 2 A * A = Answer. If B = 3 A * A * A = Answer. I'm pretty sure you don't have to make every possible equation for each outcome.
When it comes to stuff you don't know, and trust me there will always be stuff like this, you gotta start thinking with variables.
For instance
//int a = Number
//int b = Exponent
//well, why not use a while statement?
int i = 0;
int NewNumber = 1;//Anything to the zero power is 1, lets take care of that here
while(i != b)
{
NewNumber = NewNumber * a;
i++;
}
cout << NewNumber;
or a for statement to use less lines of code
int NewNumber = 1;
for(int i = 0; i != b; i++)
{
NewNumber = NewNumber * a;
i++;
}
cout << NewNumber;
None.
Yeah that happened to me a couple of times. I don't know any of the code I will just copy and modify what I already have to fit my needs. I need to set up some sort of menu. Something like a message that appears like: Select your operand by either selecting + - * / and hit enter.
None.
For the love of god, never use != as a control loop condition when a comparison operator will suffice. You're just begging for an accidental infinite loop.
Haha, you're right. I never remember though. Thankfully I compile & test often and try to debug for every scenario.
None.
How do I set it up for multiple equations? I want it to at least do addition, subtraction, multiplication, and division. I want it to be able to be selected either by typing it in, or by selecting one of the symbols. How do I set it up for it to recognize the new equation and then changing it to the one chosen? I would also like it to remove previous text after I have done one of the equations and then hit 1 again to loop the program? What is the largest number that you can input into this? I put in a ridiculous number and it repeated into infinity and never stopped.
None.
Well, you had one if statement for cubed.
To implement subtraction, just put another if statement in there that does something like a = b - c and print out a. Similarly, do this for addition, multiplication and division. Note that with integer division, it'll remove the decimal (that is, 4/3 = 1). Like I said, do not worry about exponents for now; make sure we can get the basic control flow going before implementing cool stuff.
Here's a simple example of if else statements:
if ([i]condition[/i])
{
//[i]perform action here[/i]
}
else if ([i]another condition[/i])
{
//[i]perform another action here[/i]
}
None.
cout << "Please Input A ( 1-10 )." << endl; = Display Text
cin >> a; //get a = Last inputed number
cout << a + b << endl; = Equation
How do I make it erase all of the text and start over? When the end of the equation occurs and you are asked again to select the next operand by pressing 1-4, how do I make this action loop when 1-4 is False? (the person did not hit 1-4) It locks up on me and then I have to restart the whole thing. It actually works nicely. This is way easier to make then Tuxlar's Binary Calculator Map... LULZ
Calculator Program
// While loop example
#include <iostream>
using namespace std;
int main ()
{
int a,b,c;
cout << "Please select an operand by pressing a number" << endl;
cout << "-----(1 = +) (2 = -) (3 = *) (4 = /)-----" << endl;
cin >> c; //get c
while (true) //whatever is in the parenthesis must evaluate to true for this to continue looping. refer to boolean algebra.
if (c==1) //check for command value (value of c) Addition
{
cout << "---( A + B = C )---" << endl;
cout << "Please Input A ( 1-10 )." << endl;
cin >> a; //get a
cout << "Please Input B ( 1-10)." << endl;
cin >> b; //get b
cout << a + b << endl; //add a + b
cout << "(1 = +) (2 = -) (3 = *) (4 = /)" << endl;
cin >> c; //get c
}
else if (c==2) //check for command value (value of c) Subtraction
{
cout << "---( A - B = C )---" << endl;
cout << "Please Input A ( 1-10 )." << endl;
cin >> a; //get a
cout << "Please Input B ( 1-10)." << endl;
cin >> b; //get b
cout << a - b << endl; //sub a - b
cout << "(1 = +) (2 = -) (3 = *) (4 = /)" << endl;
cin >> c; //get c
}
else if (c==3) //check for command value (value of c) Subtraction
{
cout << "---( A * B = C )---" << endl;
cout << "Please Input A ( 1-10 )." << endl;
cin >> a; //get a
cout << "Please Input B ( 1-10)." << endl;
cin >> b; //get b
cout << a * b << endl; //sub a - b
cout << "(1 = +) (2 = -) (3 = *) (4 = /)" << endl;
cin >> c; //get c
}
else if (c==4) //check for command value (value of c) Subtraction
{
cout << "---( A / B = C )---" << endl;
cout << "Please Input A ( 1-10 )." << endl;
cin >> a; //get a
cout << "Please Input B ( 1-10)." << endl;
cin >> b; //get b
cout << a / b << endl; //sub a - b
cout << "(1 = +) (2 = -) (3 = *) (4 = /)" << endl;
cin >> c; //get c
}
else if (c > 1, c < 1) {
return 0;
}
}
None.
Your comments are a bit wrong. Also, why can A or B only be 1 - 10 ?
None.