Trying to open file and get it calculate and print gpa and class.
1) Should I make getinfo process both print to file and print to screen?
2) How do I open the file, read the file and put data onto a variable?
3) how exactly would I use the putchar and fputc in this code?
4) How do to check for error opening the file?
Code:
#include <stdio.h>
#include <stdlib.h> //Remove if not in use
#include <ctype.h> //Remove if not in use
#include <iostream>
#define FLUSH while ( getchar() != '\n' );
void getchoice (void);
//void tofile (void);
//void toscreen (void);
void getinfo (void);
int main (void)
{
int oneortwo;
getchoice();
system ("pause");
return 0;
}
void getchoice (void)
{
int oneortwo;
do
{
printf ("Please select where to print the transcript: [1] To file. [2] To screen.\nPlease only select between choices \"1\" or \"2\"\n");
scanf_s ("%d", &oneortwo);
if (oneortwo == 1)
{
printf ("%d: You have chosen to print to file.\n", oneortwo);
// Call function
}
else if (oneortwo == 2)
{
printf ("%d: You have chosen to print to screen.\n", oneortwo);
// Call function
}
else
{
printf ("Invalid entry.\n");
FLUSH;
}
} while (oneortwo != 1 && oneortwo != 2);
}
/*
void tofile (void)
{
FILE* spOut;
float units;
float gpa;
spOut = fopen("record.txt", "r");
if ((spOut = fopen("record.txt", "r")) == NULL) // Or if(!spIn)?
{
printf ("Error opening 'record.txt'\n");
}
while ((fscanf_s(spOut, "%*15c%*15c%*15c:%f:%f\n", &units, &gpa)) != EOF)
{
fprintf (spOut, "Units: %f\n GPA: %f\n", units, gpa);
}
fclose (spOut);
}
*/
void getinfo (void)
{
FILE* spIn;
FILE* spOut;
float units;
float gpa;
spIn = fopen("record.txt", "r");
if ((spIn = fopen("record.txt", "r")) == NULL) // Or if(!spIn)?
{
printf ("Error opening 'record.txt'\n");
}
while ((fscanf_s(spIn, "%*15c%*15c%*15c:%f:%f\n", &units, &gpa)) != EOF)
{
printf ("Units: %f\n GPA: %f\n", units, gpa);
}
fclose (spIn);
}
/*
if (gpa >= 3.99 && gpa <= 4.01)
// = 'A';
else if (gpa >= 3.66 && gpa <= 3.68)
// = 'A-';
else if (gpa >= 3.32 && gpa <= 3.34)
// = 'B+';
else if (gpa >= 2.99 && gpa <= 3.01)
// = 'B';
else if (gpa >= 2.66 && gpa <= 2.68)
// = 'B-';
else if (gpa >= 2.32 && gpa <= 3.34)
// = 'C+';
else if (gpa >= 1.99 && gpa <= 2.01)
// = 'C';
else if (gpa >= 1.66 && gpa <= 1.68)
// = 'D+';
else if (gpa >= 1.32 && gpa <= 1.34)
// = 'D';
else if (gpa >= 0.99 && gpa <= 1.01)
// = 'D-';
else
// = 'F';
*/
/*
sumgpa = gpa1 + gpa2 + gpa3 + gpa4 + gpa5 + gpa6 + gpa7 + gpa8 + gpa9 + gpa10;
sumunits = units1 + units2 + units3 + units4 + units5 + units6 + units7 + units8 + units9 + units10;
averagegpa = sumgpa / sumunits;
printf("You've attempted %.1f units and received an overall GPA of %.2f.\n", sumunits, gpa);
*/