0

So...this is my first time messing around with functions and almost never worked with for either and I wanted to create a function that printed hello as many times as the parameter (n) said.

#include <iostream>
int say_hello(int n){
    for(int n, int t=0; t!=n; t++){
        std::cout << "Hello" << std::endl;
    }
}


int main(){
    say_hello(5);
    return 0;
}

But I seem to have done something horribly wrong because of all these errors.

  • error: expected unqualified-id before 'int'
  • error: expected ';' before 'int'
  • warning: for increment expression has no effect [-Wunused-value]
  • error: expected ')' before ';' token
  • error: 't' was not declared in this scope
  • error: expected ';' before ')' token
  • warning: no return statement in function returning non-void [-Wreturn-type]

I want to learn C++ properly and at least try to not get into too many bad habits, any advice on sites or beginner challenges?

Rishi
  • 1,387
  • 10
  • 14
Macronical
  • 53
  • 3

3 Answers3

4

Your problem boils down to replacing

for(int n, int t=0; t!=n; t++){

with

for(int t=0; t!=n; t++){

You don't need to redeclare n (as it's a function parameter), which also fixes the syntax error in the for loop. That syntax error is the cause of all the compiler diagnostics. More often than not, the first compiler diagnostic is the one you should concentrate on.

Also, don't forget to return a value from say_hello, or make it a void return type.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
2

There is a typo in the function. You shall not declare the variable n in the for statement that hides the parameter.

int say_hello(int n){
    for(int n, int t=0; t!=n; t++){
        ^^^^^^
    std::cout << "Hello" << std::endl;
    }
}

Also t is not good name for an index in a loop. It would be better to use for example the name i.

Also the function is unsafe because the argument passed to the function can be a negative number.

And the function returns nothing though it has return type int. Thus the function has undefined behavior.

So a more correct function definition can look like

void say_hello( unsigned int n )
{
    for ( unsigned int i = 0; i != n; i++ )
    {
        std::cout << "Hello" << std::endl;
    }
}

Or it can return reference to the stream that will allow to chain the function with other functions.

For example

std::ostream & say_hello( unsigned int n, std::ostream &os = std::cout )
{
    for ( unsigned int i = 0; i != n; i++ )
    {
        os << "Hello" << std::endl;
    }

    return os;
}

In fact the local variable i is not used in the body of the for statement. So it can be removed. In this case you can use a while loop instead of the for loop. For example

std::ostream & say_hello( unsigned int n, std::ostream &os = std::cout )
{
    while ( n-- )
    {
        os << "Hello" << std::endl;
    }

    return os;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

You can correct your for statement this way:

// n cannot be negative since we are counting from 0
// hence we have an unsigned int argument
int say_hello(unsigned int n) { 
    for(unsigned int t=0; t<n; t++) { // t ranges from 0 to n-1 i.e. loop executes n times
        std::cout << "Hello" << std::endl;
    }
}
Rishi
  • 1,387
  • 10
  • 14