wierd errors

This is a discussion on wierd errors within the C++ Programming forums, part of the General Programming Boards category; hey, im havin some wierd errors with this file writing program i made a little while ago: Code: if(age == ...

  1. #1
    Arggggh DeepFyre's Avatar
    Join Date
    Sep 2004
    Posts
    227

    wierd errors

    hey, im havin some wierd errors with this file writing program i made a little while ago:

    Code:
    if(age == 15)
                    {
                        ofstream fout("15MWeight.txt", ios::app);
                        fout << totalweight <<'\n';
                        fout.close();
                        
                        ofstream fout2("15MHeight.txt", ios::app);
                        fout2 << totalheight <<'\n';
                        fout2.close();
                    }
    it is obviously supposed to write the variables totalweight and total height to 15MWeight and 15MHeight respectively. The problem is that it will only write the Height file and not the weight one. I have the EXACT same setup for age 13 (copy + paste + changing 15s to 13s) and it works when I input data with the age being 13. Also i have the exact same setup for a 15 year old female and it works fine, too. anyone know why?


    thanks a load
    Keyboard Not Found! Press any key to continue. . .

  2. #2
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    The only thing I could think of is you need to flush the buffer, but I am not in the know about files.

  3. #3
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    It works for me:
    Code:
    #include <fstream>
    using namespace std;
    
    int main()
    {
        int age = 15;
        int totalweight = 175;
        int totalheight = 70;
    
        if(age == 15)
        {
            ofstream fout("15MWeight.txt", ios::app);
            fout << totalweight <<'\n';
            fout.close();
            
            ofstream fout2("15MHeight.txt", ios::app);
            fout2 << totalheight <<'\n';
            fout2.close();
        }   
    }
    Make sure you are actually looking at the right files. Make sure you don't have the files open in another application (e.g. Notepad) when you do this. Just start everything from scratch. If still doesn't work, then post more code.

  4. #4
    Arggggh DeepFyre's Avatar
    Join Date
    Sep 2004
    Posts
    227
    hmmmm....like i said before the 15 year old male category is the only section that doesnt work, and the even wierder thing is why would it create the height file but not the weight? f anything you would think it would be the other way around.
    Code:
     
        if (gendermale == true)
            {
                if(age == 13)
                    {
                        ofstream fout("13MWeight.txt", ios::app);
                        fout << totalweight <<'\n';
                        fout.close();
                        
                        ofstream fout2("13MHeight.txt", ios::app);
                        fout2 << totalheight <<'\n';
                        fout2.close();
                    }    
                if(age == 14)
                    {
                        ofstream fout("14MWeight.txt", ios::app);
                        fout << totalweight <<'\n';
                        fout.close();
                        
                        ofstream fout2("14MHeight.txt", ios::app);
                        fout2 << totalheight <<'\n';
                        fout2.close();
                    }    
                if(age == 15)
                    {
                        ofstream fout("15MWeight.txt", ios::app);
                        fout << totalweight <<'\n';
                        fout.close();
                        
                        ofstream fout2("15MHeight.txt", ios::app);
                        fout2 << totalheight <<'\n';
                        fout2.close();
                    }    
                if(age == 16)
                    {
                        ofstream fout("16MWeight.txt", ios::app);
                        fout << totalheight <<'\n';
                        fout.close();
                        
                        ofstream fout2("16MHeight.txt", ios::app);
                        fout2 << totalheight <<'\n';
                        fout2.close();
                    }    
            }
            
        if (gendermale == false)   
            {
                if(age == 13)
                    {
                        ofstream fout("13FWeight.txt", ios::app);
                        fout << totalweight <<'\n';
                        fout.close();
                        
                        ofstream fout2("13FHeight.txt", ios::app);
                        fout2 << totalheight <<'\n';
                        fout2.close();
                    }    
                if(age == 14)
                    {
                        ofstream fout("14FWeight.txt", ios::app);
                        fout << totalweight <<'\n';
                        fout.close();
                        
                        ofstream fout2("14FHeight.txt", ios::app);
                        fout2 << totalheight <<'\n';
                        fout2.close();
                    }    
                if(age == 15)
                    {
                        ofstream fout("15FWeight.txt", ios::app);
                        fout << totalweight <<'\n';
                        fout.close();
                        
                        ofstream fout2("14FHeight.txt", ios::app);
                        fout2 << totalheight <<'\n';
                        fout2.close();
                    }    
                if(age == 16)
                    {
                        ofstream fout("16FWeight.txt", ios::app);
                        fout << totalheight <<'\n';
                        fout.close();
                        
                        ofstream fout2("16FHeight.txt", ios::app);
                        fout2 << totalheight <<'\n';
                        fout2.close();
                    }
    Keyboard Not Found! Press any key to continue. . .

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    53
    Hey man, look at this...
    Code:
    #include <iostream>
    #include <string>
    
    int main(void) {
    	bool gendermale = true;
    	const unsigned int age = 14;
    	std::string filename = "";
    	filename += char(((age/10)%10)+'0');
    	filename += char((age%10)+'0');
    	filename += gendermale ? 'M' : 'F';
    	std::cout << (filename + "Weight.txt");
    	return 0;
    }
    You don't need that much ifs.

  6. #6
    Arggggh DeepFyre's Avatar
    Join Date
    Sep 2004
    Posts
    227
    yea, i was thinking of that but i just didnt know the syntax for it, thanks man!
    Keyboard Not Found! Press any key to continue. . .

  7. #7
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Code:
                if(age == 15)
                    {
                        ofstream fout("15FWeight.txt", ios::app);
                        fout << totalweight <<'\n';
                        fout.close();
                        
                        ofstream fout2("14FHeight.txt", ios::app);
                        fout2 << totalheight <<'\n';
                        fout2.close();
                    }
    This is for female, but it has an error (the height file says 14 instead of 15).

    Of course, if you do it tilex's way, or something similar, you would have a lot less chance of weird errors creeping in.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    A better way of getting the filename uses stringstreams:
    Code:
    #include <sstream>
    #include <iostream>
    
    int main()
    {
      	bool gendermale = true;
    	const unsigned int age = 14;
    	std::stringstream filename;
    	filename << std::setw(2) << std::setfill('0') << age;
    	filename << gendermale ? 'M' : 'F';
    	filename << "Weight.txt";
    	std::cout << (filename.str().c_str());
    }
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ten Errors
    By AverageSoftware in forum Contests Board
    Replies: 0
    Last Post: 07-20-2007, 10:50 AM
  2. bloodshed compiler errors
    By nerore in forum C++ Programming
    Replies: 5
    Last Post: 04-17-2004, 02:37 PM
  3. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 04:44 PM
  4. Help me with these errors... :-(
    By major_small in forum C++ Programming
    Replies: 6
    Last Post: 09-07-2003, 08:18 PM
  5. errors in class(urgent)
    By ayesha in forum C++ Programming
    Replies: 2
    Last Post: 11-10-2001, 05:51 PM

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