Calling a function twice

I'm new to programming and tried hard to figure this out by myself, but I'm confused about how to write this.

I need to write a program that displays a block of characters. The number or rows, columns, and which character used is all user-defined. I need to call a function twice to display the number of rows and columns based on what the user entered.

I'm confused about where to place the loop and what goes in it. Here is my attempt. The functions readChar and getNum are needed per the instructions.

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
#include <iostream>
using namespace std;

char readChar();
int getNum(string);
void printBox(int, int, char);

int main()
{
    readChar();
    getNum(rows);
    getNum(cols);
    printBox(rows, cols, ch);
    return 0;
}

char readChar()
{
    cout << "Enter character to use: ";
    cin >> ch;
    return ch;
}

int getNum(int rows, int cols)
{
    cout << "Enter the number of rows: ";
    cin >> rows;
    cout << "Enter the number of columns: ";
    cin >> cols;
    reutrn rows, cols;
}

void printBox(int rows, int cols, char ch)
{
    for (int a = 0; a < rows; a++)
    {
        for (int b = 0; b < cols; b++)
        {
            cout << ch;
        }
        cout << endl;

    }
}

}



Is the loop supposed to go in main? How would I get the rows, columns, and characters displayed?
Last edited on
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
#include <iostream>
#include <string>

char get_char( std::string prompt )
{
    std::cout << prompt << ' ' ;
    char c ;
    std::cin >> c ;
    return c ;
}

int get_num( std::string prompt )
{
    std::cout << prompt << ' ' ;
    int i ;
    std::cin >> i ; // error handling (user may not enter a number) skipped
    return i ;
}

void print_box( char chr, int rows, int cols )
{
    std::cout << '\n' ;

    for( int r = 0 ; r < rows ; ++r )
    {
        for( int c = 0 ; c < cols ; ++c ) std::cout << chr ;
        std::cout << '\n' ;
    }

    std::cout << '\n' ;
}

bool again()
{
    char c = get_char( "once again (y/n)?" ) ;
    return c == 'y' || c == 'Y' ;
}

int main()
{
    do
    {
        const char chr = get_char( "character?" ) ;
        const int rows = get_num( "number of rows?" ) ;
        const int cols = get_num( "number of cols?" ) ;

        print_box( chr, rows, cols ) ;

    } while( again() ) ;
}
I don't understand what std:: is, we aren't using that. Is that standard library? I keep seeing people talk about something like that.

But I've been trying to tweak things and I keep getting an error at line 11, no matter what I do:

getNum(x);

The error message says, "invalid conversion from 'int' to 'const char*' [-fpermissive]". What's going wrong?
Last edited on
Based on how I have to write this, I've tried to edit what you gave me.

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
#include <iostream>
#include <string>
using namespace std;

char readChar();
int getNum();
void printBox(int, int, char);

int main()
{
    int num, rows, cols;
    char c;

    readChar();
    getNum();
    printBox(rows, cols, c);
    return 0;
}

char readChar()
{
    char c;

    cout << "Enter character to use: ";
    cin >> c;
    return c;
}

int getNum()
{
    int num;

    cout << "Enter the number of rows: ";
    cin >> num;
    cout << "Enter the number of columns: ";
    cin >> num;
    reutrn num;
}

void printBox(int rows, int cols, char c)
{
    for (int r = 0; r < rows; r++)
    {
        for (int c = 0; c < cols; c++)
            cout << c;

        cout << endl;
    }
}

}


I'm getting an error at return num; in the getNum function. It's saying I need to declare it.
Last edited on
I think I may see where I need to edit.
The function getNum() should read one number and return that one number.
Call getNum() twice: once to get the number of rows and then a second time to get the number of columns.

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
#include <iostream>

char get_char() ;
int get_num() ;
void print_box( char chr, int rows, int cols ) ;

int main()
{
    const char chr = get_char() ;

    std::cout << "number of rows? " ;
    const int rows = get_num() ;

    std::cout << "number of cols? " ;
    const int cols = get_num() ;

    print_box( chr, rows, cols ) ;
}

char get_char()
{
    std::cout << "character? " ;
    char c ;
    std::cin >> c ;
    return c ;
}

int get_num()
{
    int i ;
    std::cin >> i ; // error handling (user may not enter a number) skipped
    return i ;
}

