Figures using multiple files

Working on a figures assignment using multiple files and i keep getting int errors
The program should allow for the choice of the shape you want and to choose the size of it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// Header File figures.h
#ifndef FIGURES_H
#define FIGURES_H

#include <iostream>
using std::cout; using std::cin; using std::endl;

void filledsquare(int , int , int );

void hollowsquare(int , int , int );

void diagonalline(int , int , int );

void bottomtriangle(int , int , int );

void toptriangle(int , int , int );



#endif FIGURES_H



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
 
//figures.cpp file
#include "figures.h"
#include <iostream>
using std::cout; using std::cin; using std::endl;
int rows, i, j, space, c, input, f, h;

void filledsquare(int i, int j, int rows) {
	for (i = 1; i <= rows; ++i)
	{
		for (j = 1; j <= rows; ++j)
			cout << "+";
		cout << endl;

	}
	cout << endl;
}

void hollowsquare(int i, int j, int rows) {
	for (i = 1; i <= rows; ++i)
	{
		if (i == 1 || i == rows)
			for (j = 1; j <= rows; ++j)
				cout << "+";
		else
			for (j = 1; j <= rows; ++j)
				if (j == 1 || j == rows)
					cout << "+";
				else
					cout << " ";
		cout << endl;
	}
}

void diagonalline(int i, int j, int rows) {
	for (int i = 1; i <= rows; ++i) {
		for (int j = 1; j <= rows; ++j) {
			if (i == j)
				cout << "+";
			else
				cout << " ";
		}
		cout << endl;
	}
}

void bottomtriangle(int i, int j, int rows) {
	for (i = rows; i >= 1; --i)
	{
		for (space = 0; space<rows - i; ++space)
			cout << " ";
		for (j = i; j <= 2 * i - 1; ++j)
			cout << "+";
		cout << endl;
	}
}

void toptriangle(int i, int j, int rows) {
	for (int i = 1; i <= rows; ++i)
	{
		for (int j = 1; j <= i; ++j)
		{
			cout << "+";
		}
		cout << endl;
	}
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//figuresinput.cpp file
#include <iostream>
#include "figures.h"
using std::cout; using std::cin; using std::endl;
int main() {
	int input, rows, f, h, c;
	cout << "What shape do you want to make? " << endl;
	cout << "1. Square " << endl;
	cout << "2. Diagonal Line " << endl;
	cout << "3. Bottom Triangle " << endl;
	cout << "4. Top Triangle " << endl;
	cin >> c;
	cout << "Enter the size of the shape:" << endl;
	cin >> rows;
	if (c = 1) {
		cout << "Did you want the square to be filled or hollow? [f/h]: " << endl;
		cin >> input;
		if (input = f) {
			filledsquare(int i, int j, int rows);
		}
		else hollowsquare(int i, int j, int rows);
			
	}
	if (c = 2) {
		diagonalline(int i, int j, int rows);
	}
	if (c = 3) {
		bottomtriangle(int i, int j, int rows);
	}
	if (c = 4) {
		toptriangle(int i, int j, int rows);
	}
	return 0;
}
Last edited on
Hello urawrath,

In what file(s) are you getting the error messages for. Post the actual error messages.

In "figures.h" do not include "iostream". this is better used in a ".cpp" file.

In the "figures.cpp" file try reversing lines 3 and 4. This way "iostream" will cover anything that is in the header file.

This may solve your problem, If not post the error message(s) to help track down where the problem is and what it is.

Hope that helps,

Andy
Hello urawrath,

Loaded up your program as is and compiled it. This is what I found.

In "main":
filledsquare(int i, int j, int rows); As a proto type this is fine. Remove the ; press Enter and add a set of {}s and this makes a function. But as a function call the type specifier is not needed here just the name of the variable.

When I removed the "int"s from the function calls I had the problem that "i" and "j" are undefined variables. Did you mean to use something else here or do you need to define "i" and "j" and give them a value before they are used.

Hope that helps,

Andy
Tried adding the {} to the lines and now its says I am missing a ;.
Also for
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 if (input = f) {
			filledsquare(int i, int j, int rows)
			{}
		}
		else hollowsquare(int i, int j, int rows) 
		{}
			
	}
	if (c = 2) {
		diagonalline(int i, int j, int rows) {}
	}
	if (c = 3) {
		bottomtriangle(int i, int j, int rows) {}
	}
	if (c = 4) {
		toptriangle(int i, int j, int rows) {}
	}
	return 0;

int i is "unexpected" while int j and int rows are fine
Also I don't know if this helps but here is the original code I made last month that has to be put in to multiple files.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
using std::cout; using std::cin; using std::endl;

