Christian Vokurka Christian Vokurka - 1 month ago 6
C Question

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:

Answer

functions' prototypes are in headers as you did functions definitions are in their source files as you did.

to call these functions you must add this functions' sources to your project otherwise you'll get a link-error

void filledSquare(int size); // ??!! here you are not calling this function but you are declaring it instead. to call it the correct way:

filledSquare(size);