0

I did a coding that accepts an integer from the user, and output the corresponding number of asterisks. For example:

Please enter a number: 10
* * * * * * * * * * 

I must use function concept to complete the coding only. I managed to get the correct number of asterisks but the '0' at the back just won't disappear.

#include<iostream>
using namespace std;

int NUMBER, i; 
int ASTERISK (int NUMBER);
int main ()

{
    cout << "Enter a number: " ;
    cin >> i ;
    cout << ASTERISK (i); 
    return 0;
}

int ASTERISK (int NUMBER){
    int ANSWER;
    for (NUMBER=1; NUMBER<=i; NUMBER=NUMBER+1){
        cout << "* " ;
    }
    return ANSWER;
}
YSC
  • 38,212
  • 9
  • 96
  • 149
Wen Dee
  • 1
  • 1
  • 1
  • `for (NUMBER=1; NUMBER<=i; NUMBER=NUMBER+1)` might be replaced with `for (NUMBER=0; NUMBER < i; NUMBER++)` – vahancho May 27 '19 at 12:02
  • 1
    ***but the '0' at the back just won't disappear*** I don't understand what you mean by that. – drescherjm May 27 '19 at 12:03
  • 1
    Why do you need to return value from ASTERISK() function? replace "cout << ASTERISK (i);" with "ASTERISK (i);" Hope it will help. – Faruk Hossain May 27 '19 at 12:05
  • 2
    The naming in this code is so unique it can serve as a great programming exercise (global i passed as argument as NUMBER but NUMBER argument is overwritten in the loop and compared to global i). Also global NUMBER just sitting there doing nothing. – UmNyobe May 27 '19 at 12:10
  • a few suggestions: (1) don't use uppercase names for variable names or method names, this is usually reserved for constants (2) do not make your asterisk function dependent on a global outside variable like i, which you don't need inside the function actually (3) I would recommend the asterisk function to return the actual "answer" string – Christian G May 27 '19 at 12:17
  • BTW, your `ASTERISK` function does not need to return a value. Change the return type to `void`, which means that the function is not returning a value. – Thomas Matthews May 27 '19 at 16:46

3 Answers3

3

This line is the cause of the problematic zero:

cout << ASTERISK (i); 

This asks to print the value returned by ASTERISK (i).

int ASTERISK (int NUMBER){
    int ANSWER;
    for (NUMBER=1; NUMBER<=i; NUMBER=NUMBER+1){
        cout << "* " ;
    }
    return ANSWER;
}

In the definition of ASTERISK, you print a NUMBER of asterisks and return the value ANSWER which is an uninitialized integer, which happens to be zero in your case.

This is bad for two reasons:

  • Manipulating uninitalized values is in general undefined behavior which can have a lot of bad consequences. Really bad.
  • Returning a value when you don't need it makes the code harder to maintain.

As a solution, make ASTERISK return nothing:

ASTERISK (i); 
// ...
void ASTERISK (int NUMBER) {
    for (NUMBER=1; NUMBER<=i; NUMBER=NUMBER+1){
        std::cout << "* " ;
    }
}

Finally, you completly missed the point of scoped variable. Try and get that important concept as soon as possible by not using global variables. With a few improvements, your function would become:

void print_asteriks(int count) {
    for (int n=0 ; n<count ; ++n) {
        std::cout << "* ";
    }
}
YSC
  • 38,212
  • 9
  • 96
  • 149
  • Additionally, a good read: [Why is “using namespace std” considered bad practice?](https://stackoverflow.com/q/1452721/5470596) – YSC May 27 '19 at 12:10
2

That zero is coming from cout << ASTERISK (i);

Replace that with ASTERISK (i);

rustyx
  • 80,671
  • 25
  • 200
  • 267
Yogesh
  • 565
  • 3
  • 21
  • 3
    Also `int ASTERISK (int NUMBER){` should be `void ASTERISK (int NUMBER){` and not return a value. – drescherjm May 27 '19 at 12:07
  • @drescherjm indeed, but the user's concern here was from where the heck this 0 is coming :). Returning the integer may not be of any use here. – Yogesh May 27 '19 at 12:10
  • Currently the return value is not initialized and is undefined behavior. – drescherjm May 27 '19 at 12:24
1

Like others already mentioned the issue is that you output the returnvalue of ASTERISK(i) using cout << ASTERISK(i)

In fact you do not need this returnvalue at all, since your function never uses it (it does not even get initialised, which causes undefined behavior), so it would be best to leave it out all together.

The use of global variables in this use case seem not really necessary either, so I would suggest using local variables, so I would suggest changing to

void ASTERISK (int number);
int main ()
{
    std::cout << "Enter a number: " ;
    int number;
    std::cin >> number;
    ASTERISK(number); 
    return 0;
}

void ASTERISK (int number){
    for (int i=0; i!=number; ++i){
        std::cout << "* ";
    }
}
maxbachmann
  • 2,862
  • 1
  • 11
  • 35
  • Thank you so much for all the guidance and advice. Still a very new learner in Programming C++, will read and understand more. – Wen Dee May 29 '19 at 03:51