My project crashes in the while loop in the second function. The program has to
ID: 3620945 • Letter: M
Question
My project crashes in the while loop in the second function.The program has to read a file which i have done succesfuly
and then it has to sort it into an array. and that is my problem.
/*
* Program Name: COP 2220-81030 Project 3
*
* Author: <your first and last name>
*
* Description: <put your description here>
*
* Input: <describe the input here>
*
* Output: <describe the output here>
*/
#include <stdio.h>
#include <stdlib.h>
#define FLUSH while(fgetc(*fpt)!= ' ');
//Function Declarations
int get_input_file(char datafile[200], int rc, FILE** fpt);
void inputdata_ary(float array[99], FILE** fpt);
int main (void)
{
//Local Declarations
int rc;
FILE* fpt1;
//Declare Arrays
char datafile[200];
float array[99];
printf("COP 2220-81030 Project 4: Kirtan");
//statements
get_input_file(datafile, rc, &fpt1);
inputdata_ary(array, &fpt1);
}//main
/* ================= get_input_file ======================
* Function Name: <put function name here>
*
* Input Parameters: <describe input parameters here>
*
* Description: <describe what the function does,
* including side effects here>
*
* Return Value: <describe the return value here>
*/
//Local Declarations
int get_input_file(char datafile[100], int rc, FILE** fpt)
{
//Local Declarations
//Statements
printf(" Enter file name: ");
rc = scanf("%s", datafile);
if (rc!=1)
{
printf("Error reading file name");
exit(100);
} // If file open error
if (!(*fpt = fopen(datafile,"r")))
{
printf("Error opening file");
exit(101);
}
return 0;
}
/* ================= inputdata_ary ======================
* Function Name: <put function name here>
*
* Input Parameters: <describe input parameters here>
*
* Description: <describe what the function does,
* including side effects here>
*
* Return Value: <describe the return value here>
*/
void inputdata_ary(float array[99], FILE** fpt)
{
// Local Declarations
int rv;
int count;
//statements
count = 0;
while(count < 99)
{
printf("before fscanf");
rv = fscanf(*fpt, "%f", array[count]);
count++;
printf("after fscanf");
if (rv == EOF)
{
printf("End Of file. No more values");
break;
}
if (rv!=1)
{
FLUSH;
}
}
return;
}
Explanation / Answer
please rate - thanks
your missing an ampersand--I added a print and a sort. this program only works for 99 numbers exactly. you input 99 elements all the time. is this what you wanted
/*
* Program Name: COP 2220-81030 Project 3
*
* Author: <your first and last name>
*
* Description: <put your description here>
*
* Input: <describe the input here>
*
* Output: <describe the output here>
*/
#include <stdio.h>
#include <stdlib.h>
#define FLUSH while(fgetc(*fpt)!= ' ');
//Function Declarations
int get_input_file(char datafile[200], int rc, FILE** fpt);
void inputdata_ary(float array[99], FILE** fpt);
void sortdata(float array[99],int n);
void printdata(float array[99],int n);
int main (void)
{
//Local Declarations
int rc;
FILE* fpt1;
//Declare Arrays
char datafile[200];
float array[99];
printf("COP 2220-81030 Project 4: Kirtan");
//statements
get_input_file(datafile, rc, &fpt1);
inputdata_ary(array, &fpt1);
printf("before sorted ");
printdata(array,99);
sortdata(array,99);
printf(" after sorted ");
printdata(array,99);
}//main
void printdata(float array[99],int n)
{int i;
for(i=0;i<n;i++)
{printf("%.2f ",array[i]);
if((i+1)%10==0)
printf(" ");
}
printf(" ");
}
void sortdata(float array[99],int n)
{int i,j;
float temp;
for(i=0;i<n-1;i++)
for(j=i;j<n;j++)
if(array[i]>array[j])
{temp=array[i];
array[i]=array[j];
array[j]=temp;
}
}
/* ================= get_input_file ======================
* Function Name: <put function name here>
*
* Input Parameters: <describe input parameters here>
*
* Description: <describe what the function does,
* including side effects here>
*
* Return Value: <describe the return value here>
*/
//Local Declarations
int get_input_file(char datafile[100], int rc, FILE** fpt)
{
//Local Declarations
//Statements
printf(" Enter file name: ");
rc = scanf("%s", datafile);
if (rc!=1)
{
printf("Error reading file name");
exit(100);
} // If file open error
if (!(*fpt = fopen(datafile,"r")))
{
printf("Error opening file");
exit(101);
}
return 0;
}
/* ================= inputdata_ary ======================
* Function Name: <put function name here>
*
* Input Parameters: <describe input parameters here>
*
* Description: <describe what the function does,
* including side effects here>
*
* Return Value: <describe the return value here>
*/
void inputdata_ary(float array[99], FILE** fpt)
{
// Local Declarations
int rv;
int count;
//statements
count = 0;
while(count < 99)
{
printf("before fscanf");
rv = fscanf(*fpt, "%f", &array[count]);
count++;
printf("after fscanf");
if (rv == EOF)
{
printf("End Of file. No more values");
break;
}
if (rv!=1)
{
FLUSH;
}
}
return;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.