Thread: Help with a program- I'm clueless

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    13

    Help with a program- I'm clueless

    Hey guys, I really need help with this program I am to write out, I don't even know where to start I just can't get my head around files no matter how much I study it. My notes aren;t that great either. Apparently its pretty basic stuff but I'm literally clueless.

    I was wondering if someone on here would be ever so kind and
    help me out somewhat with this program I am to write. I will be
    ever so grateful with any kind of help you may provide
    Code:
    Write a program that will give the user the following menu options;
    
    • Copy a file
    1. Calculate average emplyee wage
    Note: The program should be written using file & functions. Therefore if the user chooses option 1, a function called copyfile should be invoked. If the user chooses option 2, a function called averagewage should ne invoked. All the work involved in this program should be done in functions.
    1. If the user chooses option 1, they should be prompted for the name of the file to copy from. The user should then be asked for the name of the file to copy to and when the operation is finished, a message should be displayed to the user.
    1. If the user chooses option2, they should be asked for the name of thewage file that they wish to read from(assume the wage file containing an indefinite number of integer values,) The total of all wages in the file should be maintained and the average calculated.
    1. If the user chooses an invalid menu option, an appropriate message should be displayed.
    Last edited by godfrey270487; 03-23-2007 at 05:53 PM.

  2. #2
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    Code:
    int main()
    {
         return 0;
    }
    Is a good place to start. Post all the code you can do by yourself. Make the functions you need even if you don't know what to put in them. Write the things you know well. For things you don't know about like files, just write in the psuedocode (want you want to happen) and we'll help you with the specifics.
    Don't quote me on that... ...seriously

  3. #3
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    I recommend you read this tutorial about C++ file I/O.

  4. #4
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    I second KONI.

    Once you can understand the basic terminology of the fstream library, you should be able to tackle that assignment(?) much better.
    Double Helix STL

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    13
    they should be prompted for the name of the
    file to copy from. The user should then be asked for the name of the file to
    copy to and when the operation is finished, a message should be displayed
    to the user.
    How would I do this?? This is what I have so far.. It's like chinese writing to me..lol

    Code:
    void main()
    {
    		
    	
    	ifstream infile;
    	ofstream outfile;
    	string name;
    	
    	outfile.open("F:\\names2.txt");
    	infile.open("F:\\names1.txt");
    	for (int i=0; i<10; i++)
    	{
    	
    	infile>>name;
    	cout<<name<<endl;
    	outfile<<name<<endl;
    	
    	
    	
    	}cout<<" All data has been copied to names2.txt"<<endl;
    	infile.close();
    	
    	
    	}
    I'm suppose to be asked for the file name but I don't know how to do that

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    13
    Code:
    #include <fstream>
    #include <iostream>
    
    using namespace std;
    
    void main()
    {
    	cout<<"Enter name of file";
    	string filename;
    	cin>>filename;
    	
    	ifstream infile;
    	infile.open(filename.data());
    	
    	string name;
    	int wage;
    	
    	while(!infile.eof())
    	{
    	   
    	   	infile>>name;
    	   	infile>>wage;
    	   	cout<<"Name: "<<name<<" Wages: "<<wage<<endl;
    	   	}
    	   	
    	  
    }
    This is what I have for the second part (the wages part).. ..

    But how would I calculate the total of all wages in the file and the average calculated??

    Just so I'm clear the eof funtion is used if the file has an indefinite the amount of integers?

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    13
    Quote Originally Posted by godfrey270487 View Post
    How would I do this?? This is what I have so far.. It's like chinese writing to me..lol

    Code:
    void main()
    {
    		
    	
    	ifstream infile;
    	ofstream outfile;
    	string name;
    	
    	outfile.open("F:\\names2.txt");
    	infile.open("F:\\names1.txt");
    	for (int i=0; i<10; i++)
    	{
    	
    	infile>>name;
    	cout<<name<<endl;
    	outfile<<name<<endl;
    	
    	
    	
    	}cout<<" All data has been copied to names2.txt"<<endl;
    	infile.close();
    	
    	
    	}
    I'm suppose to be asked for the file name but I don't know how to do that
    Would I have to create 2 different variables, maybe calling them file1 ( fiel to fead from) and file2( file to copy to) ?

  8. #8
    Registered User
    Join Date
    Mar 2006
    Posts
    13
    Okay.. I'm now able to copy the file

    but I don;t know how to return it from the function

    Please help What am I doing wrong

    Code:
    #include <fstream>
    #include <iostream>
    
    using namespace std;
    double copyfile(double copy);
    void main()
    {
    int option;
    
    cout<<"Please choose from the following options";
    cout<<" Option 1 = Copy File";
    cout<<" Option 2 = Calculate average employee wage2";
    cin>>option;
    
    if(option == 1)
     {
      
      cout<<""<<copy<<endl;// copy is my return function
      return 0;
      }
      
      else if(option == 2)
      {
      
      
      cout<<""<<endl;
      
      
      }
      
      else if(option != 1 && != 2)
      {
      
      cout<<" Incorrect option, Please enter option 1 or 2"; 
      
      }
      
      
      double copyfile(double copy)
      {
      return copy 
        ifstream infile;
    	ofstream outfile;
    	string name;
    	
    	cout<<"Enter name of file you want to read:";
    	string filename;
    	cin>>filename;
    	
    	cout<<"Enter file to copy to:";
    	string filename1;
    	cin>>filename1;
    	
    	outfile.open(filename1.data());
    	infile.open(filename.data());
    	for (int i=0; i<10; i++)
    	{
    	
    	infile>>name;
    	cout<<name<<endl;
    	outfile<<name<<endl;
    	
    	
    	
    	}cout<<" All data has been copied to "<<endl;
    	infile.close();
    	}}

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    http://www.cprogramming.com/tutorial/lesson4.html

    That's the tutorial on writing and using functions. If you read it I think you'll be able to solve your problem. In fact looking at the tutorial to see if it has the topic you're working on is always a good idea.
    You're only born perfect.

  10. #10
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    As a side note, main returns an int. Scrap void main and use int main() or int main ( void )
    Double Helix STL

  11. #11
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    Code:
     double copyfile(double copy)
      {
      return copy 
    ifstream infile;
    	ofstream outfile;
    huh? You return from the function before you have a chance to do anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM