hey all;

is there a conversion from "char" to "int" or to

"double" possible ? If yes, how? and if not, how do

we work with numbers that have more than 32 digits, for instance

a 500 digit number?

thanks

0

hey all;

is there a conversion from "char" to "int" or to

"double" possible ? If yes, how? and if not, how do

we work with numbers that have more than 32 digits, for instance

a 500 digit number?

thanks

0

C++ places hard limits on the size of the built in types. If you want something larger you need to use a library that supports big numbers, like GMP.

0

thanks

but how do u think u'd try to answer this problem:

**Problem code: ARITH
One part of the new WAP portal is also a calculator computing expressions with very long numbers.
To make the output look better, the result is formated the same way as is it usually used with manual
calculations.
Your task is to write the core part of this calculator. Given two numbers and the requested operation,
you are to compute the result and print it in the form specified below. With addition and subtraction,
the numbers are written below each other. Multiplication is a little bit more complex: first of all, we
make a partial result for every digit of one of the numbers, and then sum the results together.
Input
There is a single positive integer T on the first line of input (equal to about 1000). It stands for the
number of expressions to follow. Each expression consists of a single line containing a positive integer
number, an operator (one of +, - and *) and the second positive integer number. Every number has at
most 500 digits. There are no spaces on the line. If the operation is subtraction, the second number is
always lower than the first one. No number will begin with zero. **

0

>but how do u think u'd try to answer this problem:

By first, trying to do it yourself...

0

By the looks of it, you're supposed to manually manage the numbers. Edward's first attempt would probably use strings to store the numbers and then handle the arithmetic just like it's taught in elementary school with digit by digit calculations and saving of the carry or borrow values.

That's the simple and easy to write solution, but it's not optimal in terms of speed or memory footprint.

0

>>is there a conversion from "char" to "int"? If yes, how?

Yes, you can either go from char to int or from string to int.

One of the easiest way to get the numeric value of a char representing a digit is to substract the ASCII (or Unicode or whatever) value of the of zero from the corresponding value of the desired digit. This, of course, relies on the facts that a char is just an int in disguise, the applicable int value for a given char being encoded in the applicable conversion table--- ASCII, Unicode, whatever; and that the char values for numerical digits are always arranged in sequential, contiguous order within the table, meaning that the value for the char '0' is always 1 less than the value of '1' which is always 1 less than the value of '2', etc.

To convert an entire string to a numerical value you hve a variety of opitons including stringstreams, one of the strtox() functions such as strtol() or one of the atox() functions, such as atoi(), atol(), etc, in addition to others. As long as the resulting numerical value is within the acceptable range of values for C/C++ any of these options will work, though some are prefered to others.

0

One of your options is the stringstream family of classes. It's not the easiest way in Edward's opinion, but it's probably the best with standard C++:

```
#include <iostream>
#include <sstream>
int ToInt(const char *s)
{
std::istringstream is(s);
int result;
is >> result;
return result;
}
int main()
{
int x = ToInt("2");
std::cout << x * x << '\n';
}
```

The good thing about this is that it can be generalized to work with other types so you don't have a ton of overloads each doing the same thing but with a different type.

0

itoa is non standard and atoi is a much maligned function. However, the use of atoi is not an issue (since this is c++) you should just use stringstreams.

0

Some remarks:

The C library is a standard part of the C++ library.

In particular, the atoi() function is declared in <cstdlib> C++ library header.

0

can u plz tell me how many parametres this atoi() function takes?

how do u use it?

0

This is how atoi() is used, basically.

```
#include <cstdlib>
#include <iostream>
int main()
{
using namespace std;
int a = atoi("10");
cout << a << " squared is " << a * a << '\n';
}
```

The basic use isn't enough unless you can guarantee that the string represents both an integer value and an integer value that fits in the range of int. If you can't guarantee that, atoi() doesn't do a good job of handling those kinds of errors.

One problem with atoi() is that it returns 0 if an error occurs. 0 is a valid integer so unless you want to ban 0 in your program, you can't reliably use it as an error indicator.

Another problem is that if the number can't be represented by int, the behavior of atoi() is undefined and there's no way around it without pre-validating the string.

The end result is that a robust use of atoi() looks more like this:

```
int main()
{
using namespace std;
char *s;
// Initialize s and fill it with a string
if (IsInt(s)) {
int a = atoi(s);
cout << a << '\n';
}
}
```

Where IsInt() is a function you write that validates the string to make sure the represented value fits in an int. The other ways of converting a char* to int may be more verbose, but they're also more user friendly when it comes to error handling.

Edward recommends that you only use atoi() when there's a 100% guarantee that the string represents a valid int and no error handling will ever be necessary.

0

thanks alot for all the replies

but i'm still not convinced that even with this function i'd be able to work with high digit numbers!

i guess it's just the c++ weakness in this case!

1

By the looks of it, you're supposed to manually manage the numbers. Edward's first attempt would probably use strings to store the numbers and then handle the arithmetic just like it's taught in elementary school with digit by digit calculations and saving of the carry or borrow values.

That's the simple and easy to write solution, but it's not optimal in terms of speed or memory footprint.

Do as what Edward has said and you will able to solve your problem easily. You don't actually need integer to store number, you can just use string to represent the number. First of all, you create one string to store the input which is in the format you have mentioned before:

`first_number[+ or -]second_number`

Then create 2 other strings: the first string stores the first number, and the second string stores the second number. Then calculate it like what you have taught in your elementary school.

For example:

```
[B]User's Input: 162823+562369[/B]
-> First String = 162823
-> Second String = 562369
Calculate it manaully like you would calculate by hand.
1 1 1
1 6 2 8 2 3
+
5 6 2 3 6 9
---------------
7 2 5 1 9 2
```

This topic has been dead for over six months. Start a new discussion instead.

Recommended Topics

I have been trying to figure this out for hours with no avail. Basically I need to print out user input as Initial value, ASCII(char), ASCII(int), and Hex. I ...

- Your program should read the input files by using Unix shell redirection (e.g. a.out<lab1.dat) to read the input from

stdin (C). By using redirection, it is unnecessary to open ...

How to connect device scanner with visual basic, its analogy I made button scan in vb and typing button scan in press scanner live and walk .. how? Help ...