1

I have 3 functions:

  1. First function takes an int a and prints the that number of dots.
  2. The second function takes an int b and prints that number of stars.
  3. The third function takes a and b and calls the dots function and stars function. When I call it in main, it returns 0 without any output.

Each of the functions work fine on their own - why doesn't the third one work then?

EDIT: Tried initializing i=0, code still compiles with the same result. Again, both functions dots() and stars() work fine when called on their own.

void dots(int a){
    for(int i; i<a; i++){
        cout << ".";
    }
}
void stars(int a){
    for(int i; i<a; i++){
        cout << "*";
    }
}

//(not working):
void dotsstars(int a, int b){
    dots(a);
    stars(b);
}

int main(){
    dotsstars(5, 6);

    return 0;
}
  • 2
    Uninitialized automatic variables (i.e. local variables) are not implicitly initialized. Their values will be *indeterminate*. And using them in C++ leads to *undefined behavior*. – Some programmer dude Apr 02 '17 at 16:13

4 Answers4

4

Instantiate your iterative i values to 0. So, the for loops becomes:

for(int i = 0; i < limit; i++)

Also, maybe you're program is closing too quickly before you can see the results if you're not executing it from your terminal/console.

 int main() {

     /* your function invoked */

     int i = 0;
     cin >> i++; // This is a not so neat way to pause your program. Do something with your value so that you don't get unused variable warning.
     return 0;
 }
Santiago Varela
  • 2,199
  • 16
  • 22
3

The variable i at the moment has garbage value so most probably the for loop will always be false (i.e it won't run) so you need to initialize i

for(int i = 0; i < number_of_times ; i++)

If your console closes quickly and you are not able to see the result then add getch(); in the end, you'll have to include the #include <conio.h> library

P.S i know using getch() is not a good practice and it makes the program OS dependent.

Jonas
  • 6,915
  • 8
  • 35
  • 53
RazaUsman_k
  • 694
  • 6
  • 20
0

You need to initialize the incrementer i to 0:

for (int i=0; i<a; i++){
Aaron Paul
  • 117
  • 3
  • 14
  • To be clear, when you use a local variable without first assigning a value to it, the results are undefined. This comment http://stackoverflow.com/questions/1597405/what-happens-to-a-declared-uninitialized-variable-in-c-does-it-have-a-value was about C, but the point is valid here. – Aaron Paul Apr 02 '17 at 16:16
  • **incrementer** is a really wrong term , it should be **initialized** – RazaUsman_k Apr 02 '17 at 16:39
  • Initialize the variable not the incrementer – RazaUsman_k Apr 02 '17 at 16:50
0

you are not initializing i that's why it is not printing. i is a local variable so default value for local variable will be garbage value that's why you need to initialize it first.

#include<iostream>
using namespace std;
void dots(int a){
    for(int i=0; i<a; i++){// i is set to be 0 here
        cout << ".";
        }
    }
void stars(int a){
    for(int i=0; i<a; i++){// i is set to be 0 here
        cout << "*";
        }
    }

//(not working):
void dotsstars(int a, int b){
    dots(a);
    stars(b);
}

int main(){
    dotsstars(5, 6);

    return 0;
}
Jonas
  • 6,915
  • 8
  • 35
  • 53
Mohit Yadav
  • 471
  • 8
  • 17