Hi, I'm trying to make a program that prints a solid square of asterisks based on the side size entered by the user. The code builds fine but it doesn't work when I try to run the program. I get some code (looks like hex)? Please help!

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

int main ()
{
	int side;

	int square(int side, int row, int col);

	cout << "Please enter a positive integer for the side size of asterisk square required: \n";
	cin >> side; 
	if (side <= 0) {
		cout << side << " is not a positive integer. \nPlease enter a positive integer for the side size of asterisk square required: \n";
		cin >> side;
	}
	else if (side > 0)
	cout << square << endl;

	return 0;
}
int square(int side, int row, int col)
	{
		for (col=0; col < side; col++){
			cout << endl << endl;
			for (row=0; row < side; row++){
				cout << " * ";
			}
		}
		return 0;
	}

Recommended Answers

All 7 Replies

cout << square << endl;

That's because you printed the value of square . You didn't actually call the function.

Look up calling a function in your text.

Thanks for the help! Fixed the function call error:

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

int main ()
{
	int side, i=0;

	int square(int side, int i);

	cout << "Please enter a positive integer for the side size of asterisk square required: \n";
	cin >> side; //user enters a value for size
	if (side <= 0) {
		cout << side << " is not a positive integer. \nPlease enter a positive integer for the side size of asterisk square required: \n";
		cin >> side;
	}
	else if (side > 0)
	cout << square(side, i) << endl; //calls function square and prints the result

	return 0;
}
int square(int side, int i)
	{
		for (i=0; i < side; i++){
			cout << endl << endl;
			for (i=0; i < side; i++){
				cout << " * " << endl;
			}
		}
		return 0;
	}

Now it prints
*
*
*
*
0

...? It's something with the function loop at the bottom but I don't know how to fix it.

A few hints:
1.) int square(int side, int i); is a function prototype. It should not be placed in main()
2.) function parameters are used to convey information between different methods. what information is i bringing into the square() method?
3.) grab a pencil and paper and go through your loop step by step for say side = 3, read through the instructions and draw what the program output is. You'll find at least one of your errors quickly.
4.) Where is the zero co ming from on the display? What are some of the options for the return type of a function? Do you need to cout for everything?

commented: Good hints :) +6

I've looked again at the code and now it prints the square correctly.

int square(int side, int col, int row)
	{
		for (col=1; col <= side; col++){
			cout << endl;
			for (row=1; row <= side; row++)
				cout << "*";
		}
		return 0;
	}

It's printing the 0 at the end because I'm asking it to return 0 - I'm not sure how to get rid of this as I can't make the function of type void so I have to return something?

i hope this code will help you
you need to read more on functions dear
i have corrected the square function too...
the variable "i" no need to pass to the function as the variable is use only in the function square only

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

void square(int side)//do not need to "pass" the value i as it only uses in the function square only
	{
		for (int i=0; i < side; i++){
			for (int j=0; j < side; j++){
				cout << " * ";
			}
			cout << endl << endl;

		}

	}


int main ()
{
	int side;


	cout << "Please enter a positive integer for the side size of asterisk square required: \n";
	cin >> side; //user enters a value for size
	if (side <= 0)
        {
        while(side<=0)
        {
		cout << side << " is not a positive integer. \nPlease enter a positive integer for the side size of asterisk square required: \n";
		cin >> side;
        }
        }
	else if (side > 0)
	square(side); //calls function square and prints the result , using viod, so the function does not return any value

	return 0;
}

I can't make the function of type void

If a function has a return value you don't have to cout it.

In your case you can either call it like lonelyday proposed square(side); which essentially ignores the return value completely
or you could say

if(square(side) == 0)
       cout<<"Success!"<<endl;

which implicitly calls the function when it tests the == 0 relationship.

Thanks everyone!

xx

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.