Ehm, I'm not sure what your new code looks like, but I'm fairly convinced takes more than a flip between < and > to "fix" your code - it may prevent it from crashing, but I doubt it does the right thing.
--
Mats
Printable View
Here's the code of the running program:
Code:#include <stdlib.h>
#include <stdio.h>
#define big 200
struct integer {
int* digits;
int size;
};
struct integer* read_integer(char* stringInt);
void print(struct integer *p);
struct integer* add(struct integer *p, struct integer *q);
struct integer* subtract(struct integer *p, struct integer *q);
int compare(struct integer *p, struct integer *q);
int main(){
FILE *ifp;
char numbr1[big];
char numbr2[big];
struct integer *num1;
struct integer *num2;
struct integer *num3;
int stringnum;
int i;
int j;
int k;
int comp;
int intarray[big];
int option;
//Read in the file
ifp = fopen("bigint.txt", "r");
//Scan for the number of strings
fscanf(ifp, "%d", &stringnum);
//Read in the numbers
for (k=0; k<stringnum; k++){
fscanf(ifp, "%d, %s, %s", option, numbr1, numbr2);
printf("%d\n", option);
num1 = read_integer(numbr1);
num2 = read_integer(numbr2);
if(option==1){
num3 = add(num1, num2);
}
else if(option==2){
num3 = subtract(num1, num2);
}
else if(option==3){
comp = compare(num1, num2);
}
}
//Read the array
printf("The arrays are: \n");
system("PAUSE");
return 0;
};
//Define functions
struct integer* read_integer(char* stringInt){
int i;
struct integer *read;
read = (struct integer*)malloc(sizeof(struct integer));
read->size = strlen(stringInt);
read->digits = (int*)(malloc(read->size*sizeof(int)));
//Move the integer into the array
int x = read->size - 1;
for(i = read->size - 1; i < 0; i++){
read->digits[i]=(int)(stringInt[x] - '0');
printf("%d", read->digits[i]);
x--;
}
return read;
}
struct integer* add(struct integer *p, struct integer *q){
}
struct integer* subtract(struct integer *p, struct integer *q){
}
int compare(struct integer *p, struct integer *q){
int i;
if(p->size < q->size){
printf("Integer 2 is greater than Integer 1");
return -1;
}
else if(p->size < q->size){
printf("Integer 1 is greater than Integer 2");
return 1;
}
else if(p->size = q->size){
for(i = 0; i > p->size; i++){
if(*q[i].digits > *p[i].digits){
printf("Integer 2 is greater than Integer 1");
return -1;
}
else if(*q[i].digits < *p[i].digits){
printf("Integer 1 is greater than Integer 2");
return 1;
}
}
return 0;
}
}
Does your compiler allow you to enable warnings?
gcc -Wall gives this:
Some of those are pretty grave, as "implict declaration of" means you didn't include the header file of the function you use, and "reached end of non-void function" indicates that you may return rubbish from that function. Even if the function is a "stub" (meaning a nearly empty function waiting to be filled in) it should return SOMETHING.Code:number.c: In function `main':
number.c:40: warning: format argument is not a pointer (arg 3)
number.c:25: warning: unused variable `i'
number.c:26: warning: unused variable `j'
number.c:29: warning: unused variable `intarray'
number.c: In function `read_integer':
number.c:65: warning: implicit declaration of function `strlen'
number.c: In function `add':
number.c:76: warning: control reaches end of non-void function
number.c: In function `subtract':
number.c:79: warning: control reaches end of non-void function
number.c: In function `compare':
number.c:92: warning: suggest parentheses around assignment used as truth value
number.c:105: warning: control reaches end of non-void function
The third if is redundant because you have only got three alternatives: a > b, a < b or a = b. So if you have already tested for the first two, the third MUST be the answer - there is no need to check it (it just makes the code bigger, which is NEVER a good idea).Code:if(p->size < q->size){
printf("Integer 2 is greater than Integer 1");
return -1;
}
else if(p->size < q->size){
printf("Integer 1 is greater than Integer 2");
return 1;
}
else if(p->size = q->size){
Also, I doubt you get past your fscanf() line.
--
Mats
Seeing as Dev-C++ normally uses gcc-mingw 3.4.2, which is EXACTLY the version I'm using (in fact, I just added the dev-cpp/bin path to make it compile with gcc before compiling your code), I doubt that you have actually enabled warnings - seeing as I get them and you don't [or you simply don't look in the right place? - I don't actually use dev-cpp, just Emacs and command line for simple stuff like this].
And my compiled code crashes inside fscanf() when trying the input file you gave earlier in the thread - with two 27-or-so digit numbers, one starting with 74 and the second starting with 29. Not that what input file is given matters, as the fault is in the call to fscanf, not in the rest of the code. Also, the warning for line 40 is the one we are talking about - I missed mentioning it in the "I get these warnings" post, but it's definitely an important one.
--
Mats
Well, unless you posted a different version of the code than you are actually running, it is VERY LIKELY that you will crash on the line of fscanf(), but it is POSSIBLE that you don't under some circumstances - because it is what is called "undefined behaviour". Particularly since "option" is not initialized, your runtime environment may end up with a different value than I do - try setting option to zero before reading it - that ought to crash it.
However, I also checked the return value from fscanf(), and each fscanf() reads ONE element, not the three you expect. That's probably caused by the commas in the format string, which are not matched by commas in the input file - unless you are using a different input file than what I have:
By the way, is this actually correct for your input - you read a stringNum first, then an option and two numbers - but there is a total of only 3 numbers in that input file.Code:2
7432968527294356297648975290
2986783975917945029875942976
--
Mats
Oh, right, sorry. I made a post earlier talking about the fact that I screwed up the original input file. Here's the real correct test input file:
Oh, yeah, and I initialized Option and the program started crashing again. Once again, it's an Access Violation somewhere... Probably at that fscanf(), now that I think about it... What should I do to fix that?Code:3
1 8888888888 2222222222
2 9999999999 10000000000
2 10000000000 9999999999
Hi Graham.
Someone else has started a thread for a problem which appears similar to yours. :)
http://cboard.cprogramming.com/showthread.php?t=111542
Aright, I've done a little more work on the program, and it's running, but Option, numbr1, and numbr2 are spitting junk back at me. Can anyone help me fix this?
Code:#include <stdlib.h>
#include <stdio.h>
#define big 200
struct integer {
int* digits;
int size;
};
struct integer* read_integer(char* stringInt);
void print(struct integer *p);
struct integer* add(struct integer *p, struct integer *q);
struct integer* subtract(struct integer *p, struct integer *q);
int compare(struct integer *p, struct integer *q);
int main(){
FILE *ifp;
char numbr1[big];
char numbr2[big];
struct integer *num1;
struct integer *num2;
struct integer *num3;
int stringnum;
int i;
int j;
int k;
int comp;
int intarray[big];
int option = 0;
//Read in the file
ifp = fopen("bigint.txt", "r");
//Scan for the number of strings
fscanf(ifp, "%d", &stringnum);
printf("%d\n", stringnum);
//Read in the numbers
for (k=0; k<stringnum; k++){
printf("%d\n", k);
fscanf(ifp, "%d, %s, %s", &option, numbr1, numbr2);
printf("%d, %s, %s\n", option, numbr1, numbr2);
num1 = read_integer(numbr1);
num2 = read_integer(numbr2);
if(option==1){
num3 = add(num1, num2);
}
else if(option==2){
num3 = subtract(num1, num2);
}
else if(option==3){
comp = compare(num1, num2);
}
}
//Read the array
printf("The arrays are: \n");
system("PAUSE");
return 0;
};
//Define functions
struct integer* read_integer(char* stringInt){
int i;
struct integer *read;
read = (struct integer*)malloc(sizeof(struct integer));
read->size = strlen(stringInt);
read->digits = (int*)(malloc(read->size*sizeof(int)));
//Move the integer into the array
int x = read->size - 1;
for(i = read->size - 1; i < 0; i++){
read->digits[i]=(int)(stringInt[x] - '0');
printf("%d", read->digits[i]);
x--;
}
return read;
}
void print(struct integer *p){
int i;
printf("%d\n", p->size);
//this print loop prints out wrong even though the other one is right
for(i = p->size -1 ; i >= 0; i--){
printf("%d\n", p->digits[i]);
}
}
struct integer* add(struct integer *p, struct integer *q){
}
struct integer* subtract(struct integer *p, struct integer *q){
}
int compare(struct integer *p, struct integer *q){
int i;
if(p->size < q->size){
printf("Integer 2 is greater than Integer 1");
return -1;
}
else if(p->size < q->size){
printf("Integer 1 is greater than Integer 2");
return 1;
}
else {
for(i = 0; i > p->size; i++){
if(*q[i].digits > *p[i].digits){
printf("Integer 2 is greater than Integer 1");
return -1;
}
else if(*q[i].digits < *p[i].digits){
printf("Integer 1 is greater than Integer 2");
return 1;
}
}
return 0;
}
}