-2

I have code written in C, but it's returning a runtime error showing STANDARD INPUT MISSING and moreover it's showing variables x,y,z can't accept integer values as they need to be double. Here is my code:

#include <stdio.h>;
#include<stdlib.h>;


int main()
{

double r,H;
int i;
int a=5;
int b=10;
int t=5;
int I=10;
int y;
double x,z;
FILE *fp;
fp=fopen("Graph.xlsx" ,"w");
for(i=0;i<=1000;i++){
    r=(.015*i);
    if(r>=0 && r<5){
        H=(I*r)/(2*3.14*a*a);
        fprintf(fp,"%f",r);
        fprintf(fp,"\t");
        fprintf(fp,"%f",H);
        fprintf(fp,"\n");
    }
    else if(r>=5 && r<10){
        H=I/(2*3.14*r);
        fprintf(fp,"%f",r);
        fprintf(fp,"\t");
        fprintf(fp,"%f",H);
        fprintf(fp,"\n");
    }
    else if(r>=10 && r<15){
        x=(r*r- b*b);
        y=(t*t+2*b*t);
        z= 1 - x / y;
        H=z*I/(2*3.14*r);
        fprintf(fp,"%f",r);
        fprintf(fp,"\t");
        fprintf(fp,"%f",H);
        fprintf(fp,"\n");
    }
    else
        H=0;

}
fclose(fp);
return 0;
}
  • 2
    Have you checked the return value of `fopen`? – csl May 23 '16 at 18:14
  • double x,z; ... that's why x and z have to be double and not int. – HardCode May 23 '16 at 19:26
  • 1
    This code (apart from the `;` after the `#include` statements) compiled and ran, producing a text file with `.xlsx` extension, containing pairs of floating point values. But this is not `.xlsx` format and is rejected by MS Excel. You'll need to research the proper format to do that. – Weather Vane May 23 '16 at 19:48

1 Answers1

1

The fundamental mistake is to think that MS Excel can open a text file and take it as a correctly formatted .xlsx file. The text format accepted by Excel is commonly called a .csv or "comma separated variable" file. So I have replaced your tab character with a comma (and simplified each print). When I double-click Graph.csv it correctly runs Excel with the data.

#include <stdio.h>                          // removed trailing ;
#include <stdlib.h>                         // removed trailing ;

int main()
{
    double r,H;
    int i;
    int a=5;
    int b=10;
    int t=5;
    int I=10;
    int y;
    double x,z;
    FILE *fp;
    fp=fopen("Graph.csv" ,"wt");            // open a a CSV file in text mode
    if(fp == NULL) {
        exit(1);                            // added error checking
    }
    for(i=0;i<=1000;i++){
        r = 0.015 * i;
        if(r>=0 && r<5){
            H=(I*r)/(2*3.14*a*a);
            fprintf(fp,"%f,%f\n", r, H);    // simplified to a single statement ...
        }
        else if(r>=5 && r<10){
            H=I/(2*3.14*r);
            fprintf(fp,"%f,%f\n", r, H);    // ... with a comma instead of a tab ...
        }
        else if(r>=10 && r<15){
            x=(r*r- b*b);
            y=(t*t+2*b*t);
            z= 1 - x / y;
            H=z*I/(2*3.14*r);
            fprintf(fp,"%f,%f\n", r, H);    // ... same thing here too
        }
        else
            H=0;

    }
    fclose(fp);
    return 0;
}
Weather Vane
  • 33,872
  • 7
  • 36
  • 56
  • again when I am trying to run your modified code in visual studio 2015 it is showing unexpected error with fopen – Somit Sinha May 25 '16 at 23:05
  • @SomitSinha did you notice I changed the file name from `"Graph.xlsx"` to `"Graph.csv"`? It's unclear from your comment whether the C code is failing to make that file, or whether Excel does not like the file created. – Weather Vane May 26 '16 at 08:28
  • in the error its pinning that( the function type fopen may be unsafe) .the complier is pinning (consider opening fopen_s instead). – Somit Sinha May 26 '16 at 16:42
  • That's not an error, it's a compiler warning. Did your code work? Please [see recent question](http://stackoverflow.com/questions/37459312/safe-functions-in-visual-studio-2015#37459720) about this. What is "unexpected error with fopen" anyway? That's not a message I have ever had. Please be *very specific* about what you are doing, exact messages etc, and what produced them. – Weather Vane May 26 '16 at 16:48