Mail Archives: djgpp/1997/02/03/13:13:28
Date: Sun, 02 Feb 1997 19:58:57 -0800
From: Pyro Technic <invid AT dmv DOT com>
My prog is a stupid little prog for a math problem. It's due in 14
hours. I need help bad.The prog, B low, compiles w/ just 1 warning
D:\PROGS>gxx -o TOG.exe TOG#2.cpp
TOG#2.cpp: In function `int main(...)':
TOG#2.cpp:30: warning: implicit declaration of function `int getch(...)'
Include <conio.h> to remove this warning. But is is not your problem, see below:
[SNIP]
This is the program.
<here>
// Test of geinus problem #2 for 9th grade math
#include <iostream.h>
void main()
{
char dummy;
int num2, temp, digit[3], j, i;
float num1;
for(i=1; i < 10000; ++i)
{
num1 = i;
num2 = i * 4;
for(j=3; j>=0; ++j)
Here's your problem. C array indices range from 0->(N-1) where N is the size
of the array. The array digit is declared length 3, therefore, digit[j] where
j=3 is out of bounds; also your loop increments j to 4, 5, 6, etc instead of
decrementing without end (this is an infinite loop which eventually goes past
the end of allocated virtual memory pages and gets the SEGV)! This should be:
EITHER
for(j=2; j>=0; j--)
OR
for(j=0; j<3; j++)
{
temp = (int)num1 % 10;
digit[j]=temp;
num1 /= 10;
num1 -= (temp/10);
}
num1 = 0;
num1 = digit[0];
num1 += digit[1] * 10;
num1 += digit[2] * 100;
num1 += digit[3] * 1000;
if(num1==num2)
{
cout << '\n' << num1 << " * 4 = " << num2 << "\nPush a key to
continue" << flush;
dummy = getch();
}
else
cout << ".";
}
}
<to here>
please answer, I'm a desperate newbie. Thanks in advance.
Pyro
--
Art S. Kagel, kagel AT quasar DOT bloomberg DOT com
A proverb is no proverb to you 'till life has illustrated it. -- John Keats
- Raw text -