Iampig
Iampig

Reputation: 9

C++ Homework: I keep getting an error

The question is:

Write a program that reads an integer and displays, using asterisks, a filled and hollow square, placed next to each other.

This is the modified source code

#include <iostream>
#include <cstdlib>
void printTop (int a);
int main() {
int num = 1;
cout << "Please, Enter a Number: " ;
cin >> num;
while (num >= 1) {
    printTop (num);
    for (int w = 0; w < num-2; w++) {
        for (int i = 0; i < num; i++) {
            cout << "*";
        }
        cout << " *";
        for (int i = 0; i < num-2; i++) {
            cout << "*\n";
        }
        printTop (num);
        cin >> num;
    }
    return 0;
 }
   void printTop (int a) ;
 {

    for (int i = 0; i < a*2; i++)
    {
        cout << "*";
        if (i == a-1) {
            cout << " " ; 
        }
    }
    cout << "\n" ;
}

Upvotes: 0

Views: 116

Answers (2)

Henricus V.
Henricus V.

Reputation: 948

Unlike Java and some other languages, C++ does not allow methods to be invoked before they were declared.

You can put the following declaration before your int main(int argc, char* argv[]) method:

void PrintTop(int a);

However, you don't need to put the function's definition before main. The declaration is suffice.

Some other problems from your code: for (int w = 0; w < num-2; i++) should be replaced by for (int w = 0; w < num-2; w++).

Upvotes: 1

manglano
manglano

Reputation: 844

You must declare printTop() above the main method. C++ compilers expect methods to be forward declared (https://en.wikipedia.org/wiki/Forward_declaration).

Upvotes: 2

Related Questions