int main() {
	int rows, i, j, space;
	cout << "Enter a real number:";
	cin >> rows;
	//square
	for (i = 1; i <= rows; ++i)
	{
		for (j = 1; j <= rows; ++j)
			cout << "+";
		cout << endl;
		
	}
	cout << endl;
	//diagonal line
	for (int i = 1; i <= rows; ++i) {
		for (int j = 1; j <= rows; ++j) {
			if (i == j)
				cout << "+";
			else
				cout << " ";
		}
		cout << endl;
	}
	cout << endl;
	//first triangle
	for (int i = 1; i <= rows; ++i)
	{
		for (int j = 1; j <= i; ++j)
		{
			cout << "+";
		}
		cout << endl;
	}
	cout << endl;
	//inverse
	for (i = rows; i >= 1; --i)
	{
		for (space = 0; space<rows - i; ++space)
			cout << " ";
		for (j = i; j <= 2 * i - 1; ++j)
			cout << "+";
		cout << endl;
	}
	cout << endl;
	//hollow box
	for (i = 1; i <= rows; ++i)
	{
		if (i == 1 || i == rows)
			for (j = 1; j <= rows; ++j)
				cout << "+";
		else
			for (j = 1; j <= rows; ++j)
				if (j == 1 || j == rows)
					cout << "+";
				else
					cout << " ";
		cout << endl;
	}
	return 0;
}
Hello urawrath,

I believe you have misunderstood what I was saying. This should give you a better idea:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//figuresinput.cpp file
#include <iostream>
#include <cctype>  // <--- Used to std::tolower() and std::toupper().

#include "figures.hpp"

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

int main()
{
	// <--- "h", "c", "i" and "j" are easy, but you should come up with a better name to describe
	// what they are or do. It makes the code easier to follow.
	int rows, h, c/*, i{ 5 }, j{ 10 }*/;  // <--- "h" is unued. Defined "i" and "j" and gave them a
	                                  // value for testing. Later figured out they are not needed.
	char input{}, f{ 'f' };  // <--- Changed these two to "char"s. Initialized variable "f" to "f"
	                         // so there would be something to compare "input" to.

	cout << "What shape do you want to make? " << endl;
	cout << "1. Square " << endl;
	cout << "2. Diagonal Line " << endl;
	cout << "3. Bottom Triangle " << endl;
	cout << "4. Top Triangle " << endl;
	cin >> c;

	cout << "Enter the size of the shape:" << endl;
	cin >> rows;

	if (c == 1)  // <--- Changed. The "=" sets "c" equal to 1 Which evaluates to true. The "==" compairs "c" to 1.
	{
		cout << "Did you want the square to be filled or hollow? [f/h]: " << endl;
		cin >> input;

		if (std::tolower(input) == f)  // <--- Changed. Added the "std::tolower(...)".
		{  // <--- The {}s are not needed for one line unless you plan to add more later.
			filledsquare(/*i, j,*/ rows);  // <--- All you really need to send here is "rows".
		}  // <--- The {}s are not needed for one line unless you plan to add more later.
		else 
			hollowsquare(/*i, j,*/ rows);  // <--- Easier to read if on its own line.
	}

	if (c == 2)  // <--- Changed.
	{
		diagonalline(/*i, j,*/ rows);
	}

	if (c == 3)  // <--- Changed.
	{
		bottomtriangle(/*i, j,*/ rows);
	}

	if (c == 4)  // <--- Changed.
	{
		toptriangle(/*i, j,*/ rows);
	}

	// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
	std::cout << "\n\n Press Enter to continue";
	std::cin.get();

	return 0;
}

The comments in the program should explain most of what has changed. Any questions let me know.

After working with the program lines like: filledsquare(i, j, rows); the "i" and "j" are not needed to be sent to the functions.

In the "figures.cpp" file:

The global variables are not needed. By sending each function "i" and "j" they over shadow the global variables.

As an example:
1
2
3
4
5
6
7
8
9
10
11
12
13
//int rows, i, j, space, c, input, f, h;  // <--- Not needed here. Most of these should be defined in the functions. Specifically in the for loops.

void filledsquare(/*int i, int j,*/ int rows)
{
	for (int i = 1; i <= rows; ++i)  // <--- Changed. Added type "int". Best not to start at 1, but zero.
	{
		for (int j = 1; j <= rows; ++j)  // <--- Changed.
			cout << "+ ";  // <--- Changed. Added a space so the output will look like a square.
		cout << endl;

	}
	cout << endl;
}

The "int"s "i" and "j" are local variables and are used in the function's for loops. Not the global variables or the variable defined in the function. In the function definition they are commented out because if you write the for loops correctly they are not needed. Unless you need variables "i" and "j" outside the for loop it is best to define them in the for loop. It is also better to define them in the function and not as global where the other function could change them.

