Like Tree2Likes
  • 2 Post By Salem

Please help me - void functions/byref

This is a discussion on Please help me - void functions/byref within the C++ Programming forums, part of the General Programming Boards category; Hello. I need some help with homework. I'm not asking anyone to do it for me, but I need some ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    8

    Please help me - void functions/byref

    Hello. I need some help with homework. I'm not asking anyone to do it for me, but I need some help.

    Basically, I have to create a program with a main function that will call two other functions. The two other functions will be inputFunction and outputFunction. The inputFunction will pull the data from a text file that I've made. The outputFunction will calculate the totals for each category and the averages for each category. I am not allowed to use an array for this.

    my notepad data looks as follows:

    "Day Food Clothing School Entertainment
    Monday 10 20 30 40
    Tuesday 15 21 25 31
    Wednesday 16 22 26 32
    Thursday 17 23 27 33
    Friday 18 24 28 34
    Saturday 19 14 11 13
    Sunday 4 8 9 10"

    The code I've written so far (which seems to be useless) is as follows:

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    
    using namespace std;
    
    
    void inputFunction(ifstream& infile);
    
    
    int main()
    {
    	inputFunction(infile);
    }
    
    
    void inputFunction(ifstream& infile)
    {
    	ifstream infile;
    	infile.open("expenses.txt");
    	
    	string day1, day2, day3, day4, day5, day6, day7, day, food, clothing, school, entertainment;
    	double food1, food2, food3, food4, food5, food6, food7;
    	double clothing1, clothing2, clothing3, clothing4, clothing5, clothing6, clothing7;
    	double school1, school2, school3, school4, school5, school6, school7;
    	double ent1, ent2, ent3, ent4, ent5, ent6, ent7;
    	double totalFood, double totalClothing, double totalSchool, double totalEnt;
    	double averageFood, double averageClothing, double averageSchool, double averageEnt;
    
    
    	ifstream infile;
    	infile >> day >> " " >> food >> " " >> clothing >> " " >> school >> " " >> entertainment;
    	cout << " " << endl;
    	infile >> day1 >> " " >> food1 >> " " >> clothing1 >> " " >> school1 >> " " >> ent1;
    	cout << " " << endl;
    	infile >> day2 >> " " >> food2 >> " " >> clothing2 >> " " >> school2 >> " " >> ent2;
    	cout << " " << endl;
    	infile >> day3 >> " " >> food3 >> " " >> clothing3 >> " " >> school3 >> " " >> ent3;
    	cout << " " << endl;
    	infile >> day4 >> " " >> food4 >> " " >> clothing4 >> " " >> school4 >> " " >> ent4;
    	cout << " " << endl;
    	infile >> day5 >> " " >> food5 >> " " >> clothing5 >> " " >> school5 >> " " >> ent5;
    	cout << " " << endl;
    	infile >> day6 >> " " >> food6 >> " " >> clothing6 >> " " >> school6 >> " " >> ent6;
    	cout << " " << endl;
    	infile >> day7 >> " " >> food7 >> " " >> clothing7 >> " " >> school7 >> " " >> ent7;
    	cout << " " << endl;
    }
    I know it's ugly looking and I apologize for that. I'm getting errors. I don't know what I'm doing and I'm on the verge of switching my major. I'm so frustrated right now. Can someone please help me along the way? I'd appreciate it more than you know.

  2. #2
    Registered User
    Join Date
    Oct 2011
    Posts
    8
    Sorry, I should add that those numbers are to represent dollar amounts that a student spends on each category for each day of the week. I made the data up myself as we are allowed to do that.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    > string day1, day2, day3, day4, day5, day6, day7
    When you start adding numbers on the ends of variables, you should be thinking about

    string days[7];

    and using a loop like
    for ( nDays = 0 ; nDays < 7 ; nDays++ )
    instead of copy/pasting code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    8
    But isn't that an array? We aren't allowed to use arrays. And when I call the function in the main function it tells me that infile is an undefined paramater. If i define it up there then it tells me that it is redefined because i defined it in the inputFunction. I don't know how to fix that part.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    2,919
    You can not use an array, but can you use a structure/class to hold the information?

    Also if you get error messages post the complete error messages exactly as they appear in your development environment.

    One problem I see right away is in main you have never defined the ifstream& you will be passing to your function. In your function you never use the parameter but instead you create a instance of your ifstream.

    Code:
    int main()
    {
        // remove the next two lines from your function and put them here.
        ifstream infile;
        infile.open("expenses.txt");
    
        inputFunction(infile);
    }
    Also in your function you have this line:
    Code:
        double totalFood, double totalClothing, double totalSchool, double totalEnt;
    In this line you are defining your variables, you do not need any of the "double" except the first one. You are doing this is several places.

    Jim

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    Well the first thing to do would be to delete all those >> " " inputs, and then post your new set of error messages.

    > We aren't allowed to use arrays
    ...
    > I don't know what I'm doing and I'm on the verge of switching my major.
    It would seem that your tutor is also in the area of "I don't know what I'm doing" as well.
    Gotta be some kind of bone-head to give an assignment which plainly begs for arrays and then says not to use them.
    jimblumberg and Elysia like this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    8
    Everyone in my class seems to think he (teacher) is a bonehead. We are in Chapter 5 and arrays aren't until chapter 8, so that's why we can't use them. When I took intro to programming I learned arrays in a very basic language, so I understand that would be way easier. But he specifically said not to. We are just now learned void functions and by ref.

    These are the errors I'm getting:

    Code:
    c:\users\Ashley\documents\visual studio 2010\projects\7hw\7hw\7hw.cpp(17): error C2082: redefinition of formal parameter 'infile'c:\users\Ashley\documents\visual studio 2010\projects\7hw\7hw\7hw.cpp(25): error C2062: type 'double' unexpected
    c:\users\Ashley\documents\visual studio 2010\projects\7hw\7hw\7hw.cpp(26): error C2062: type 'double' unexpected
    c:\users\Ashley\documents\visual studio 2010\projects\7hw\7hw\7hw.cpp(28): error C2082: redefinition of formal parameter 'infile'
    c:\users\Ashley\documents\visual studio 2010\projects\7hw\7hw\7hw.cpp(29): error C2678: binary '>>' : no operator found which takes a left-hand operand of type 'std::basic_istream<_Elem,_Traits>' (or there is no acceptable conversion)

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    2,919
    Show your current code, hopefully the code that generated these errors.

    Jim

  9. #9
    Registered User
    Join Date
    Oct 2011
    Posts
    8
    Here's my code after taking out the stuff that Salem asked me to remove:

    Code:
    #include <iostream>#include <fstream>
    #include <string>
    
    
    using namespace std;
    
    
    void inputFunction(ifstream& infile);
    
    
    int main()
    {
    	inputFunction(infile);
    }
    
    
    void inputFunction(ifstream& infile)
    {
    	ifstream infile;
    	infile.open("expenses.txt");
    	
    	string day1, day2, day3, day4, day5, day6, day7, day, food, clothing, school, entertainment;
    	double food1, food2, food3, food4, food5, food6, food7;
    	double clothing1, clothing2, clothing3, clothing4, clothing5, clothing6, clothing7;
    	double school1, school2, school3, school4, school5, school6, school7;
    	double ent1, ent2, ent3, ent4, ent5, ent6, ent7;
    	double totalFood, totalClothing, totalSchool, totalEnt;
    	double averageFood, averageClothing, averageSchool, averageEnt;
    
    
    	ifstream infile;
    	infile >> day >> food >> clothing >> school >> entertainment;
    	cout << " " << endl;
    	infile >> day1 >> food1 >> clothing1 >> school1 >> ent1;
    	cout << " " << endl;
    	infile >> day2 >> food2 >> clothing2 >> school2 >> ent2;
    	cout << " " << endl;
    	infile >> day3 >> food3 >> clothing3 >> school3 >> ent3;
    	cout << " " << endl;
    	infile >> day4 >> food4 >> clothing4 >> school4 >> ent4;
    	cout << " " << endl;
    	infile >> day5 >> " " >> food5 >> " " >> clothing5 >> " " >> school5 >> " " >> ent5;
    	cout << " " << endl;
    	infile >> day6 >> " " >> food6 >> " " >> clothing6 >> " " >> school6 >> " " >> ent6;
    	cout << " " << endl;
    	infile >> day7 >> " " >> food7 >> " " >> clothing7 >> " " >> school7 >> " " >> ent7;
    	cout << " " << endl;
    }
    and the error messages:

    c:\users\Ashley\documents\visual studio 2010\projects\7hw\7hw\7hw.cpp(11): error C2065: 'infile' : undeclared identifier
    c:\users\Ashley\documents\visual studio 2010\projects\7hw\7hw\7hw.cpp(16): error C2082: redefinition of formal parameter 'infile'
    c:\users\Ashley\documents\visual studio 2010\projects\7hw\7hw\7hw.cpp(27): error C2082: redefinition of formal parameter 'infile'
    c:\users\Ashley\documents\visual studio 2010\projects\7hw\7hw\7hw.cpp(38): error C2678: binary '>>' : no operator found which takes a left-hand operand of type 'std::basic_istream<_Elem,_Traits>' (or there is no acceptable conversion)

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    2,919
    For the first three errors please re-read post #5. Also you will need to remove line #31.

    For the last error re-read post #6

    Jim

  11. #11
    Registered User
    Join Date
    Oct 2011
    Posts
    8
    Okay so I think I made the right changes to my code:

    Code:
    #include <iostream>#include <fstream>
    #include <string>
    
    
    using namespace std;
    
    
    void inputFunction(ifstream& infile);
    
    
    int main()
    {
    	ifstream(infile);
    	infile.open("expenses.txt");
    	inputFunction(infile);
    }
    
    
    void inputFunction(ifstream& infile)
    {
    	
    	
    	string day1, day2, day3, day4, day5, day6, day7, day, food, clothing, school, entertainment;
    	double food1, food2, food3, food4, food5, food6, food7;
    	double clothing1, clothing2, clothing3, clothing4, clothing5, clothing6, clothing7;
    	double school1, school2, school3, school4, school5, school6, school7;
    	double ent1, ent2, ent3, ent4, ent5, ent6, ent7;
    	double totalFood, totalClothing, totalSchool, totalEnt;
    	double averageFood, averageClothing, averageSchool, averageEnt;
    
    
    	ifstream infile;
    	infile >> day >> food >> clothing >> school >> entertainment;
    	cout << " " << endl;
    	infile >> day1 >> food1 >> clothing1 >> school1 >> ent1;
    	cout << " " << endl;
    	infile >> day2 >> food2 >> clothing2 >> school2 >> ent2;
    	cout << " " << endl;
    	infile >> day3 >> food3 >> clothing3 >> school3 >> ent3;
    	cout << " " << endl;
    	infile >> day4 >> food4 >> clothing4 >> school4 >> ent4;
    	cout << " " << endl;
    	infile >> day5 >> food5 >> clothing5 >> school5 >> ent5;
    	cout << " " << endl;
    	infile >> day6 >> food6 >> clothing6 >> school6 >> ent6;
    	cout << " " << endl;
    	infile >> day7 >> food7 >> clothing7 >> school7 >> ent7;
    	cout << " " << endl;
    }
    But I'm still getting this one error:

    ------ Build started: Project: 7hw, Configuration: Debug Win32 ------
    7hw.cpp
    c:\users\Ashley\documents\visual studio 2010\projects\7hw\7hw\7hw.cpp(28): error C2082: redefinition of formal parameter 'infile'
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

  12. #12
    Registered User
    Join Date
    Oct 2011
    Posts
    8
    I also just took out the "ifstream infile;" out of the inputFunction and I added "system("PAUSE"); return 0;" to the bottom of the main function.

    the program now runs but it tells me I have a bunch of unreferenced variables now and no output actually shows up in the program. It's just a blank screen.

    Am I just an idiot?

  13. #13
    Registered User
    Join Date
    May 2010
    Posts
    2,919
    the program now runs but it tells me I have a bunch of unreferenced variables now and no output actually shows up in the program. It's just a blank screen.
    You have several "unused" variables because you have not used them yet (your totals and averages). It does not print any thing because you have not told it to print anything.

    Try adding the following line to your inputFunction() after all of your file reads.

    Code:
    cout << day <<  " " <<  food << " " <<  clothing << " " <<  school << " " <<  entertainment << endl;
    Jim

  14. #14
    Registered User
    Join Date
    Oct 2011
    Posts
    8
    Wow, I knew that. Now I'm getting output. I've been so frustrated I'm just not thinking straight.

    Thanks a lot for your help, Jim. I will try and take it from here and see if I can get it done now. If I can't I'll be bothering you all some more. Again, I really do appreciate you taking the time to help me go through this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using void functions
    By amie001 in forum C Programming
    Replies: 2
    Last Post: 06-07-2011, 12:42 AM
  2. C void functions-- help please?
    By shane981 in forum C Programming
    Replies: 3
    Last Post: 03-28-2008, 08:07 AM
  3. Void Functions
    By MyntiFresh in forum C++ Programming
    Replies: 10
    Last Post: 07-13-2005, 07:47 AM
  4. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 06:30 PM
  5. Void functions
    By cap in forum C Programming
    Replies: 16
    Last Post: 09-04-2002, 09:08 AM

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