Thread: Two Problems

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    3

    Two Problems

    Hello Everyone...

    This is my first post here... I'm new to C++ and I have a home work program that has me confused.


    My first problem is my program just hangs.. I can type in the file name and then it hangs.. If I do put in the incorrect name it traps the error. I know my program is pretty screwed up. I worked on this all day yesterday.

    Secondly, I need to use a class? How can I create a class out of this program? (classes totally confuse me)

    Thanks for your help...

    Code:
    #include  <iostream>                         
    #include  <iomanip>                                          // for set precision 
    #include  <fstream>                                          // for file i/o 
    #include  <string> 
    #include  <istream> 
    
    using namespace std; 
    
    
    
    //*****  Main Program ***** 
    
    int main () 
    
    { 
    
    
    //***** Variable and Const Declaration *****//    
             string      FirstName; 
             string       LastName; 
             float        PersonalIncome; 
             ifstream   IncFile; 
             string       FileName; 
             float       FamilyIncome; 
             int         FamilyMembers; 
             float      AGI; 
             float      TaxRate; 
             //float      TaxAmount; 
             string      FamilyName; 
        const   float      Deduction  =  5000.00; 
        const   float      MaxIncome  = 60000.00;             
        
    
       cout    <<fixed<<showpoint           // Set floating point; 
           <<setprecision(2);              // Set up output format 
    
    
    
        
    //***** File Name Input and Read *****// 
    
    cout << "Name of the File to read: "; 
    cin  >> FileName; 
    IncFile.open(FileName.c_str());      //opens the input file 
    if (!IncFile)    
     { 
       cout << "***** Can't open input file *****" << endl; 
       return 1;    
     };    
      
         
        
      
      //***** Intilization of variables *****// 
      
      FirstName           =  " "; 
      LastName            =  " "; 
      FamilyName           =   " "; 
      PersonalIncome        =  0.00; 
      FamilyIncome          =  0.00; 
      FamilyMembers          =  1   ; 
      AGI                =  0.00; 
      
      
    
    IncFile >> LastName >> FirstName >> PersonalIncome; 
    
    
    while(IncFile) 
      
         { 
                
         getline(cin,LastName); 
         getline(cin,FirstName); 
         cin >> PersonalIncome; 
             
                            
          
         if (LastName==FamilyName) 
         {      
                           FamilyIncome=FamilyIncome + PersonalIncome;             FamilyMembers++;                                 } 
                
         else       
         AGI = FamilyIncome - (Deduction * FamilyMembers); 
               if (FamilyIncome < MaxIncome) 
                  {TaxRate = (AGI/100000); }    
                   
             else 
               {TaxRate = (AGI * .50);};                        
               cout << LastName << setw(2) << TaxRate; 
               cout << endl; 
                
                           FamilyName = LastName;   //reset variables 
                           FamilyIncome        =  0.00; 
                FamilyMembers        =  1   ; 
                AGI                =  0.00; 
                     
        } 
         
    
    return 0; 
    };

  2. #2
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Code:
    while(IncFile) 
      
         {
    For some reason at that point, IncFile is false, therefore the while block is skipped and the program returns.

    If you're new to C++, I wouldn't get into classes just yet. Get good at the basics, then try classes.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    3
    Thanks...

    I wasn't sure if my file is getting read correctly...?

    I really don't have a choice(regarding classes)... involved in a class.. no pun intended

    Thanks for your help...

  4. #4
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Code:
         getline(cin,LastName); 
         getline(cin,FirstName); 
         cin >> PersonalIncome;
    Are you sure you want 'cin' instead of 'IncFile'?
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  5. #5
    Registered User
    Join Date
    May 2004
    Posts
    3
    OK... Got it working... Now.. I know I did this backwards.. but I need to create a class and use it with this program......

    Thanks for yourt help...


    Here is my code...

    Code:
    #include  <iostream>                          
    #include  <iomanip>                                          // for set precision 
    #include  <fstream>                                          // for file i/o 
    #include  <string> 
    #include  <istream> 
    #include  <console.h>
    
    using namespace std; 
    
    
    
    //*****  Main Program ***** 
    
    int main() 
    {        
        int			index;
    	int			count;
    	int         FamilyMembers;
        
        string      FirstName[50]; 
        string      LastName[50]; 
        string      FileName;
        string      FamilyName; 
       
        ifstream    IncFile; 
         
        float       FamilyIncome; 
        float       AGI; 
        float       TaxRate;  
        float		FamilyTax;
        float       PersonalIncome[50]; 
        
        const float Deduction = 5000.00; 
        const float MaxIncome = 60000.00;      
    	
    
       cout << fixed << showpoint << setprecision(2);														//set output format              
    
       cout << "Input file name: "; 													
       cin  >> FileName;																									// Get file name and location 
       IncFile.open(FileName.c_str());      																				// Opens the input file 
       if (!IncFile)    
        { 
          cout << "***** Can't open input file *****" << endl; 
          
          return 1;    
        }
       
     //clrscr();   
     count=0;  
    
    while(IncFile) 																											//Read file
      { 
        IncFile >> LastName[count] >> FirstName[count] >> PersonalIncome[count];
        IncFile.ignore (100,'\n');
        count++;
        
      }
       
       
           
     
     FamilyMembers=1;																										//Set variables to first record
     FamilyIncome=PersonalIncome[0];
     FamilyName=LastName[0];
     
    cout << setw(10) << "Family" << setw(20) << "Total Income" << setw(20) << "AGI" << setw(20) <<  "Tax Due";				// Output headers
    cout << endl << endl;
    
    
           
     for (index=1; index < count; index++)   		    
        
       { 
    	  if (LastName[index-1] == LastName[index]) 
           { 
             FamilyIncome=FamilyIncome + PersonalIncome[index]; 
             FamilyMembers++; 
           } 
       	  else        																						        																								
          {
            AGI = FamilyIncome - (Deduction * FamilyMembers); 																//Calculate adjusted gross income
            
                if (FamilyIncome < MaxIncome) 
                  { 
                   TaxRate = (AGI/100000); 																					// Calculate tax rate for under $100,000
                  }                
                 else 
                    {    
                     TaxRate = (AGI * .50); 																				//Calculate tax rate for over $100,000
                    };                        	
            
           
           FamilyTax = FamilyIncome * TaxRate;																				// Calculate Family Tax		
            if  (FamilyTax <= 0)
            	{
            	 FamilyTax = 0;
            	};  															
           
           cout << setw (10) <<  FamilyName << setw(20) << FamilyIncome <<  setw(20) << AGI << setw(20) << FamilyTax;		//Output  
           cout << endl; 
          
           TaxRate=0.00;																									//Set data for next family
           FamilyMembers=1;
           FamilyIncome=PersonalIncome[index];     
           FamilyName=LastName[index];       
        
          } 
    	   
        
              
       } 	
     
      
    return 0;  
    }

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    The following variables suggest that a class called FamilyMember might be useful:

    string FirstName[50];
    string LastName[50];
    string FileName;
    string FamilyName;
    float FamilyIncome;
    float PersonalIncome[50];

    Then to allow you to do something with that data you could have a methods called CalculateFamilyTax() and displayResults() or something else, since that seems to be the primary purpose of the gathering the above data.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 06:35 AM
  3. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:48 PM
  4. contest problems on my site
    By DavidP in forum Contests Board
    Replies: 4
    Last Post: 01-10-2004, 09:19 PM
  5. DJGPP problems
    By stormswift in forum C Programming
    Replies: 2
    Last Post: 02-26-2002, 04:35 PM