Ok, so I have the general guts of the code figured out.(Or so I think.)
I have to have a calculator that displays the exponent or a number. However, the result cannot exceed a certain number. I have tired using while loops and if else loops. When you run the program it only displays the if statement no matter the numbers.
#include <iostream>
#include<iomanip>
usingnamespace std;
int main(int argc, char** argv)
{
//Integer declarations
int x = 1;
int y = 1;
int result = x;
//
for(int i = 1; i <= y; i++ ) {
result = result * x;
}
//User Input
cout << "Enter your base:";
cin >> x;
cout << "Enter the number to raise base by:";
cin >> y;
//
//Loop
if(result >= result){
cout << "ERROR!!! Can not calculate" << endl;
}
elseif(result == result){
cout << "The answer is:" << endl;
cout << result << endl;
}
return 0 ;
}
Ok, that makes perfect sense. So I guess I'm just having a hard time wrapping my head around how to define two different outputs from on formula. I'm sorry if these are really basic solutions I'm missing, as I am just starting out with c++.
The OP wants to get practice writing his own version.
@lolwut331
A greater than condition won't work if the number is bigger than what it's type will hold, it will just overflow. Try Google c++ detect overflow I get the feeling that the results may be a bit much at this stage, but nevermind have a go !
Use a larger type, std::size_t which is the largest unsigned type the system has, typically 264-1 on a 64 bit system. There is also longint 64bit or longlongint 32bit if you want negative numbers.
There are a lot of things to look out for:
zero to power zero.
zero as the base number, will result in zero - need to shortcut this.
Exactly. I'm trying to avoid using POW. Yea, I'm gonna need a few study sessions to wrap my head around the whole concept. I am at the extremely beginner level of c++.
So, the goal of the calculator is to calculate the maximum value that can be stored in 31 bits using this formula 2^31 - 1 = 2,147,483,647\/10
Am I going in the right direction with my code, or scar and start over?
Let me also say, I'm not looking for the answer. Just some serious help lol.
Ok, so I hit the books again and I came up with this. It debugs fine. It will run, it just doesn't display any output. So I feel like I'm back to square one.
#include <iostream>
usingnamespace std;
int main(int argc, char** argv) {
int x, y, prod =1;
cout<<"Please enter the values of x and y ";
cin>>x>>y;
while (y < 1)
{
prod *= x;
y--;
if (y < 31)
{
cout << "Calculation Not Possible!" << endl;
}
else
{
prod = y > 31;
cout << "The answer is:" << prod << endl;
}
}
return 0;
}
Your while-loop condition is pretty weird. Unless you enter 0 or a negative value for y, it will never be entered, meaning the program will just end. Which means, your first if statement will always be true, because 0 and all negative values are less than 31.
prod = y > 31;
Can you tell us with your own words what you think this does? Because it does not do what you think it does.
Keep in mind i havent read the other posts, only the post before this response, so if I missed something, my apologizes.
Oh I'm sure it is weird. I'm just now learning loops.
Honestly, the prod = y > 31; was an attempt at a "formula" for the correct answer.(Sounds better in my head.Dumb, but better.)
You should consider changing your variable names to something meaningful, like, instead of x and y use base and exp. It's much easier to read that way.
For your while loop on line 14, it's probably more appropriate to use a for loop here but I'll show you how to do it with both.
1 2 3 4 5 6 7 8
while (y > 1) //you probably want to exit when y is less than 1, not greater. so use greater than operator
{
prod *= x;
y--;
//I have no idea what you're trying to do with the if else statement here
//so I omitted it for clarity
}
Now if you wanted to use a for loop here, which you probably should since decrementing y for the while loop changes the value of y, which you probably don't want since you try to use it again later but it would be 0 at the end of the while loop.
#include <iostream>
int main(){
double base = 0;
double old = 0; //need to keep original base.
int raisedBy = 0;
std::cout << "Enter your number: ";
std::cin >> base;
old = base; //saved base
std::cout << "Enter the power: ";
std::cin >> raisedBy;
for (int i=0; i < raisedBy; i++)
base *= old;
std::cout << base << " Is your new number.";
}
I never tested this code.
With this code in mind, complete your task.
@zarman and @pnoid
After messing with this project since last Friday, I think I finally have the loop concept down-ish.
The if else statement is to notify the user if the number cannot be calculated due to it exceeding( 2147483648) or showing the answer if it is less than said number.
I've given it another attempt and it displays the error or correct messages, just doesn't do the math. Here's the code:
#include <iostream>
usingnamespace std;
int main(int argc, char** argv) {
int base =1;
int exponent =1;
int result = base;
cout << "Enter your base:";
cin >> base;
cout << "Enter the power to raise it by:";
cin >> exponent;
while (exponent < 1)
{
result *= base;
exponent--;
}
{
if (exponent < 31)
{
cout << "The answer is: " << endl;
cout << result;
}
elseif (exponent >= 31)
{
cout << "Error!! Can not calculate." << endl;
}
}
return 0;
}
Simple problem. You didn't make result equal to base AFTER base is input, leaving you to for example say if you want to find 2^3, your while loop won't even do the number, because 3 is not less than one. Also if the loop worked, it would start with result *=base, or 1 = 1*2, leaving your result to be 4, when it's supposed to be 8. I don't know if you understand what I mean, but see the code I made above.
#include <iostream>
usingnamespace std;
/*
*
*/
int main(int argc, char** argv) {
int base = 1;
int exponent = 1;
int result = base;
cout << "Enter your base:";
cin >> base;
cout << "Enter the power to raise it by:";
cin >> exponent;
for (int i=1; i <= exponent; i++)
result = result * base;
{
if (exponent < 31)
{
cout << "The answer is: " << endl;
cout << result;
}
elseif (exponent >= 31)
{
cout << "Error!! Can not calculate." << endl;
}
}
return 0;
}
Results:
Enter your base:2
Enter the power to raise it by:6
The answer is:
64
RUN SUCCESSFUL (total time: 3s)
And
Enter your base:2
Enter the power to raise it by:31
Error! Can not calculate.
RUN SUCCESSFUL (total time: 1s)
Your program works when base is 2, but what if the user put in something higher than that?
Consider calculating the (log of the maximum / log of the base) and use that to determine if the power is too high.
Edit 1:
As mentioned earlier, it's a simple test to see if the power is negative, you need to disallow that. I was trying to get you to use an unsignedint type to help with that.
Edit 2:
You do all of your tests after you calculate your result, better to do them beforehand :+)
Edit 3:
The braces on line 22 and 33 are ineffective, it looked like you intended for that code to be part of the for loop, but as I say, it should all go before the for loop. Things like this are easier to see if you have correct indentation.
Always use braces, even when there is only one statement in a loop or if , or whatever. It will save you one day when you add more code.