Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: C+ Programming Homework Help

  1. Oct 19, 2008 #1
    Here is the homework we have to do:

    Write a program that will read a series of integer grades in the range 0-100 from a file, and display the number of nonzero grades read and the average grade. Here is a test data file (grades.dat) and a sample executable (grades1.exe). For the executable to work, the test data file must be saved to c:\temp.

    Here is what I have coded so far:

    #include <stdio.h>

    int main()

    char namefile[200]; /* name of file to open limited
    to 200 characters */
    FILE *access; // streamName

    int nonZero = 0;
    double total = 0.0;
    double nextVal;
    double value;

    printf("Hello, this program is designed to open a file \n"
    "at a specified directory and open it to calculate \n"
    "the number of non-zero integers and also the average \n"
    "of those integers! \n\n");

    printf("What file would you like to open and calculate? ");
    scanf("%s", namefile);

    access = fopen(namefile, "r");

    if ((access = fopen(namefile, "r")) == NULL)
    printf("\nThat file does not exist! \n");
    printf("The program will now be terminated. \n");

    return 1;

    while (fscanf(access, "%d", &value) != EOF)
    if (value != 0)
    printf("you have %d non-zero integers", nonZero);


    return 0;

    I cannot seem to get the "while" section to work and give me the correct number of non-zero integers. Our teacher gave us an executable file and I know that there is supposed to be 22 non-zero integers, but my program keeps coming up with 25 which I am assuming is the amount of integers in the file we are opening, therefore, there must be three zero integers. Could you please examine my code and tell me what is wrong with my calculating the "non-zero" integer portion? Please I am desperate here!!!
  2. jcsd
  3. Oct 19, 2008 #2


    User Avatar
    Science Advisor
    Homework Helper

    fscanf will also read any blank lines and return 0 (non mached)
  4. Oct 20, 2008 #3
    Well, to be a good programmer you need to learn the tricks.. one trick is to print the value that you read.. so..
    printf("you have %d non-zero integers", nonZero);

    would be:
    printf("Incrementing from %d to %d, the value read was %d\n", nonZero, ++nonZero, value);
    system("pause"); // this can be getch() I think

    So now you can go through and see if you are really adding up zeroes.. also you can do something like:

    else {
    printf("Looks like we got a 0!");

    To see if your if is doing anything at all!
  5. Oct 20, 2008 #4


    User Avatar
    Science Advisor
    Homework Helper

    That's generally a really bad idea. C++ doesn't define what happens if you do i and i++ in the same function call - the compiler is free to order the calls how ever it wants.
    In practice a printf() call is unlikely to change the order - but in general function(i,i++) leads to some really interesting bugs, especially since the order may be differen tin realease and ebug builds.
  6. Oct 25, 2008 #5
    First, I suggest you verify the content of the data file to see if they are integers or floating points (they are supposed to be integers).
    You want to know if there are other non numeric data in the data that could cause problems.
    If you want, you can post the data file or a portion of it as well, so we will know if the integers are on the same line, or on separate lines, whether there are blank lines in between, are there commas as serparators, etc.

    One thing you may want to correct, although I am not sure if it will solve your problem:
    the variable type must match the printing template, in your code:
    > double value;
    > ...
    > while (fscanf(access, "%d", &value) != EOF)
    Value was defined as double, but was read in as integer.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook