PDA

View Full Version : C++ Help

avaris
02-17-2007, 04:29 PM
I'm doin an independant study in C++ this semester...well as my professor told me that literally means independant study.

I got a program due tuesday and there are three small pieces of the program I can't figure out the syntax for.

1. I need to format a number to 4 decimal places.
2. I need to raise a number to a power. (do i need to write my own function for this?)
3. How to change a string to integer(variable[so many different names for it])

If anyone knows any of these it would be a great help. Although this is my first experience with C++ I do know Basic fairly well.

Risk
02-19-2007, 10:10 AM
1. To format a number to 4 decimal places, you'll need to include the iomanip library. Let's do an example:

double f = 10.198472;
cout << fixed << setprecision(4) << f;

What happens here is that when you display the variable f to output, it'll modify the output to include 4 decimal places. So you'll get: 10.1985.
Remember that this does NOT modify the variable itself! Only what is outputted onto the screen.

2. For this one, to what power do you need to raise the number to? We'll call this value "y". In order to raise the number to a power, you'll have to use a loop (unless there's some library out there that will compute the power but I haven't run across it yet).

For instance, let's give the variable y (the number we want to raise it to a power of) a value, and everything else values.

int number = 2;
int y = 4;
int i = 1;
int value = number;

while (i < y)
{
value = value * number;
i++;
}

So, when the code executes, it will be 2^4.
The 1st time, you'll have 2 * 2 = 4. (2^2)
The 2nd time, you'll have 4 * 2 = 8. (2^3)
The 3rd time, you'll have 8 * 2 = 16. (2^4)

2^1 will never execute because 1 is not less than 1. So if y was 2, the code would execute only once to give you a value of 4.

3. I'm not entirely sure about this but I think it had to do with using static<cast>.

Hope this helps!

avaris
02-19-2007, 01:45 PM
Holy shite thanks dude! I can't believe I didn't figure out the loop thing...shame on me. The logic in it seems perfectly fine so I'm sure it'll work. I was not looking forward to searching through the internet all day to day to figure out these two things.

Oh cool first post, welcome to the community.

Kanthos
02-19-2007, 04:46 PM
Some versions of C++ have a pow(x, y) function that calculates x^y. If you're using the loop approach, make sure that you'll never have a power less than 1, decimal powers, or anything else like that.

avaris
02-19-2007, 05:21 PM
I actually have to code it so the user can enter in negative #'s and decimals. For some reason I keep on getting 2 errors saying the 'else' in the smaller if statements don't match anything. I've been looking over the code bout 2 hours and can't solve it. Those are the only two errors I'm getting.

if ( Base > 0 )
{
if (Power < 0 )
{
while (I > Power)
{
HoldPow = 1 / Power;
Value = HoldPow * Base;
I = I - 1;
}
else
while (I < Power)
{
Value = Value * Base;
I = I + 1;
}
}
else
if (Power < 0)
{
while (I > Power)
{
HoldPow = 1 / Power;
Value = HoldPow * Base;
I = I - 1;
}

else
while (I < Power)
{
Value = Value * Base;
I = I + 1;
}
}
cout << (Value)<< " IT WORKS!!!" << endl << endl;
double Value;
cout << fixed << setprecision(4) << Value;
}

I know it has something to do with the {}, but it looks perfectly fine. Of course the computer only does what it tells you to do, so I'm def missing something.

Logically it looks like I can account for negative numbers and decimals with this set-up.

I am using Visual C++.

suzumebachi
02-19-2007, 05:23 PM
Ahh man use code tags please.

avaris
02-19-2007, 05:26 PM
Ahh man use code tags please.

Haha yeah...right after I posted it was like wtf is this. :banghead: Thanks

Kanthos
02-19-2007, 05:37 PM
You're missing the } before the else, right after the else, and right before the cout line.

If statements use the following construct:

if (condition) {
dosomestuff
} else if (other condition) {
do some other stuff
} else {
do something different
}

Xerol Oplan
02-19-2007, 06:22 PM
Some versions of C++ have a pow(x, y) function that calculates x^y. If you're using the loop approach, make sure that you'll never have a power less than 1, decimal powers, or anything else like that.

It's in math.h.

And for #3, I think it's in string.h, there's an atoi() function that does that for you. Alternately you can write your own, consider that a char is just a 1-byte integer, that holds the integer value of the appropriate ascii character.