1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

C++ and XCode Help

Discussion in 'Mac Programming' started by FireflyMal, Mar 19, 2012.

  1. macrumors newbie

    #1
    Hello,

    I am new to programming and am taking an intro class. I am having trouble with XCode and getting my code to compile with multiple files. I am using headers, but I keep receiving the following errors.

    On consoleUI.cpp, I get 2 errors:
    1. No previous prototype for function "consoleUI"
    2. No matching function for call "PaintJobEstimator"

    On PaintJobEstimator.cpp, I get 1 error:
    1. No previous prototype for function "PaintJobEstimator"


    If anyone could help, I would be grateful. Thank you. Below is my code. I can't even compile to test if the logic works.


    main.cpp:
    Code:
    #include <iostream>
    
    // Include Header file
    #include "consoleUI.h"
    
    using namespace std;
    
        
    void consoleUI();
    
    int main()
    {
        
        consoleUI();
        
        return 0;
    }
    
    consoleUI.cpp:
    Code:
    #include <iostream>
    #include <iomanip>
    
    // Include Header File
    #include "PaintJobEstimator.h"
    
    
    using namespace std;
    
    
    
    // Begin Console UI
    void consoleUI ()
    {
            
        int rooms = 0;              // Number of Rooms
        int cnt = 0;                // Counter for rooms
        double paint = 0.00;        // Number of gallons of paint
        double ceilPaint = 0.00;    // Paint rounded up
        double labor = 0.00;        // Labor hours needed
        double sqft = 0.00;         // Square footage of wall
        double laborCost = 0.00;    // Cost of labor
        double paintCost = 0.00;    // Cost of paint
        double priceOfPaint = 0.00; // Price of paint * paintCost
        double totalCost = 0.00;    // Total Cost
        bool isValid = false;       // Loop validation
    
        
        // Input for rooms
        cout<<"How many rooms would you like to paint?\n";
        cin>>rooms;
        cout<<"You entered: "<<rooms <<" rooms.\n";
        
        // Input for Paint Cost
        cout<<"What is the price of paint per gallon?\n";
        cin>>paintCost;
        
        
        // Input validation loop
        while (! isValid)
        {
            
            // Loop for calculation of each room
            for (cnt=0; cnt<=rooms; cnt--)
            {
                
                if (rooms > 0 && rooms <= 25)
                {
                    cout<<"What is the total square footage for the walls of the entire room?\n";
                    cin>>sqft;
                    
                    sqft += sqft;
                        
                    
                    if (sqft > 0.00)
                    {                    
                        if (paintCost >= 10.00)
                        {
                            isValid = true;
                        }                    
                    }                
                } 
                
                else
                {
                    cout<<"Invalid entry.\n";
                }
                
            }
                        
        }
        
        // Call function to compute costs
        PaintJobEstimator (paint, labor, sqft, paintCost, laborCost, priceOfPaint); 
        
        
        // Output Costs    
        cout << fixed << setprecision(2);
        cout <<"Number of gallons of paint: " << ceilPaint <<endl;
        cout <<"Hours of labor: " << labor <<endl;
        cout <<"Cost of paint: $"<< setw(7) << paintCost <<endl;
        cout <<"Labor charge: $"<< setw(7) << laborCost <<endl;
        cout <<"Total cost: $"<< setw(7) << totalCost <<endl;
        
        
    }
    
    
    
    PaintJobEstimator.cpp:
    Code:
    #include <stdio.h>
    #include <math.h>
    #include <cmath>
    
    double PaintJobEstimator (double& ceilPaint, double& paint, double& labor, double& sqft, double& paintCost, double& laborCost, double& priceOfPaint, double& totalCost)
    {
        // Ceiling Function for gallons of paint    
        ceilPaint = sqft/115;
        paint = ceil (ceilPaint);
        
        // Cost of labor, paint & total costs
        labor = ((sqft/115) * 8);
        laborCost = labor * 18.00;
        priceOfPaint = paint * paintCost;
        totalCost = laborCost + priceOfPaint;
        
        return 0;
    }
    
    consoleUI.h:
    Code:
    #ifndef Paint_Job_Estimator_consoleUI_h
    #define Paint_Job_Estimator_consoleUI_h
    
    void consoleUI ();
    
    #endif
    
    PaintJobEstimator.h:
    Code:
    #ifndef Paint_Job_Estimator_PaintJobEstimator_h
    #define Paint_Job_Estimator_PaintJobEstimator_h
    
    double PaintJobEstimator (double& ceilPaint, double& paint, double& labor, double& sqft, double& paintCost, double& laborCost, double& priceOfPaint, double& totalCost);
    
    #endif
    
     
  2. macrumors G5

    gnasher729

    #2
    A source file like PaintJobEstimator.cpp would include PaintJobEstimator.h unless you have a very very good reason not to. Same for consoleUI.cpp and consoleUI.h.

    Next, compare the call to PaintJobEstimator and the actual function very, very carefully.
     
  3. macrumors newbie

    #3
    Thank you! Doing the #include worked for the errors. Now I have to fix my code. There's an infinite loop in there, and no check for invalid entries for square footage.

    I also guess I don't have an understanding of references as I thought.

    Gnasher, I appreciate your help. Thank you!
     
  4. macrumors G5

    gnasher729

    #4
    This line

    Code:
    for (cnt=0; cnt<=rooms; cnt--)
    looks just wrong.
     
  5. macrumors 604

    chrono1081

    #5
    So does this function:

    Code:
    double PaintJobEstimator (double& ceilPaint, double& paint, double& labor, double& sqft, double& paintCost, double& laborCost, double& priceOfPaint, double& totalCost)
    {
        // Ceiling Function for gallons of paint    
        ceilPaint = sqft/115;
        paint = ceil (ceilPaint);
        
        // Cost of labor, paint & total costs
        labor = ((sqft/115) * 8);
        laborCost = labor * 18.00;
        priceOfPaint = paint * paintCost;
        totalCost = laborCost + priceOfPaint;
        
        [B]return 0;[/B]
    }
    
    The function is set up as a value returning function, but in your code you call it like a void function with this line:

    Code:
      // Call function to compute costs
        PaintJobEstimator (paint, labor, sqft, paintCost, laborCost, priceOfPaint); 
    Honestly I'm not sure how it compiles :confused::confused::confused:


    EDIT: Ooops I see Gnasher already mentioned it in a previous post :D
     
  6. macrumors newbie

    #6
    Ok, I think I've got it, but I am having trouble with compounding the values entered for sqft. For some reason, it is not compounding it correctly. I'm not exactly sure what it's doing.

    Again, thanks for the help everyone. It's awesome to have a Mac programming community that is helpful for beginners like myself!

    main.cpp:
    Code:
    #include <iostream>
    
    // Include Header file
    #include "consoleUI.h"
    
        
    void consoleUI();
    
    int main()
    {
        
        consoleUI();
        
        return 0;
    }
    
    consoleUI.cpp
    Code:
    #include <iostream>
    #include <iomanip>
    
    // Include Header File
    #include "PaintJobEstimator.h"
    #include "consoleUI.h"
    
    using namespace std;
    
    
    
    // Begin Console UI
    void consoleUI ()
    {
            
        int rooms = 0;              // Number of Rooms
        int cnt = 0;                // Counter for rooms
        double ceilPaint = 0.00;    // Number of gallons of paint rounded up
        double labor = 0.00;        // Labor hours needed
        double sqft = 0.00;         // Square footage of wall
        double laborCost = 0.00;    // Cost of labor
        double paintCost = 0.00;    // Cost of paint
        double priceOfPaint = 0.00; // Price of paint * paintCost
        double totalCost = 0.00;    // Total Cost
        bool isValid = false;       // Loop validation
        bool sqftValid = false;     // Loop validation for square footage
        bool paintValid = false;    // Loop validation for paint cost
    
        
        
        // Input validation loop
        while (! isValid)
        {
            
            // Input for rooms
            cout<<"How many rooms would you like to paint?\n";
            cin>>rooms;
            cout<<"You entered: "<<rooms <<" rooms.\n\n";
            
                if (rooms > 0)
                {    
                    // Validation loop for paint cost
                    while (!paintValid)
                    {
                       
                        // Input for Paint Cost
                        cout<<"What is the price of paint per gallon?\n";
                        cin>>paintCost;
                        cout<<"You entered " <<paintCost <<"\n\n";
            
                            // Control Statement to determine if for loop will run
                            if (paintCost >= 10.00)
                            {
                                paintValid = true;
                                isValid = true;
                                
                                    // Square footage validation loop
                                    while (! sqftValid)
                                    {
                                        cout<<"What is the total square footage for the walls of the entire room?\n";
                                        cin>>sqft;
                                    
                                            if (sqft > 0.00)
                                            {                     
                                                sqftValid = true;                 
                                    
                                    
                                                // Loop for calculation of each room
                                                for (cnt=(rooms-1); cnt>0; cnt--)
                                                {
                                                    cout<<"For the next room, what is the total square footage for the walls of the entire room?\n";
                                                    cin>>sqft;
                                                    sqft += sqft;
                                                }
                                            }
                                            else
                                            {
                                                cout <<"That is not a valid entry for square footage. Please enter a valid square footage.\n";
                                            }
                                    }
                            
                            }
                            else
                            {
                                cout <<"Invalid entry for cost of paint. Must be higher than $10\n\n";
                            }
                    }
    
                            
                    
                }
                else
                {            
                    cout<<"Invalid Entry for rooms.\n";
                }                    
        }
        
        // Call function to compute costs and output Costs 
        PaintJobEstimator(ceilPaint, labor, sqft, paintCost, laborCost, priceOfPaint, totalCost);
        cout << fixed << setprecision(2);
        cout <<"Gallons of paint:   " << setw(7) << ceilPaint <<endl;
        cout <<"Hours of labor:     " << setw(7) << labor <<endl;
        cout <<"Cost of paint:     $" << setw(7) << priceOfPaint <<endl;
        cout <<"Labor charge:      $" << setw(7) << laborCost <<endl;
        cout <<"Total cost:        $" << setw(7) << totalCost <<endl;
        
        
    }
    
    PaintJobEstimator.cpp
    Code:
    #include <stdio.h>
    #include <math.h>
    #include <cmath>
    #include "PaintJobEstimator.h"
    
    double PaintJobEstimator (double& ceilPaint, double& labor, double& sqft, double& paintCost, double& laborCost, double& priceOfPaint, double& totalCost)
    {
        // Ceiling Function to round up for gallons of paint
        ceilPaint = sqft/115;
        ceilPaint = ceil (ceilPaint);
        
        // Cost of labor, paint & total costs
        labor = ((sqft/115) * 8);
        laborCost = labor * 18.00;
        priceOfPaint = ceilPaint * paintCost;
        totalCost = laborCost + priceOfPaint;
        
        return 0;
    }
    
    consoleUI.h
    Code:
    #ifndef Paint_Job_Estimator_consoleUI_h
    #define Paint_Job_Estimator_consoleUI_h
    
    void consoleUI ();
    
    #endif
    
    PaintJobEstimator.h
    Code:
    #ifndef Paint_Job_Estimator_PaintJobEstimator_h
    #define Paint_Job_Estimator_PaintJobEstimator_h
    
    double PaintJobEstimator (double& ceilPaint, double& labor, double& sqft, double& paintCost, double& laborCost, double& priceOfPaint, double& totalCost);
    
    #endif
    


    ----------

    My thought process here was to call the function at the end to perform all of the math. Then, I would output the results for the user to see. The code compiles, however I have an issue with compounding the sqft. It's not compounding correctly.
     
  7. macrumors 603

    #7
    If by "compounding" you mean the red-hilited code, I urge you to walk through this loop manually, using pencil and paper if necessary. Show your work at each iteration. That is, write out the values of the affected variables at each statement of each iteration. Is this what you expected to happen?

    Or learn to use the debugger to step through loops like this, observing variables.

    The important thing is that you visualize what actually happens. Not what you thought would happen when you wrote it, but what actually happens when the algorithm executes. This loop is simple enough that you ought to be able to do it manually. An experienced programmer should be able to visualize this loop in their head. You will too after you see what's wrong with it.
     
  8. chrono1081, Mar 19, 2012
    Last edited: Mar 19, 2012

    macrumors 604

    chrono1081

    #8
    Well, first lets address the function mentioned. Apologies in advance if you understand the concept but just in case you don't here it is:

    There are two types of functions, void functions and value returning functions.

    A void function will look like this:

    Code:
    void AddNumbers(int a, int b)
    {
    
    int c = a + b;
    cout << "The numbers added are: "<< c << endl;
    }
    
    To call this function you would just simply say:

    Code:
    AddNumbers(4, 5);
    and the console would output 9 because the void function is printing straight to the console and not relying on another part of the program to do that for it.

    Now, your function is declared as a value returning function which looks similar this:

    Code:
    double AddNumbers(double a, double b)
    {
    return a + b; //Your function has 0 instead of a value
    }
    
    This means your function is expected to return a value of type "double".

    To use it, you have to use it in a way that the result can be used (or in your case, printed out to the screen).

    Something like this would work:

    Code:
    cout << "10 + 5 = " << AddNumbers(10,5) << endl;
    
    //Note: Its generally good programming practice to write a decimal after floats and doubles for clarity, like
    //AddNumbers(10.0, 5.0) but I omitted them.
    
    You could also do it like this:

    Code:
    double c = AddNumbers(10,5);
    cout << "c = " << c << endl;
    

    Notice how in your code you have this function:

    Code:
    double PaintJobEstimator (double& ceilPaint, double& labor, double& sqft, double& paintCost, double& laborCost, double& priceOfPaint, double& totalCost)
    {
        // Ceiling Function to round up for gallons of paint
        ceilPaint = sqft/115;
        ceilPaint = ceil (ceilPaint);
        
        // Cost of labor, paint & total costs
        labor = ((sqft/115) * 8);
        laborCost = labor * 18.00;
        priceOfPaint = ceilPaint * paintCost;
        totalCost = laborCost + priceOfPaint;
        
        return 0; //This should return totalCost, not zero. 
    }
    
    You declared the function as type double, meaning its a value returning function and should return a double. While 0 is a perfectly valid value to return its not going to give you the result you wanted.

    When you call this function in this line:

    Code:
    PaintJobEstimator(ceilPaint, labor, sqft, paintCost, laborCost, priceOfPaint, totalCost);
    It doesn't do anything because there is no where for it to output its results. If you want to show the total cost on the screen, do something like this:

    Code:
    cout << "This is the total cost of the paint job: " << PaintJobEstimator(ceilPaint, labor, sqft, paintCost, laborCost, priceOfPaint, totalCost) << endl;
    
    I know that was wordy and lengthy but do you understand the difference between void and value returning functions? If not maybe someone can explain better than I can. It's insanely important that you grasp this concept early on.
     
  9. macrumors newbie

    #9
    Thank you for telling me to write this out. After doing this, I found my error. That helped me visualize it step by step.

    Here is the fixed code. It is now working as intended!
    Code:
    // Square footage validation loop
                                    while (! sqftValid)
                                    {
                                        cout<<"What is the total square footage for the walls of the entire room?\n";
                                        cin>>sqft;
                                        totalSqft = totalSqft + sqft;
                                        
                                            if (sqft > 0.00)
                                            {                     
                                                sqftValid = true;                 
                                    
                                    
                                                // Loop for calculation of each room
                                                for (cnt=(rooms-1); cnt>0; cnt--)
                                                {
                                                    cout<<"For the next room, what is the total square footage for the walls of the entire room?\n";
                                                    cin>>sqft;
                                                    totalSqft = totalSqft + sqft;
                                                }
    
    


    ----------

    Ok, I thought I had understood this, but you clarified this for me a bit better. Since I am only using this function to perform math and don't care about this function returning a value, I have now set it to void. It now works exactly as I want it to.

    My new code is:
    Code:
    #include <stdio.h>
    #include <math.h>
    #include <cmath>
    #include "PaintJobEstimator.h"
    
    void PaintJobEstimator (double& ceilPaint, double& labor, double& totalSqft, double& paintCost, double& laborCost, double& priceOfPaint, double& totalCost)
    {
        // Ceiling Function to round up for gallons of paint
        ceilPaint = totalSqft/115;
        ceilPaint = ceil (ceilPaint);
        
        // Cost of labor, paint & total costs
        labor = ((totalSqft/115) * 8);
        laborCost = labor * 18.00;
        priceOfPaint = ceilPaint * paintCost;
        totalCost = laborCost + priceOfPaint;
        
    }
    

    Thanks everyone for the help with this! I'm still very new to this (first semester of programming).
     
  10. macrumors 604

    chrono1081

    #10
    Oh crap I didn't catch that you were using references.

    No matter though, its better as a void function since having a double returning 0 is kind of confusing to anyone using the function since they can't use it as part of an equation or something.

    Also great news that it is now working for you. One thing to definitely learn early on (if you don't know how to do it already) is to learn to use the debugger to check variable values. This will help you so much now and later down the road.

    I remember not knowing this was even possible until my second year of my first (of three) colleges!
     
  11. lloyddean, Mar 19, 2012
    Last edited: Mar 19, 2012

    macrumors 6502a

    #11
    Your calculation loop can be expressed less awkwardly with.

    Code:
    // Loop for calculation of each room
    for ( cnt = rooms; cnt--; )
    
     
  12. macrumors newbie

    #12
    Just tried this, and when I enter 4 rooms, it asks me for 5 square footage values for rooms.

    ----------

    I'm going to have to check this out in Xcode. Thanks for the help and tips!
     
  13. macrumors 604

    chrono1081

    #13
    Gimme a few minutes and I'll make a video to show you how. Its super easy.
     
  14. macrumors newbie

    #14
    You know, I tried this and deleted all of the reference variables, and everything returned 0. I think I need these reference variables for this to work.
     
  15. macrumors 6502a

    #15
    And how many times is "Hello!" printed?

    Code:
    #include <iostream>
    
    int main()
    {
        const size_t    room = 4;
        
        for ( int i = room; i--; )
        {
            std::cout << "Hello!\n";
        }
    
        return EXIT_SUCCESS;
    }
     
  16. macrumors 604

    chrono1081

    #16
    No you don't want to delete the reference variables, those stay. You just don't want a "return 0" or a "double" in front of the variable name since its a void function.

    You'll want it to look like this:

    Code:
    void addFive(int &a, int &b)
    {
         a += 5;
         b += 5;
    }
    
    Its still a void function, but the references to the variables allow them to be changed.

    Take this piece of code for example:

    Code:
    #include <iostream>
    
    using std::cout;
    using std::string;
    using std::endl;
    
    void addFive(int &, int &);
    
    int main (int argc, const char * argv[])
    {
    
        
        int a = 5;
        int b = 10;
        
        cout << "Before addFive function: a = " << a << " b = " << b << endl;
        
        addFive(a, b);
        
        cout << "After addFive function: a = " << a << " b = " << b << endl;
        
        return 0;
    }
    
    void addFive(int &a, int &b)
    {
        a += 5;
        b += 5;
    }
    
    
    This is essentially how you want your function to work.
     
  17. macrumors 68040

    #17
    The idiomatic way of doing that would be:

    Code:
    for(cnt = 0; cnt < rooms; cnt++)
    
     
  18. macrumors newbie

    #18
    Have to run to my Calc 1 class. I'll try this when I get back home. Did you try compiling and running my code with your change to the counter by any chance? I did try your way and it asked me for square footage 5 times when I entered 4.

    I believe the issue is that I ask for the initial square footage first. That first square footage question is asked outside the loop. In order for the loop to return the correct amount, I need to have (rooms - 1) to omit the square footage outside the loop.
     
  19. macrumors 6502a

    #19
    Yes, but not if you're writing for performance or easier to read or shorter code.
     
  20. macrumors 68040

    #20
    Performance? Give me a break, the example you proposed have a: initialization part, evaluation part and decrement part, thus the same.

    The idea behind the idiom is that it is easier to read code if all loops that does the same job, always looks the same.
     
  21. macrumors 6502a

    #21
    Idioms are only useful if they are not awkward, hard to read or take more effort to understand.

    We're going to disagree here. But that's no reason not to introduced it.
     
  22. macrumors 68040

    #22
    Absolutely, which is why I suggested the most typical of for loops. I only commented on this since you suggested a less awkward way of doing this btw. Never mind.
     
  23. macrumors 604

    chrono1081

    #23
    Ok OP, Youtube is being stupid and compressing my video for some reason but I hope you can tell whats going on via the slides.

    Here is a simple explanation of how to use XCodes debugger:

    http://www.youtube.com/watch?v=9HMRUBRENeg
     
  24. macrumors newbie

    #24
    Thanks again Chrono. Will have to try that. Bookmarked your vid.
     

Share This Page