c++

You have to test the condition. hmm i cheated on my but there is a function you can call that test if its a char or a digit : e.x. if (isdigit(digit))

i can look it up later if you like my note are at home.
 
The stream can be checked to see if its still in a good state after atempting to exract input, either by checking std::cin.good() or the return value from the exraction operator. If the stream is bad it means the parsing failed and the stream will need to be cleared and flushed before using again.

e.g.

Code:
#include <iostream>
#include <limits>

int _tmain(int argc, _TCHAR* argv[])
{
	int i;
	bool badInput = true;

	do
	{
		std::cout << "Please input a number" << std::endl;
		if( !(std::cin >> i) )
		{
			std::cout << "Bad input" << std::endl;
			std::cin.clear();
			std::cin.ignore( std::numeric_limits<int>::max(),'\n' );
		}
		else badInput = false;
	}
	while( badInput );

	std::cout << "Number was : " << i << std::endl;

	return 0;
}
 
You haven't broken it, it simply rounded the fraction because the number was too big.
I finished adding the final functions, it can now add, multiply, divide and raise fractions to a power for any integer value +- 2^32 -1
 
Last edited:
The fraction entered by Ace Modder was too big as it can only handle up to 9 figures, then it goes mad. I just discovered this __int64 which should improve things, EDIT cin doesn't like it now. What is the most efficient way to compute really big numbers, say 512-bits or more?
 
Last edited:
PanMaster said:
The fraction entered by Ace Modder was too big as it can only handle up to 9 figures, then it goes mad. I just discovered this __int64 which should improve things, EDIT cin doesn't like it now. What is the most efficient way to compute really big numbers, say 512-bits or more?

I used a structure of multiple longs with overidden operators the lst time I had do do it (56 digit keys in oracle designer :eek: ) or store them as strings and do the math yourself.

Why do you need to deal with such long input? Just throw and exception and handle the error nicely to the user, Input argument too long, please enter a number between x and y.

HT
 
I heard it was originally intended as a joke as ++ means an increment of 1 in the language. C++ is an object orientated programming language which allows efficient, but not perfect, program code to be compiled in a fraction of the time it would take using machine code.
 
PanMaster said:
The program now uses 64-bit variables and the HCF of 184512469581365169415 / 1541265 is 5 to give 9005768853930651 / 308253 which is still wrong as it only allows 19 figures :o

As stated above, you should write a class (or use one of the many available online, though writing your own would be a good learning exercise), to represent arbitrarily long numbers.

As a first iteration, why not just store it as a string?

Implement the various operators, and then you can use it as a simple drop-in replacement for types such as __int64.....
 
I keep stumbling because I can't get the string that is entered in a useful form.
int A[77];
char string[77];
for(int i=0; i<length_of_the_string; i++)
A[length_of_the_string-1-i] = string;
No matter what I do it refuses to store the numbers in the string in my array called A and when I check what is in A it reads garbage.
Even if I get an array there is still lots of complicated algebraic calculations I must perform before even the simplest arithmetic operations can be done.
 
PanMaster said:
I keep stumbling because I can't get the string that is entered in a useful form.
int A[77];
char string[77];
for(int i=0; i<length_of_the_string; i++)
A[length_of_the_string-1-i] = string;
No matter what I do it refuses to store the numbers in the string in my array called A and when I check what is in A it reads garbage.
Even if I get an array there is still lots of complicated algebraic calculations I must perform before even the simplest arithmetic operations can be done.


I'm not sure what you're trying to do there. Why not use std::string, or at the very least std::vector<char>?
 
and there is your problem, you need to convert the characters in the string to integers in the array A. the std: types visage is talking about are the standard C++ library. One of the ways to convert ASCII chars to ints is to cast them to an unsigned int and take away a number (34 I think) this should give you the corerect int value.

There's probably a handy int char_to_int(char) type of function somewhere in the std: library.

HT
 
PanMaster said:
Thank you for the idea. It isn't easy. I've tried using atoi and similar things but they're basically useless. Fortunately conversion isn't necessary and the magic number is 48 no 34.

A better way of doing it is to use the streaming operators:

std::string s("1234") ;
int i;

std::stringstream str ;
str << s ; //Insert the string into the stream
s >> i ;// output the stream to the int.

i now contains 1234.
 
On second thoughts, there's actually no need to store the digits as chars. You could just use a vector of chars (8 bit ints), and not need to do any conversion.

You'd be far better off using one of the pre-written libraries available though. Its very easy to throw together a rough and ready library, but there are a lot of gotchas involved.

Take a look at:

http://www.swox.com/gmp/
 
I'm now creating a class where there are two functions. One creates a large integer, the other adds two large integers. How do I get the answer (which is an 256 integer array) outside the function? Before I used to use the & symbol but it won't work with arrays.
The large integers are stored as a long array of signed single numbers of increasing powers of ten, the first number is the length of the array, thus the arrays are around 1Kb in size.
 
Back
Top Bottom