Jump to content

C++ Help


avaris
 Share

Recommended Posts

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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++.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...