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.
#include<iostream>
usingnamespace 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.
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?
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.
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)
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
}
@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 :)