nested loops PLEASE HELP!!

This is a discussion on nested loops PLEASE HELP!! within the C++ Programming forums, part of the General Programming Boards category; HI all I am brand new to C++.(like chaper 2 in D&D)..and my new assignment is driving me insane! I ...

  1. #1
    Registered User scuba22's Avatar
    Join Date
    Oct 2002
    Posts
    35

    Unhappy nested loops PLEASE HELP!!

    HI all I am brand new to C++.(like chaper 2 in D&D)..and my new assignment is driving me insane!
    I am doing external data formatting student IDs and 4 grades into a table of ID#s...GPA...and special notes for
    honors >= GPA 3.5 and warnings GPA <=1.5.

    I've got that while loop down with an if/else if nested...
    and it works well.
    The thing is the headers STUDENT ID GPA
    SPECIAL NOTATION
    I have them before and out of the while loop and they come up great EXCEPT when I try to say
    "if there's an ID number in the infile print the header and go on to the data, but if the infile is empty print <NO DATA>"
    when I add that if/else statement before the while statement, it does print out NO DATA for the empty file but
    eliminates the first student in the list
    when i format the other files...(file3).

    I hope I am being clear enoguh.
    the 3 external data files are as follows:
    __________________________________________________ __________________________________________________ _______________________________
    1.
    1022 2.0 2.0 2.0 4.0
    1319 4.0 0.0 3.6 3.7
    1191 3.0 0.0 1.5 1.5
    1333 3.6 4.0 2.7 3.2
    1032 2.2 3.7 2.6 2.8
    1115 1.0 0.0 2.0 1.3
    1234 1.0 1.0 1.0 1.0
    1551 4.0 4.0 4.0 4.0
    1789 4.0 3.1 3.0 2.7
    1729 0.0 2.3 2.4 0.0

    2.

    //empty file

    3.
    1022 2.0 2.0 2.0 4.0

    (The first number is the student ID and the next 4 their grades)
    __________________________________________________ __________________________________________________ ______________________________________

    the code that I am using that messes the output is in /* */...
    without it I can print out my output correctly but cannot get a NO DATA statement for the second empty external file.

    Code:
    #include<iostream> 
    #include <iomanip> 
    #include<fstream> 
    #include<stdlib> 
    using namespace.std
    
    int main() 
    
    { 
    
    cout << setprecision(3); 
    cout << setiosflags(ios::fixed | ios::showpoint); 
    
    int IDnum; 
    double gr1, gr2, gr3, gr4; 
    double average; 
    double c1av; 
    double c2av; 
    double c3av; 
    double c4av; 
    
    ifstream infile("A:\\IF1.txt"); 
    if (! infile) 
    { cerr << "Cannot open input file" << endl; 
    exit(1); 
    } 
    
    ofstream outfile("A:\\0F1.txt"); 
    if (! outfile) 
    { cerr << "Cannot open output file" << endl; 
    exit(1); 
    } 
    
    
    
    
    cout << "\t\t\n\n"; 
    
    
    /* 
    	infile >> IDnum >> gr1 >> gr2 >> gr3 >> gr4;
    
    	if (IDnum > 0)
    cout<<setw(10)<<"\t\t\tSTUDENT"<<setw(10)<<"GPA"<<setw(20)<<"SPECIAL NOTE"<<endl; 
    	else if (IDnum <=0)
    	cout<<setw(20)<<"\t\t\t<NO DATA"<<endl; */ 
    
    cout << "\t\t\n\n"; 
    
    
    
    while(infile >> IDnum >> gr1 >> gr2 >> gr3 >> gr4) 
    
    { 
    
    average =(gr1+gr2+gr3+gr4)/4; 
    
    if (average <= 1.5) 
    cout << setw(10) << "\t\t\t" << IDnum << setw(15) << average << setw(15) << "WARNING" << endl; 
    else 
    if (average >= 3.5) 
    cout << setw(10) << "\t\t\t" << IDnum << setw(15) << average << setw(15) << "HONORS" << endl; 
    
    else 
    cout << setw(10) << "\t\t\t" << IDnum << setw(15) << average << setw(15) << endl; 
    } 
    
    
    
    cout << "\t\t\n\n\n"; 
    
    
    
    infile.close(); 
    outfile.close(); 
    
    return 0; 
    }
    the output (which i am doing to the screen not a file so I can check it ...is:
    (it eliminates the first student)??
    so, I am figuring it is because of the aqua code.

    I really need some help here, I'm probably 16 hours deep into this and no closer to an answer than at the beginning!!!

    2.
    I need to also list the average for each grade (Ie: gr1, gr2 etc.)
    would that loop also be included in the big while loop?
    I am having trouble changing the names of the courses:
    like course 1 average:
    course 2 average:
    etc....
    I hope someone can please lend me a hand, i am so frustrated with this!


    any guidance would be MOST appreciated..
    scuba22
    Michele

    &#91;code]&#91;/code]tagged by Salem

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    The way you have it will only work if there is more than one line in the file. To account for the other two possibilities you need to adjust your code a little bit. To solve your problem you need to know that files have a built in end of file marker, called EOF. Even if there is no data in a file there is still an EOF marker. Next you need to know that stream object methods can evaluate for that marker. For example, >> will flip the fail bit if it encounters an EOF marker and get() or getline() will terminate input if an EOF marker is identified even if the maximum number of char hasn't been found yet and even if the terminating character hasn't been found yet.

    Here's a rough outline you can use to modify your code:

    set IDnum to -1111 before attempting to read file;
    attempt to read in first IDnum only, outside the loop;
    set up loop to look for either fail of ifstream or EOF;
    read in rest of first line if ifstream is valid
    do whatever you need to do with the first line of data
    attempt to read in next IDnum
    recheck loop conditional and keep going if appropriate

    when loop is completed look at value of IDnum:
    if IDnum is -1111, then nothing was read in, meaning file was empty
    if IDnum is anything other than -1111 then file wasn't empty.

    the stream function used to look for EOF specifically is:
    eof(). If EOF is encountered on the most recent read from the file, then eof() returns true, otherwise it returns false. It is frequently used like this:

    while(!ifstreamName.eof())
    Last edited by elad; 10-07-2002 at 09:55 AM.

  3. #3
    BMJ
    BMJ is offline
    Banal internet user BMJ's Avatar
    Join Date
    Aug 2002
    Location
    Chicagoland
    Posts
    1,380
    lol, I like the D&D comparison

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    The easiest way to be able to calculate a students grade average over a series of four exams and calculate an average score for everyone who took the each of the four exams is to use a data structure to hold all the information read in from the file rather than doing calculations for a given student and discarding the data. For example, if you use a 2D array of type double you can essentially reproduce the table of values listed in problem 1 internally. Then each student will be a line and each exam will be a column. You will need 5 columns and 10 rows.

    const int COL = 5;
    const int ROW = 10;

    double table[ROW][COL];

    now each col in each row is accessed by the following syntax:

    table[i][j];

    and to build table from data in file you would probably read in all the data first, and then do the calculations later in the program. Maybe something like this:

    declare the table and initialize all values to -1111

    int i and j both initialized to 0 outside loop;

    the first double is read in to table[i][j++];
    then the loop is started
    while(whatever conditional you use)
    {
    if(j == 5)
    {
    j = 0;
    i++;
    }

    read in next data value to table[i][j++]
    }

    when loop is done check value in table[0][0]

    if table[0][0] == -1111 then file empty

    then check value in table[1][0]

    if table[1][0] == -1111 then there was only one row in file
    and calculate the student's average score

    if table[1][0] != -1111
    then each students average can be calculated using columns with index 1 to 4 and each exam average score can be calculated from the given column value (column index 1 to 4) in each row from 0 to 9;

  5. #5
    Registered User scuba22's Avatar
    Join Date
    Oct 2002
    Posts
    35
    I SO APPRECIATE what you guys are trying to tell me...
    but you've gotto understand, I'm on chapter 2!
    I don't have any clue as to what your talking about...
    well I have a LITTLE clue..

    i need to see the code so that i can break it down and try to
    figure out what's being said.

    i'm really trying but i need show and tell basically...
    this is the FIRST loop program I've ever written!

    firt if
    first while...

    i've got my book by my side and am still hgaving trouble.
    can somebody take me thru my problem just a little slower?
    thanks Michele

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Here's a program that is very similar to yours, but not enough so that you can copy it directly. It should give you some ideas and examples you can use. By understanding the comments and using similar comments in your program I think you can take it from here if you follow my previous posts.

    Note: this program has not been compiled yet so there may be some errors. It is meant to be an example, not a solution.

    Code:
    #include <fstream.h>
    
    //global constants to use throughout program
    const int ROW = 2;
    const int COL = 3;
    
    int main()
    {
    
    int i, j;
    
    //container to hold data read from file
    //each int in table initialized to -1111 as default values;
    int table[ROW][COL] = {-1111};
    
    //display table with all defalut values
    for(i = 0; i < ROW; i++)
    {
      for(j = 0; j < COL; j++)
      {
         cout << table[i][j] << ' ';
      }
      cout << endl;
    }
    
    //create a data file with some easy to use numbers
    ofstream fout("data.txt")
    fout << 50 << ' ' << 90 << ' '  << 70  << endl;
    fout << 80 << ' ' << 90 << ' ' << 100;
    
    //declare ifstream to read back data to program
    ifstream fin("data.txt");
    
    //assign default values to index variables
    i = 0;
    j = 0;
    
    //read in first value to fist spot in table, then go to next col
    fin >> table[i][j++];
    
    //as long as fin is valid
    while(fin)
    {
      //if at end of first row
      if(j == 2)
      {
         //reset columns to 0;
         j = 0;
         //go to next row
         i++;
      }
      //read in next value from file
      fin >> table[i][j++];
    }
    
    //display table of values read in from file
    for(i = 0; i < ROW; i++)
    {
      for(j = 0; j < ROW; j++)
      {
         cout << table[i][j];
      }
      cout << endl;
    }
    
    
    //check value in first spot of table, if it's still default value
    if(table[0][0] == -1111)
      cout << "file was empty" << endl;
    else
    {
    //read table to develop student average first and then
    //exam score averages
    int sum;
    double ave;
    
    //calculate each students average
    //use one row at a time
    for(i = 0; i < ROW; i++)
    {
      //set default values for sum and ave for each student
      sum = 0;
      ave = 0.0;
      for(j = 0; j < COL; j++)
      {
         //add each value in line to sum
         sum = sum + table[i][j];
      }
      //calucluate ave
      ave = sum/j;
      //display results
      cout << "student " << (i + 1) << " has average score of " << ave << endl;
    }
    
    //calculatae average score for each exam 
    //now use one column at a time
    for(j = 0; j < COL; j++)
    {
      sum = 0;
      ave = 0.0;
      //add each value in column to sum
      for(i = 0; i < ROW; i++)
      {
         sum += table[i][j];
      }
      //calculate average score on exam
      ave = sum/i;
      //display result
      cout << "ave score on exam " << j +1 << " was " << ave << endl;
    }
    }//end else
    
    return 0;
    }
    edit: had rows and columns switched orignially. Now corrected. Sorry.
    Last edited by elad; 10-07-2002 at 01:46 PM.

  7. #7
    Registered User scuba22's Avatar
    Join Date
    Oct 2002
    Posts
    35
    Thanks very much...really
    but 1. I haven't learned arrays yet
    & 2. the format required is this:

    ifstream infile("A:\\IF1.txt");
    if (! infile)
    { cerr << "Cannot open input file" << endl;
    exit(1);
    }

    ofstream outfile("A:\\0F1.txt");
    if (! outfile)
    { cerr << "Cannot open output file" << endl;
    exit(1);
    }


    followed by while(infile >> IDnum >> gr1 >> gr2 >> gr3 >> gr4)
    __________________________________________________ _

    I'm struggling with the entire concept of loops, and if/else, while, and do/while ......i know your program probably is much more
    sound, versatile and quick, but the basic thing here is I need to know where my initial mistake was, why I made it, and how to fix it.
    I cannot use arrays because I haven't learned them and this assignment was given to us for us to complete with the knowlege we have thus far.

    I'm not looking for a solution - i'm looking to learn.
    elad, maybe you could look at what i wrote...
    and tell me what is wrong with it?
    I'v egot to learn the basics before I can jump 4 weeks and 3 chapters ahead right?
    Thanks a bunch guys
    Michele (scuba22)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Nested loops
    By zeondik in forum C# Programming
    Replies: 2
    Last Post: 10-26-2008, 07:58 AM
  2. Nested For Loops
    By smitsky in forum C++ Programming
    Replies: 2
    Last Post: 11-28-2004, 01:58 PM
  3. Evaluation of nested loops
    By Mister C in forum C Programming
    Replies: 2
    Last Post: 08-13-2004, 02:47 PM
  4. nested for loops
    By akub3 in forum C Programming
    Replies: 2
    Last Post: 04-01-2004, 06:21 AM
  5. Nested for loops
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 02-12-2002, 10:25 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21