简体   繁体   中英

How to call a function from a separate .cpp file?

So here is the original question I am trying to solve;

*Create a project titled Lab5_Figures. This project shall contain multiple files. Write a program that repeatedly asks the user to select either square, left or right triangle, then inputs the figure size and then prints the appropriate shape in stars. For square, the program should ask whether the user wants a filled or a hollow square. The program should quit if the user inputs an invalid option. See an example dialog below:

  1. square
  2. bottom left triangle
  3. top right triangle

select figure: 1

select size: 4

filled or hollow [f/h]: h

//print out appropriate figure then repeat

  1. square
  2. bottom left triangle
  3. top right triangle ...

You can reuse your code from the Looping lab (I already did this). Place star-printing code in four separate functions: filledSquare, hollowSquare, leftTriangle and rightTriangle. Each function should accept a single integer parameter - the size of the figure and return no value (be a void-function). Create three separate files figures.cpp, figures.h, and figuresInput.cpp. Place the triangle and square function definitions in figures.cpp and their prototypes in figures.h. Make sure that the header file is protected against multiple inclusion. Place the main function in figuresInput.cpp*.

Okay, cool. Now here are my files; ( I apologize if my formatting is off :( )

figuresInput.cpp

// This program creates shapes based on user input


#include <iostream>
#include <string>
#include "figures.h"

using std::cin; using std::cout; using std::string; using std::endl;

int main()
{
    int option = 1;

while (option == 1 || option == 2 || option == 3)
{
    //determine choice
    cout << "1. Square" << endl << "2. Left Triangle" << endl << "3. Right Triangle" << endl;
    cout << "Select an option: ";

    cin >> option;

    if (option == 1)
    {
        char fillHollow;

        cout << "Filled or hollow? [f/h]";
        cin >> fillHollow;

        if (fillHollow == 'f')
        {
            int size;

            cout << "Input Size: ";
            cin >> size;

            void filledSquare(int size);
        }

        else if (fillHollow = 'h')
        {
            int size;

            cout << "Input Size: ";
            cin >> size;

            void hollowSquare(int size);
        }
    }

    else if (option == 2)
    {
        int size;

        cout << "Input Size: ";
        cin >> size;

        void leftTriangle(int size);
    }

    else if (option == 3)
    {
        int size;

        cout << "Input Size: ";
        cin >> size;

        void rightTriangle(int size);
    }

    else
        exit(EXIT_FAILURE);
}
} //end main

figures.cpp

//function defintions

#include "figures.h"
#include <iostream>

using std::cin; using std::cout; using std::string; using std::endl;

void filledSquare(int a)
{
//print stars for first square
for (int b = 0; b < a; b++)
{
    for (int c = 0; c < a; c++)
        cout << "*";
    cout << endl; //new line
}
cout << endl; //new line

} //end

void hollowSquare(int a)
{
for (int b = 0; b < a; b++)
{
    int spaces = a - 2;
    if (b == 0 || b == (a - 1))
    {
        for (int i = 0; i < a; i++)
            cout << "*";
        cout << endl;  //new line
    }
    else
    {
        cout << "*";
        for (int i = 0; i < spaces; i++)
            cout << " ";
        cout << "*";
        cout << endl;  //new line
    }
}
} //end

void leftTriangle(int a)
{
//get user input and print stars for first triangle
for (int b = a; b < a; b--)
{
    for (int c = 0; c < b; c++)
        cout << "*";
    cout << endl; //new line
}
cout << endl; //new line
} //end

void rightTriangle(int a)
{
//get user input and print stars for second triangle
for (int b = 0; b < a; b++)
{
    int stars = a - b;
    for (int i = 0; i < b; i++)
        cout << " ";
    for (int i = 0; i < stars; i++)
        cout << "*";
    cout << endl; //new line
}
cout << endl; //new line
} //end

and finally figures.h

//funtion prototypes

#ifndef FIGURES_H
#define FIGURES_H

void filledSquare(int);

void hollowSquare(int);

void leftTriangle(int);

void rightTriangle(int);

#endif;

Okay, so I think my problem is that I am not calling the function definitions from main correctly. I'm not sure if I just didn't include something right or what; I would really appreciate any help I could get.

Here is what my output looks like;

1. Square
2. Left Triangle
3. Right Triangle
Select an option: 1
Filled or hollow? [f/h]f
Input Size: 4
1. Square
2. Left Triangle
3. Right Triangle
Select an option:

You are not actually calling the functions, but declaring them.

You call them like this:

hollowSquare(size);

in your example you are not calling filledSquare function but you are declaring it again as long as with the same SIGNATURE and RETURN TYPE. in this scenario the compiler will not complain eg:

void foo(); // ok
void foo(); // ok
void foo(); // ok

all these prototypes above are correct to the compiler as long as they are identical in everything BUT you must provide one definition only because they are the same function's prototype.

void foo(); // ok
     foo(); // error foo differs only in return type which doesn't mean overloading it. this function without return type by default returns int
char foo(); // error foo differs only in return type.

the prototypes above are incorrect because they differ only in return type. to overload a function signatures must differ in number of parameters or type of parameters(at least in one parameters they differ) or the two.

void foo();        // ok first prototype with which the compiler compare the following
int  foo(int);     // ok differs in number and type of parameters
     foo(int&)     // ok differs in number and type of parameters
void foo(int, int) // ok differs in number and type of parameters

int your example you are re-declaring filledSquare as long as it is already declared.

void filledSquare(int size); // ??!! here you are not calling this function but you are declaring it instead.

to call a function just put the name of the function without return type and parenthesis inside which put parameters if it takes any BUT never state the TYPE of parameters.

filledSquare(size); // without return type nor type of parameters

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM