Click here to Skip to main content
14,975,411 members
Please Sign up or sign in to vote.
3.55/5 (6 votes)
See more:
Hi everyone,

I am new to this site and to coding. Well, I am not completely new, as I have experimented with a bit of Excel VBA and HTML and CSS. I am just new to C++.

I am teaching myself how to code in C++ using C++ Programming for the Absolute Beginner, 2nd Edition.

Although I have done one of the Chapter 1 challenges, I notice that the program is imperfect.

C++
/* Write a program that asks users for their names, that greets them, 
and that asks them for 2 numbers and then provides the sum. */

#include <iostream>
#include <string>

using namespace std;

int main(void)

{
	using std::cout;
	using std::cin;
	using std::string;
	string name = "";
	short firstNumber, secondNumber, Sum;
	//ask for name
	cout << "What is your name?\n";
	cin  >> name;
	//greet user
	cout << "\nHello " << name.c_str() << "\n";
	cout << "I am a smart computer. Give me 2 numbers, and I will add ";
	cout << "them together.\n";
	//ask 2 numbers
	cout << "Input two numbers to add. \n";
	cin  >> firstNumber >> secondNumber;
	Sum = firstNumber + secondNumber;
	cout << "Your sum is: " << Sum << "\n"; 
	return 0;
}


What I have tried:

As a beginner who has just read the chapter on variables, I know that I used the short datatype, which can only store a limited amount of integers. Long datatype also can only store a limited amount of integers. Is there a possible datatype that can store an infinitely big number or just a very, very, very big number like 100,000,000,000,000,000,000,000,000,000 or 10^29 or one hundred octillion?
Posted
Updated 14-May-16 5:52am
v3

Use Google and see if BigIntegers or BigNum fit your needs.
BigIntehers can be integrated in your compiler libraries depending on how old it is.


The GNU MP Bignum Library[^]
   
v2
Comments
CPallini 14-May-16 9:00am
   
5.
Patrice T 14-May-16 9:08am
   
Thank you
Sergey Alexandrovich Kryukov 14-May-16 11:38am
   
5ed.
—SA
Patrice T 14-May-16 13:42pm
   
Thank you
I don't know what is "large variable", forget "infinitely large variable". Any computer is a finite-state machine. Does it tell you something? It computing, there is a concept of "infinity", but it's an abstraction, more exactly, a number of abstractions.

Also, the size of data is not the same as size of a variable.

Now, you should see the big difference between two related concepts. One thing is "very large number". Such numbers can be represented using floating-point number. First thing to understand is: representation of a really big number can carry pretty little amount of information. Isn't it a big number: 999999999? :-) But it carries very modest amount of information. See also:
Floating point — Wikipedia, the free encyclopedia[^],
IEEE floating point — Wikipedia, the free encyclopedia[^].

Another thing is the number holding big volume of information, ideally, of the arbitrary size limited only by available computer memory. For simplicity, let's set aside such complicated thing as representation of arbitrary-size real numbers. Let's consider arbitrary integer number, represented precisely, without any approximation. Apparently, such numbers can be represented as a sequence of bits of arbitrary size, limited only by available memory. The usual name for such types is "big integer", or, for a type name, BigInt, BigInteger. Some of such types are really of unlimited size, but in C++ it's more usual to develop much weaker type where the maximum size is limited at the moment of object initialization.

The development of such classes is not even extremely difficult. Basically, you just mimic the operations learned in elementary schools arithmetic. Only I would not advice using strings to represent the sequence of 0..9 digits; such representation is utterly inefficient. It should be sequence of bits. You can do your search and find a number of available implementations (of different quality :-)).

—SA
   
v2
Comments
Member KL 14-May-16 0:47am
   
No fair. Your username is pronounceable. Mine is just a set of numbers, as if the forum doesn't even bother giving me a natural-language name. :(
Richard MacCutchan 14-May-16 3:54am
   
You can change your username quite easily.
CPallini 14-May-16 9:00am
   
5.
   
Thank you, Carlo.
—SA
Patrice T 14-May-16 13:49pm
   
a 5 for you too :-)
Sergey Alexandrovich Kryukov 14-May-16 14:31pm
   
Thank you very much.
—SA
Matt T Heffron 16-May-16 17:25pm
   
+5
Sergey Alexandrovich Kryukov 16-May-16 18:39pm
   
Thank you, Matt.
—SA
Prava-MFS 17-May-16 11:47am
   
Nice answer +5 :).
Sergey Alexandrovich Kryukov 17-May-16 12:51pm
   
Thank you very much.
—SA
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900