function counter

i have a program where it displays 1-5. it has to increment in the count() function, but it only displays 1's. how do i fix this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<iostream>
using namespace std;
int i;
int main()
{
    int i=0,j;
    int count(int);
    for(j=1;j<=5;j++)
        {cout << count(i) << endl;}
    system("pause");
    return 0;
}

int count(int i)
{
    i++;
    return i;
}
I don't see how this program would display anything since it won't compile as shown. You can't declare the count function inside of main. Anyway, once you fix that you need to pass I to count as a reference so that it is modifying the original i and not a temporary copy created within count. As shown, i would remain 0 inside of main and therefore count would increment 0 to 1 and return one each and every time.

int count(int& param);
closed account (S6k9GNh0)
I seriously appreciate you using the code BBCODE.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<iostream>
using namespace std; //Made evident multiple times that this probably isn't the best way. You'll learn about this later.
int i; //This is a bad idea.
int main()
{
    int i=0,j;                   //only i is needed.
    int count(int);          //Does this really compile?
    for(j=1;j<=5;j++)   //j is being incremented. Not i. Only i is called so switch them.
        {cout << count(i) << endl;} //use i since i is going to be incremented.
    system("pause"); //You could just use getchar();
    return 0;
}

int count(int i) // this isn't needed.
{
    i++;
    return i;
}

The above results in my assumption of you being new to C++?

1
2
3
4
5
6
7
8
9
10
#include<iostream>
int main()
{
    for( int i = 1; i <= 5; ++i )
    {
       std::cout << i << std::endl;
    }
    getchar(); //Waits for a key to be pressed.
    return 0;
}

This is the shortest and probably most correct way to finish this. Please check the errors I pointed out and learn them. The best way to learn can very easily be from mistakes.

As I can point very easily is that everything in C++ has a logical explanation. EVERYTHING. If you don't understand how something works, you need to understand how it works before you use it.
Last edited on
yes this does complile. the reason i need it does the way i have it above is because it is a simpler version of a homework assingment which needs to be done a specific way.

computerquip--- the program is required to increment i in another function other than int main(), return the value to main, then main displays i. i know the way you have above but it isnt an acceptable formatting for the assignment.

kempofighter--- could you disply the code so i can see exaactly what you mean?
Last edited on
I already did. Just add the '&' after the int in the declaration and definition of the function and the problem will be solved. Actually that code compiled with my windows compiler after all. I don't know why though. I guess I stand corrected on that point. In addition, take a look at this tutorial so that you can understand why that will work.

http://www.cplusplus.com/doc/tutorial/functions2/
On line 7 (one the first bit of posted code) you arent declaring the variable passed to the function unless int was a variable which it is not
oooooooo, ok i got it kempofighter. sorry and thanks. it works now.

and line 7 is supposed to be a function prototype. isnt that right. i mean it works so i guess so,lol.
Last edited on
hm ye it works lol but thats like saying this works


1
2
3
4
5
6
7
int undeclaredVariable;

int main() {

return 0;

}


The thing I was meaning is, that in my code the variable was undeclared so it could be anything, in your case, the type could be random (thats if I am right lol)
@arcadiu: That is called a function prototype. In a function prototype, variable names are not necessary. All the compiler needs is the variable type.

Btw: Function prototypes should be placed after the include declarations, not in main.
Last edited on
ok thank you so you dont have to say stuff like how many arguments and how many variables are to be declared?
I don't really understand what your trying to say but, if you mean "Do I have to put all the passed in variables in the prototype?"

Yes, (There are special cases, ellipsis)

ex:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include ...

int somef(int a, int b, int c); // I like to place the variable names in the prototype

int main()
{
  somef(1, 2, 3);
  return 0;
}

int somef(int a, int b, int c)
{
  cout << a << " " << b << " " << c << endl;
  return 7; // some random value
}
ah ok sorry to be a pain up the arse but how come he didnt put the variable names in the prototype?

sorry!!

arcadiu
@arcadiu: because the language doesn't require him too. Either of the following are valid prototypes. Although some of us prefer the variable to be named in the prototype, it isn't required. You don't have to provide the names until the function is defined.

1
2
int somef(int a, int b, int c); // I like to place the variable names in the prototype
int somef(int, int, int); // I like to place the variable names in the prototype 

oo i see!!!!! Sorry to be a pain up the arse guys. Appreciate the help. I understand now, there are some things books dont tell you so you have to ask the masters themselves :)
Topic archived. No new replies allowed.