Agello

code is the same:

[CODE]#include

include

int main(){

int factor, o, copy[10], i;
char option, input[20], output[20], getnumber[10], a[20];
FILE *source, *target;

do{

    printf("Name input file:\t");
    scanf("%s", &input);
    source=fopen(input, "r");
    if(source==NULL)
        printf("\nCannot access input file. Try again.\n");
}while(source==NULL);

printf("Name output file:\t");
scanf("%s", &output);
target=fopen(output, "w");

printf("Choose your operator: \"+\" ; \"-\" ; \"*\" ; \"/\" \n>\t");
scanf(" %c", &option);
printf("You chose : %c \nChoose your factor:\n>\t", option);
scanf("%d", &factor);
printf("You chose : %i\n",factor); 
printf(" %c", option);

if(option=='+'){
    printf("\nLet's Add!!!\n");
    system("cls");
    do{ 
        o=0;
        do{                                                         //do that causes the problem
            getnumber[o]=fgetc(source);                

            if(getnumber[o]==' '){
                 /*               some function        */
            }

            o++;
            printf("\n\n%d",o-1);                                   //used to find out the number of extra useless lines before terminating i.e. 316 lines
        }while(getnumber[o]!=' ');                                  //this while goes with do

    }while(getnumber[o-1]!=EOF);

}    
fclose(source);
fclose(target);
system("pause");[/CODE]

Agello

Nope it is not working....

Help GUYS!!!

Agello

Ok I found the error

Agello

But I do need it to compute the number, do operation on it. The program is not finished you see. And plus I want to know why it doesn't work.

Or should I resign from the loop and find alternative way?

Agello

Another problem lads:
[CODE]

include
include

int main(){

int factor, o, copy[10], i;
char option, input[20], output[20], getnumber[10], a[20];
FILE *source, *target;

do{

    printf("Name input file:\t");
    scanf("%s", &input);
    source=fopen(input, "r");
    if(source==NULL)
        printf("\nCannot access input file. Try again.\n");
}while(source==NULL);

printf("Name output file:\t");
scanf("%s", &output);
target=fopen(output, "w");

printf("Choose your operator: \"+\" ; \"-\" ; \"*\" ; \"/\" \n>\t");
scanf(" %c", &option);
printf("You chose : %c \nChoose your factor:\n>\t", option);
scanf("%d", &factor);
printf("You chose : %i\n",factor); 
printf(" %c", option);

if(option=='+'){
    printf("\nLet's Add!!!\n");
    system("cls");
    do{ 
        o=0;
        do{                                                         //do that causes the problem
            getnumber[o]=fgetc(source);                

            if(getnumber[o]==' '){
                 /*               some function        */
            }

            o++;
            printf("\n\n%d",o-1);                                   //used to find out the number of extra useless lines before terminating i.e. 316 lines
        }while(getnumber[o]!=' ');                                  //this while goes with do

    }while(getnumber[o-1]!=EOF);

}    
fclose(source);
fclose(target);
system("pause");

}[/CODE]

Now as in comments I pointed out a [ICODE]do{}[/ICODE] is causeing me some problems. The function works perfectly untill the program gets to the end of the string in the input file, that it repeats [ICODE]getnumber[o]=fgetc(source);[/ICODE] only, and when it gets to [ICODE]o[/ICODE]=315 that it terminates.

On the other hand if i remove the [ICODE]do{[/ICODE] and [ICODE]}while(getnumber[o]!=' ');[/ICODE], the program works fine! It's just that I need this [ICODE]do[/ICODE] loop there.

Please any ideas guys?

Agello

Thanks a MIL mitrmkar!!! I completely forgot that original I had [ICODE]factor[/ICODE] as [ICODE]char[/ICODE]. So I changed it to [ICODE]int[/ICODE] and it works like a charm!

[ICODE]scanf()[/ICODE] is flushing [ICODE][/ICODE] as %d is skipping newline anyway - that's what i learned yesterday. It's [ICODE]scanf("%c",&x)[/ICODE] with c, where the problem exists, and to prevent that a space is required, [ICODE]scanf(" %c",&x)[/ICODE], that's what I have learned over the weekend anyway.

Thanks all of you guys for help!
Cheers

Agello Newbie Poster

Hi there folks,

I got some minor problem with this code:

[CODE]

include
include

int main(){

char option, factor;

printf("Choose your operator: \"+\" ; \"-\" ; \"*\" ; \"/\" \n>\t");
scanf(" %c", &option);
printf("You chose : %c \nChoose your factor:\n>\t", option);
scanf("%d", &factor);
printf("You chose : %i\n",factor); 
printf(" %c", option);

if(option=='+'){
    printf("It's WORKING!");
    system("pause");
}

return 0;

}
[/CODE]

and it doesn't work. Program skips "if()" as if it wasn't there and continues with "return 0;". On the other hand if I remove "scanf("%d", &factor);" it works prefectly fine!

Can anyone tell me why?

regards