Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Big integer help PLEASE!!!!

Status
Not open for further replies.

Nikki1081

Programmer
Jun 9, 2003
1
US
I have been working on this same problem for 3 weeks and I just dont get why my code will not work. I am trying to input an integer bigger than the Long int will allow but I keep getting an error that my subscript is not of integral type. Anyone have any ideas what I am doing?

//---------------------------------------------------------------------
// Assignment 1
// Course: CSC 202, Section 060A, Summer 2002
//
// Purpose: This program will act as in interactive calculator that will
// be capable of handling very large nonnegative numbers.
//
//---------------------------------------------------------------------
#include <iostream.h>
//functions for add or multiple
double Add (double firstNumber, double secondNumber)
{
cout << firstNumber << &quot; + &quot; << secondNumber << &quot; = &quot;;

return(firstNumber + secondNumber) ;
}

double Multiply (double firstNumber, double secondNumber)
{
cout << firstNumber << &quot; * &quot; << secondNumber << &quot; = &quot;;
return (firstNumber * secondNumber);
}

int main()
{
//variable declaration
double firstNumber1;
double secondNumber1;
int operand;
//Prompt for input of numbers
cout << &quot;This Calculator Adds and Multiplies&quot; << endl;
cout << &quot;Enter First Number:\n&quot;;
cin >>firstNumber1;
cout << &quot;\n&quot;;
double firstNumber=firstNumber1;
cout << &quot;Enter Second Number:\n&quot;;
cin>>secondNumber1;
cout << &quot;\n&quot;;
double secondNumber=secondNumber1;
//for loops that inializes and takes in long integers
double array1[100];
//set count equal to the size of the array;
//and let it count down to last element
int count1=sizeof(array1);

for(firstNumber=0; firstNumber < 99; firstNumber++)
{
cin >> firstNumber1;
//array1[firstN]= array1[firstNumber];
array1[firstNumber1];
firstNumber1=firstNumber1+1;
count1--;
}
double array2[100];
int count=sizeof(array2);
for (secondNumber=0; secondNumber < 100; secondNumber++)
{
cin>> secondNumber1;
//array2[secondNumber]=arrays[secondNumber]

array2[secondNumber1]; secondNumber1=secondNumber1+1;
count--;
}
//Promptfor operation
cout << &quot;\n(1)Add, (2)Multiply:&quot; << endl;
cin >> operand;
//switch case for operation process
cout << &quot;The Answer is: &quot;;
switch (operand)
{
case 1:
cout << Add(firstNumber, secondNumber);
break;
case 2:
cout << Multiply(firstNumber, secondNumber);
break;
}
return 0;
}

 
You've defined firstNumber1 as a double, which is a decimal-point type number. Then you have used it as an array subscript, i.e. in square brackets after array. That's like giving an address as 3.14 Bloggs Street. House numbers just don't work that way, and nor do arrays. The successive boxes of an array are numbered in 1's.
Even if a real number holds an integer, it is not really an integer; it's as well to remember that, anyway, because when you think a real number is 1, the computer probably thinks it is 1.000000000001 due to rounding problems etc. anyway.

I'm not sure what your array is actually doing, but that's probably my problem.
 
Don't use doubles/floats as array indices. If you are going to handle very large integers, use long long int. The maximum this will hold is 9223372036854775807. No idea if you can have an array that big though. You'll probably run out of virtual memory first.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top