The global variables "c", "input", "f" and "h" are never used in the "figures.cpp" file.

I also noticed that most of the for loops start at 1. A better habit to get into is to start for loops at zero. Arrays, std::strings, vectors, lists and other containers are zero based and start their indexing at zero. Starting a for loop at 1 will miss the first element. Better to start a for loop at zero and check for < something.

Hope that helps,

Andy

Edit:
Last edited on
Majority of the changes have been made but now for each function I get an Error - Code C2660 function does not take 1 argument.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//figuresinput.cpp
#include <iostream>
#include "figures.h"
using std::cout; using std::cin; using std::endl;
int main() {
	int rows c;
	char input{}, f{ 'f' };
	cout << "What shape do you want to make? " << endl;
	cout << "1. Square " << endl;
	cout << "2. Diagonal Line " << endl;
	cout << "3. Bottom Triangle " << endl;
	cout << "4. Top Triangle " << endl;
	cin >> c;
	cout << "Enter the size of the shape:" << endl;
	cin >> rows;
	if (c == 1) {
		cout << "Did you want the square to be filled or hollow? [f/h]: " << endl;
		cin >> input;
		if (input == f)
			filledsquare(rows);

		else hollowsquare(rows);
	}
	if (c == 2)
		diagonalline(rows);
	
	if (c == 3)
		bottomtriangle(rows);
	
	if (c == 4)
		toptriangle(rows);
	cout << "Press Enter to continue" << endl;
	cin.get();

	return 0;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//figures.cpp
#include <iostream>
#include "figures.h"

using std::cout; using std::cin; using std::endl;
void filledsquare(int rows) {
	for (int i = 1; i <= rows; ++i)
	{
		for (int j = 1; j <= rows; ++j)
			cout << "+ ";
		cout << endl;

	}
	cout << endl;
}

void hollowsquare(int rows) {
	for (int i = 1; i <= rows; ++i)
	{
		if (int i = 1 || i == rows) {
			for (int j = 1; j <= rows; ++j)
				cout << "+";
		}
		else
			for (int j = 1; j <= rows; ++j)
				if (j == 1 || j == rows)
					cout << "+";
				else
					cout << " ";
		cout << endl;
	}
}

void diagonalline(int rows) {
	for (int i = 1; i <= rows; ++i) {
		for (int j = 1; j <= rows; ++j) {
			if (i == j)
				cout << "+";
			else
				cout << " ";
		}
		cout << endl;
	}
}

void bottomtriangle(int rows) {
	for (int i = rows; i >= 1; --i)
	{
		for (int space = 0; space<rows - i; ++space)
			cout << " ";
		for (int j = i; j <= 2 * i - 1; ++j)
			cout << "+";
		cout << endl;
	}
}

void toptriangle(int rows) {
	for (int i = 1; i <= rows; ++i)
	{
		for (int j = 1; j <= i; ++j)
		{
			cout << "+";
		}
		cout << endl;
	}
}

Last edited on
I realized I did not edit the header file to remove the two unused ints. build successful, testing now
So now the problem that I am running in to is that
1
2
3
4
5
6
7
8
9
 if (c == 1) {
		cout << "Did you want the square to be filled or hollow? [f/h]: " << endl;
		cin >> input;
		if (input == f)
			filledsquare(rows);

		else
			hollowsquare(rows);
	}

it will do the filled square just fine but it will not output a hollow square in the input is something other than f, tried changing the else to another if (input==h) and added h{'h'} to char and still had no affect.
Last edited on
Hello urawrath,

Much better. I will have to load it up and give it a test.

Andy
Hello urawrath,

I get an Error - Code C2660 function does not take 1 argument.

Chances are the prototypes in the "figures.h" file do not match the the function definitions in "figurrs.cpp" or the function calls in "main".

When I made everything match in the three files it compiled without any warnings or errors.

Hope that helps,

Andy
Yes, realized that as well and added rows to int in the figures.h but now the hollow square is not forming correctly, did you also get this?
Last edited on
Hello urawrath,

You messed up the if statement in the "hollowsquare" function.

What you have is: if (int i = 1 || i == rows) and it should be: if (i == 1 || i == rows). You do not need the type of the variable here. It was already defined in the for loop. And you are assigning 1 to "i" not checking for equality.

Your hollow square was coming out filled.

You also missed the extra space on lines 22, 27 and 29. The square is to shoert on width.

The other functions work fine.

Hope that helps,

Andy
Everything works now! Thank you for everything, being patient with me and helping me learn, only started c++ 6 weeks ago.
Hello urawrath,

You are welcome.

You are doing well for six weeks. Just some minor things to work out like when to give a variable a type and the difference between "=" and "==".

Andy
Topic archived. No new replies allowed.