void print_box( char chr, int rows, int cols )
{
    std::cout << '\n' ;

    for( int r = 0 ; r < rows ; ++r )
    {
        for( int c = 0 ; c < cols ; ++c ) std::cout << chr ;
        std::cout << '\n' ;
    }

    std::cout << '\n' ;
}
Hmm, it's still not working. Is my prototype and function definition written correctly? I don't understand why it's showing this error.
The function getNum() should read one number and return that one number.


That's something I fixed in the second code I showed. I ended up changing it to 'i' like you did because I wasn't sure if I could use 'num'. I'm still getting the error saying I need to declare return.
> I'm getting an error at return num; in the getNum function.
>> I don't understand why it's showing this error.

spelling (line 37): reutrn num; should be return num ;

In any case, you can't return two values (both number of rows and number of columns) from that function.
see: http://www.cplusplus.com/forum/general/198997/#msg952279
Oh wow, it was a typo. Let me see if it'll work now.
Yes, I see that was my problem.

The program executes, but it doesn't display any characters. Or it shows it's running something, just not displaying anything.

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
#include <iostream>
#include <string>
using namespace std;

char readChar();
int getNum();
void printBox(int, int, char);

int main()
{
    int num, rows, cols;
    char c;

    cout << "Enter character to use: ";
    readChar();

    cout << "Enter the number of rows: ";
    getNum();

    cout << "Enter the number of columns: ";
    getNum();

    printBox(rows, cols, c);
    return 0;
}

char readChar()
{
    char c;

    cin >> c;
    return c;
}

int getNum()
{
    int i;

    cin >> i;
    return i;
}

void printBox(int rows, int cols, char c)
{
    cout << endl;

    for (int a = 0; a < rows; a++)
    {
        for (int b = 0; b < cols; b++)
            cout << c;

    }
    cout << endl;
}
1
2
3
4
5
6
7
8
9
10
11
int main()
{
    // these variables are uninitialised
    int num, rows, cols;
    char c;
    //  ...
    // values returned by readChar, getNum are discarded without being used
    // ...

    printBox(rows, cols, c); // still uninitialised: no values have been assigned to rows, cols and c
}
closed account (48T7M4Gy)
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
#include <iostream>
using namespace std;

char readChar();
int getNum();
void printBox(int, int, char);

int main()
{
    char character = readChar();
    
    cout << "Please enter number of rows: ";
    int number_rows  = getNum();
    
    cout << "Please enter number of columns: ";
    int number_cols = getNum();
    
    printBox(number_rows, number_cols, character);
    
    return 0;
}

char readChar()
{
    char ch;
    
    cout << "Enter character to use: ";
    cin >> ch;
    
    return ch;
}

int getNum()
{
    int number;
    cin >> number;
    return number;
}

void printBox(int rows, int cols, char ch)
{
    for (int a = 0; a < rows; a++)
    {
        for (int b = 0; b < cols; b++)
        {
            cout << ch;
        }
        cout << endl;
    }
}
Enter character to use: c
Please enter number of rows: 2
Please enter number of columns: 6
cccccc
cccccc
 
Exit code: 0 (normal program termination)
Why are they being discarded? I thought once a value is returned from a function, it goes back to main and continues . Do I need to move my cout statements from main to the other functions?
@kemort Oh, I see.
closed account (48T7M4Gy)
when you call getnum() you are not allocating the returned value to anything, so nothing happens, the value is lost.

you need int x = getNum();
Last edited on
> Why are they being discarded?
> I thought once a value is returned from a function, it goes back to main and continues.

It does get back (return to) main, but the value it returns is not being used in main.

1
2
3
4
int rows ; // uninitialised
cout << "Enter the number of rows: ";
getNum(); // the value returned by getNum() is discarded.
          // there is no change to the uninitialised state of the variable rows  


1
2
3
int rows ; // uninitialised
cout << "Enter the number of rows: ";
rows = getNum() ; //  the variable rows now holds the value returned by getNum() 


1
2
cout << "Enter the number of rows: ";
const int rows = getNum() ; //  the variable rows is initialised with the value returned by getNum() 
closed account (48T7M4Gy)
Do I need to move my cout statements from main to the other functions?
The short answer is no. It' not working for the reason I gave above.

While not mandatory, it's not always the best policy to use functions for cout's etc. Functions are best when they process and return values, strings etc rather than tie your program down to specific console use. That way they can be easily re-used elsewhere on other (eg windows based) platforms.
I see. Thank you. Looking up my notes again, it seems like I miswrote that part. Gotta fix that now before I make that same mistake.
closed account (48T7M4Gy)
:)
Topic archived. No new replies allowed.