Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

nested loops

scuba22scuba22 Member Posts: 61
HI all and an brand new to C++...and my new assignment is killin me!
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 headersL STUDENT ID GPA
SPECIAL NOTATION
I have them before and out of the while loop and it 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...(which means the file with only one student info doesn't print at all).

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.



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.


#include
#include
#include
#include


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 << "

";


/*
infile >> IDnum >> gr1 >> gr2 >> gr3 >> gr4;
if (IDnum > 0)
cout << setw(10) << " STUDENT" << setw(10) << "GPA" << setw(20) << "SPECIAL NOTE" << endl;
else if (! IDnum) // I'm trying to say if there is not an IDnum then priint <NO DATA>, not sure if I'm saying correct
cout << setw(20) << " <NO DATA" << endl;
*/

cout << "

";



while(infile >> IDnum >> gr1 >> gr2 >> gr3 >> gr4)

{

average =(gr1+gr2+gr3+gr4)/4;

if (average <= 1.5)
cout << setw(10) << " " << IDnum << setw(15) << average << setw(15) << "WARNING" << endl;
else
if (average >= 3.5)
cout << setw(10) << " " << IDnum << setw(15) << average << setw(15) << "HONORS" << endl;

else
cout << setw(10) << " " << IDnum << setw(15) << average << setw(15) << endl;
}



cout << "


";



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.


do you think you can just point me in the correct direction?
I am not looking for you to write it for me, maybe just give me an idea of where or what the messup is.

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'm more confused as to how to configure that...even the psuedocode would help.
any guidance would be MOST appreciated..:-D
scuba22
Michele

«1

Comments

  • PeterTheMasterPeterTheMaster Member Posts: 636
    [code]
    : /*
    : infile >> IDnum >> gr1 >> gr2 >> gr3 >> gr4;
    : if (IDnum > 0)
    : cout << setw(10) << " STUDENT" << setw(10) << "GPA" << setw(20) << "SPECIAL NOTE" << endl;
    : else if (! IDnum) // I'm trying to say if there is not an IDnum then priint <NO DATA>, not sure if I'm saying correct
    : cout << setw(20) << " <NO DATA" << endl;
    : */
    [/code]
    no, you dont say it correctly.
    it should be
    [code]

    if(infile>>IDnum){
    infile>>gr1>>gr2>>gr3>>gr4;
    //print entry
    }else{
    //say "NO DATA"
    }

    [/code]

    use [ code ] [ /code ] tags (without spaces) please.


    : do you think you can just point me in the correct direction?
    : I am not looking for you to write it for me, maybe just give me an idea of where or what the messup is.
    :

    awesome.


    : 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'm more confused as to how to configure that...even the psuedocode would help.
    : any guidance would be MOST appreciated..:-D


    thats easy: you put int count = 0; and double sum1 = 0; before the while loop, then ++count; and sum1+=gr1; inside the while loop and double avg1 = sum1/cound; after the while loop.

    of course you need 4 sums. it might be easier to have double gr[4]; and double sum[4];

    im willing to continue helping you. make these modifications and say if you have more problems.

    ps: i will only help if you replace
    [code]
    #include
    #include
    #include
    #include
    [/code]
    by
    [code]
    #include
    #include
    #include
    #include
    using namespace std;
    [/code]

    i wonder where the newbies always pick up that nonstandard old stuff.

  • scuba22scuba22 Member Posts: 61
    thank you for the help...I haven't moved on to the gr averages yet, as I've been away from the computer all day...
    I made the modification on my data vs no data code and the same thing happens...I lose the first students data altogether.
    This is driving me nuts...(i'm sure you can imagine)

    thank you so much for offering your help...
    I'm moving on to the grade averages...though.

    I just an squeaked out on my other problem:
    when I can say no data- i lose the 1st students info...
    when i cannot read an empty file, it all comes out nice-nice.

    I've tried lots of diff combinations with it am am just stuck.
    but I'll letcha know how the averages go..
    Thanks again, hope to hear from you soon.
    (Michele) scuba22
    PS
    i wonder where the newbies always pick up that nonstandard old stuff.
    .h vs "using namespace.std"
    my instructor told us we could use either notation...is that not true?





    : :

  • abcabc Member Posts: 443
    : ps: i will only help if you replace
    : [code]
    : #include
    : #include
    : #include
    : #include
    : [/code]
    : by
    : [code]
    : #include
    : #include
    : #include
    : #include
    : using namespace std;
    : [/code]
    :
    : i wonder where the newbies always pick up that nonstandard old stuff.

    yeah, but i wonder why you want since you follow up with 'using namespace std;' seems kind of pointless.
  • scuba22scuba22 Member Posts: 61
    okay...yes!!! I changed it...

    I don't know what the difference its- since my instructor told us we could use either thought there wwas none...
    I'm l e a r n i n g :-D

    so,...... help?


  • PeterTheMasterPeterTheMaster Member Posts: 636
    : thank you for the help...I haven't moved on to the gr averages yet, as I've been away from the computer all day...
    : I made the modification on my data vs no data code and the same thing happens...I lose the first students data altogether.
    : This is driving me nuts...(i'm sure you can imagine)

    ok, i only checked out your commented block. there is more to do about the logic. you need a do{}while loop. i know its not the best way for you, but since it would take me much longer to explain than just type the code, i do it. i apologize in advance.

    [code]

    #include
    #include
    #include
    #include
    #include
    using namespace std;

    const string inName = "scuba22.txt";
    const string outName = "scuba22res.txt";
    const int GRADES = 4;


    int main(){

    ifstream infile(inName.c_str());
    if(!infile){
    cerr << "Cannot open input file" << endl;
    return 1;
    }

    ofstream outfile(outName.c_str());
    if(!outfile){
    cerr << "Cannot open output file" << endl;
    return 1;
    }




    int IDnum;

    if(!(infile>>IDnum)){
    cout<<setw(20)<<" <NO DATA"<<endl;
    return 0;
    }

    cout << setprecision(3);
    cout << setiosflags(ios::fixed | ios::showpoint);

    cout << "

    ";
    cout<<setw(10)<<" STUDENT"<<setw(10)<<"GPA"<<setw(20)<<"SPECIAL NOTE"<<endl;
    cout << "

    ";

    int count = 0;
    double sum[GRADES] = { 0, 0, 0, 0};

    do{
    ++count;

    double temp_sum = 0;

    for(int i=0;i<GRADES;++i){
    double grade;
    infile >> grade;
    temp_sum+=grade;
    sum[i]+=grade;
    }

    const double average = temp_sum/4;

    cout << setw(10) << " " << IDnum << setw(15) << average;

    if(average<=1.5){
    cout << setw(15) << "WARNING" << endl;

    }else if(average>=3.5){
    cout << setw(15) << "HONORS" << endl;

    }else{
    cout << endl;

    }

    }while(infile>>IDnum);


    cout << "


    ";

    double avg[GRADES];
    for(int i=0;i<GRADES;++i){
    avg[i]=sum[i]/count;
    cout<<sum[i]<<"/"<<count<<"="<<avg[i]<<" ";
    }
    cout<<endl;

    //infile.close(); //not really necessary since we are about to leave scope anyway
    //outfile.close(); //they make only sense in the middle of a function.

    return 0; //satisfy vc++
    }
    [/code]

    : my instructor told us we could use either notation...is that not true?
    :

    absolutely not. some compilers allow the old notation. but its not standard, so you cant rely on it, so you shouldnt use it.

    btw: what kind of a name is michele? where are you from?
  • PeterTheMasterPeterTheMaster Member Posts: 636

    :
    : yeah, but i wonder why you want since you follow up with 'using namespace std;' seems kind of pointless.
    :

    i usually dont like using namespace std; i just didnt feel like explaining unsing declarations.
  • scuba22scuba22 Member Posts: 61
    Thank you...
    even though you wrote it out, don't think for a minute I'm not going to figure out the whys...I really want to learn this, and want to know it at the end.

    Michele is the french form of Michael, and even though I'm a girl I was named after my uncle who was killed in WWII in France. He was Michael.

    I am an RN, and a graphic artist (who wouldn't eat if it wern't for nursing) but I want to be a programmer...being my second career, I'm really into learning it - not just the grade.
    Thanks..
    Be sure you'll hear from me again, and maybe one day I won't be in the beginner group anymore.

    Michele
    ps my art site-please visit:
    myOmedia aka Michele's Myocardium:
    http://micheles-myocardium.com/

  • scuba22scuba22 Member Posts: 61

    i am trying to take apart your code...and we haven't gotten to anything blocked by this [] as you do here;

    [code]double avg[GRADES];
    for(int i=0;i<GRADES;++i){
    avg[i]=sum[i]/count;
    cout<<sum[i]<<"/"<<count<<"="<<avg[i]<<" ";[/code]
    can you explain that part to me?
    also i am gonna need to give a title to each of those averages, like
    CLASS AVERAGES
    class1 average =
    class2 average =

    and i'm afraid i'll mess up the headers, just want to know if i mimic the previous code.
    we just learned the [code](int i=0;i<GRADES;++i[/code]
    statements but i've only been shown to initialize something without those brackets. It's probable nothing big...just haven't seen it yet.
    scuba22
    ps how'my doing with the code syntax? & I'm from Boston


  • PeterTheMasterPeterTheMaster Member Posts: 636
    :
    : i am trying to take apart your code...and we haven't gotten to anything blocked by this [] as you do here;

    ever heard of arrays?
    in double avg[GRADES]; avg is called an array. array means like "lots of them", so double avg[GRADES]; means we have many doubles. how many? well, GRADES many. and somewhere above we had const int GRADES = 4; so its the same as double avg[4]; but avoids the redundance in writing 4 more than once (what if this value changes?).

    if we have many, we still want to access single ones. so we can access all the four doubles in avg with avg[0], avg[1], avg[2], avg[3]. and since this is such a nice systematic scheme, its perfect for a loop.

    :
    : [code]double avg[GRADES];
    : for(int i=0;i<GRADES;++i){
    : avg[i]=sum[i]/count;
    : cout<<sum[i]<<"/"<<count<<"="<<avg[i]<<" ";[/code]
    : can you explain that part to me?
    : also i am gonna need to give a title to each of those averages, like
    : CLASS AVERAGES
    : class1 average =
    : class2 average =

    like maths 1.0, english 1.3, pe 4.0?
    but you only need these names in the output, right? so whats the problem?

    :
    : and i'm afraid i'll mess up the headers, just want to know if i mimic the previous code.
    : we just learned the [code](int i=0;i<GRADES;++i[/code]
    : statements but i've only been shown to initialize something without those brackets. It's probable nothing big...just haven't seen it yet.
    : scuba22

    you mean you dont know a for loop?
    [code]
    for(int i=0;i<4;++i){
    cout<<i<<endl;
    }
    [/code]
    well, you know a while loop and you know if, right? so you can think of [code]
    for(A;B;C){ D; }
    [/code] as an optimized version of
    [code]
    A;
    while(B){
    D;
    C;
    }
    [/code]
    easy, isnt it?

    its called for loop because of mathematics i think. you say the sum FOR i = 0 to n ... you know that big sigma, right? thats what helped me understanding a for loop about 13 years ago (o my god, i am old!).


    : ps how'my doing with the code syntax?

    well, the syntax can hardly be anything else than right or wrong. your compiler decides about that...

    : & I'm from Boston

    hm. im not too familiar with american prejudices. does that mean like "speak slow and loud please"?



  • scuba22scuba22 Member Posts: 61
    : : & I'm from Boston
    :
    : hm. im not too familiar with american prejudices. does that mean like "speak slow and loud please"?
    :
    :
    : nope. you asked a coupla replys ago...
    gnight...
    gonna give it my best to understand tomorrow, & thanks again
    M
    :

«1
Sign In or Register to comment.