Thread: bool problems

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

    bool problems

    Hi all...
    i hope someone can help me cause i have been going nuts over this for over a week!

    I need to read a file and if there is data in it
    print a header line : StUDENT ID GPA NOTES
    ONE TIME. and go on to read the information and format it.
    If the infile is empty Print <no data>.

    I've decided to use a bool flag, but i am messing up the syntax somehow because my header line keeps repeating and my NO DATA always prints as well...

    here is my code
    Code:
    	bool flag = true;
    	while(infile >> IDnum >> gr1 >> gr2 >> gr3 >> gr4){
    	if (flag = true)
    	cout<<setw(10)<<"\t\t\tSTUDENT"<<setw(10)<<"GPA"<<setw(20)<<"SPECIAL NOTE"<<endl; 
    	bool flag = false;   //changing the flag to false 
    	else cout << "<NO DATA>"<<   endl; 
    			}

    can anyone tell me what is wrong with it?
    if the flag is flase, which i change it to after printing the headers..
    it shouldn't loop around and print again, right?
    and if it is flase to begin with it should say no data...
    ahhhhh this is driving me nuts!! can some one please help?
    thanks Michele scuba22

  2. #2
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    try:
    Code:
    if( flag == true )  //or just if( flag ), same thing
    {
    .
    .
    .
    }
    You are testing the bool but assigning it true evry time, by mixing up = and ==
    Couldn't think of anything interesting, cool or funny - sorry.

  3. #3
    Registered User
    Join Date
    Aug 2002
    Posts
    170
    when you do an if always use ==.

    == compares to values
    = sets the value.

    if( flag = true ) // THis sets flag to true and the if says "Did I succesfully set it to true?

    you want

    if( flag == true )

    or better yet

    if(flag)
    Best Regards,

    Bonkey

  4. #4
    Registered User
    Join Date
    Aug 2002
    Posts
    170
    1 more thing. You are redefining flag. get rid of the bool in:

    bool flag = false;

    You already defined the variable flag. So it's just:

    flag = flase;
    Best Regards,

    Bonkey

  5. #5
    Registered User
    Join Date
    Aug 2002
    Posts
    170
    1 more thing. (hehe)

    Your if statement will only process 1 line if no {} are used.

    Code:
    if(flag)
    {
      cout<<setw(10)<<"\t\t\tSTUDENT"<<setw(10)<<"GPA"<<setw(20)<<"SPECIAL NOTE"<<endl;
      flag = false;
    }
    else
      cout << "<NO DATA>"<<   endl;
    I think that's everything.
    Best Regards,

    Bonkey

  6. #6
    Registered User scuba22's Avatar
    Join Date
    Oct 2002
    Posts
    35
    my output is showing the NO DATA on each read line!:
    (although the header only printed once!)
    STUDENT GPA SPECIAL NOTE
    1022 2.500
    <NO DATA>
    1319 2.825
    <NO DATA>
    1191 1.500 WARNING
    <NO DATA>
    1333 3.375
    <NO DATA>
    1032 2.825
    <NO DATA>
    1115 1.075 WARNING
    <NO DATA>
    1234 1.000 WARNING
    <NO DATA>
    1551 4.000 HONORS
    <NO DATA>
    1789 3.200
    <NO DATA>
    1729 1.175 WARNING
    Press any key to continue


    i used
    Code:
    			 bool flag = true;
    			 while(infile >> IDnum >> gr1 >> gr2 >> gr3 >> gr4){
    		 if(flag)
    			{
    			 cout<<setw(10)<<"\t\t\tSTUDENT"<<setw(10)<<"GPA"<<setw(20)<<"SPECIAL NOTE"<<endl;
    			 flag = false;
    			}
    			else
    			cout << "<NO DATA>"<<   endl;
    any other suggestions?
    I'm closer, i think?
    thanks
    Michele

  7. #7
    Registered User
    Join Date
    Aug 2002
    Posts
    170
    Can you post the code jsut before this? I don't understand the what that flag is suposed to do. The way it is written it should only read 1 line then it will say no data. If you are putting this inside another loop this would explain the output you are seeing.


    Code:
    bool FoundData=false;
    while(infile >> IDnum >> gr1 >> gr2 >> gr3 >> gr4){
    	cout<<setw(10)<<"\t\t\tSTUDENT"<<setw(10)<<"GPA"<<setw(20)<<"SPECIAL NOTE"<<endl; 
    	FoundData =true; 
                   }
    if(!FoundData)
      cout << "<NO DATA>"<<   endl;
    Last edited by bonkey; 10-11-2002 at 10:41 AM.
    Best Regards,

    Bonkey

  8. #8
    Registered User scuba22's Avatar
    Join Date
    Oct 2002
    Posts
    35
    the program is supposed to read a file of students and grades
    and calculate the student average and the course average..
    we are reading from 3 diff files, the first has 10 students, the second is empty and the third one student.
    so, what i want it to
    have the header line: student gpa special notations
    porint just once if there is data and then list the students and their gpa with a warning or honors for high and low grades, but if the file has no info print <NO DATA>...

    Code:
    //Michele D'Addio
     //cs201 HW_10/03/02
    
     
    #include<iostream> 
    #include<iomanip> 
    #include<fstream> 
    #include<cstdlib> 
    #include<string>
    using namespace std;
     
    
    
     
    
    int main(){ 
    	int IDnum;
    	double gr1, gr2, gr3, gr4;
    	double average;
    	int total = 0;
    	int gr1Count = 1;
    	int gr2Count = 1;
    	int gr3Count = 1;
    	int gr4Count = 1;
    	double gr1Av;
    	double gr2Av;
    	double gr3Av;
    	double gr4Av;
    
    
     
        ifstream infile("A:\\IF1.txt");
        if(!infile){ 
            cerr << "Cannot open input file" << endl; 
            return 1; 
        } 
     
        ofstream outfile("A:\\0F1.txt"); 
        if(!outfile){ 
            cerr << "Cannot open output file" << endl; 
            return 1; 
        } 
    
    	cout << setprecision(3); 
        cout << setiosflags(ios::fixed | ios::showpoint); 
    
    
    			 bool flag = true;
    			while(infile >> IDnum >> gr1 >> gr2 >> gr3 >> gr4){
    		 
    				if(flag == true)
    			{
    			 cout<<setw(10)<<"\t\t\tSTUDENT"<<setw(10)<<"GPA"<<setw(20)<<"SPECIAL NOTE"<<endl;
    				flag = false;
    			}
    				else{
    			 cout << "<NO DATA>"<<   endl;
    				}
    
    		
    
     
    				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;
    
    			
    					total++;
    					gr1Count++;
    					gr2Count++;
    					gr3Count++;
    					gr4Count++;
    
    
    
    			 }
    
    
    
    					
    
    
    
    
    
    
    	return 0;
    }
    thx 4 your help, Bonkey

  9. #9
    Registered User
    Join Date
    Aug 2002
    Posts
    170
    Oh ok, now it makes sense.

    Code:
    bool flag = true;
    bool FoundData=false;
    while(infile >> IDnum >> gr1 >> gr2 >> gr3 >> gr4)
    {
      if (flag)
      {
        cout<<setw(10)<<"\t\t\tSTUDENT"<<setw(10)<<"GPA"<<setw(20)<<"SPECIAL NOTE"<<endl; 
        flag = false;   //changing the flag to false 
      }
      
      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;
    
    			
      total++;
      gr1Count++;
      gr2Count++;
      gr3Count++;
      gr4Count++;
    
    
      FoundData=true;   // Mark that we found some data.
    }
    
    if(! FoundData)
      cout << "<NO DATA>"<<   endl;
    Try that.
    Last edited by bonkey; 10-11-2002 at 11:32 AM.
    Best Regards,

    Bonkey

  10. #10
    Registered User scuba22's Avatar
    Join Date
    Oct 2002
    Posts
    35
    hey bonkey...
    that works....
    i have a ?? tho...
    can it be done without the founddata flag and just with the one bool?

    set bool f to true
    if the bool f is true
    //print header &
    change f to false
    if the bool flag is not true from the beginning, it jumps right to
    changing it to false and prints the NO DATA line.

    just wanna understand it all the way around, ya know?

    Thanks for the help...makes a big difference
    michele

  11. #11
    Registered User
    Join Date
    Aug 2002
    Posts
    170
    yes you could change if( ! FoundData) to if(flag).
    Best Regards,

    Bonkey

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inserting a swf file in a windows application
    By face_master in forum Windows Programming
    Replies: 12
    Last Post: 05-03-2009, 11:29 AM
  2. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  4. C++ FTP class won't work
    By lord mazdak in forum C++ Programming
    Replies: 8
    Last Post: 12-18-2005, 07:57 AM
  5. Socket class OOD
    By lord mazdak in forum C++ Programming
    Replies: 6
    Last Post: 12-17-2005, 01:11 AM