Sign in to follow this  
MadsGustaf

whats wrong with this code?

Recommended Posts

[source lang = "cpp"]
#include <iostream>

using namespace std;
double counter (int i)
 {
 while(i < 100)
  ++i;
  cout << i << "\t";
return 0;
 }  
 
double counter2 (int i2)
 {
  while (i2 < 100)
   ++i2;
    i2= i2*i2;
   cout << i2 << "\t";
   return 0;
 }


int main()
{
int i = 0;
int i2 =0;

double counter (int i);

double counter2 (int i2);

return 0;
}

im trying to make a program that makes 2 lines. 1st line with numbers from 1-100. 2nd line. numbers from line 1 raised to the 2nd (i^2). when i compile and run it shows nothing on DOS.

Share this post


Link to post
Share on other sites
In main(), change:

double counter (int i);
double counter2 (int i2)


To:

counter(i);
counter2(i2);


The problem is that your code doesn't call the two functions. It just declares them.

Share this post


Link to post
Share on other sites
Without more brackets, your while loops only encompass the next line. Just like the functions you've declared, you should enclose everything in a while loop inside of { }. For example:
while (i < 100)
// happens 100 times:
++i;

// happens once:
cout << i;


i = 0;
while (i < 100)
{
// happens 100 times:
++i;
cout << i;
}

Share this post


Link to post
Share on other sites
You need braces to group statements under a while. The code:
while(i < 100)
++i;
cout << i << "\t";


Is equivalent to:
while(i < 100)
{
++i;
}
cout << i << "\t";

This effectively increments i until it's 100 and then prints it. What you presumably meant was:
while(i < 100)
{
++i;
cout << i << "\t";
}


This will print all of the numbers for you.

HTH

Share this post


Link to post
Share on other sites
thanks everyone! :D
heres the final result, not-so-messy (when runned)

[source lang = "cpp"]
#include <iostream>

using namespace std;
double counter (int i)
{
while(i < 100)
{
++i;
cout << i << "\t" << i*i << "\n";
}
return 0;
}

int main()
{
int i = 0;
counter(i);
return 0;
}



Share this post


Link to post
Share on other sites
OK, so it works; now, let's make it sensible.

- When you say 'double counter(int i)', you promise that the function will return a value of type double. What is this value supposed to indicate? In the current code, the returned value is always 0 (well, 0.0 - i.e. 0 as a double - because when you say you are returning a double, you return a double, and here that means that the int will be cast to a double implicitly), and never used. This suggests that it actually doesn't mean anything :)

In C++, you don't actually have to return something from a function. You can (and in a case like this, should) declare the return type as 'void', which means you aren't returning anything. At the end of a void function (i.e. a function declared with void return type), control returns implicitly, so there is no need for a return statement (you don't need to specify a value to return, because there is no value). If you need to return from the function before the end, you can just use 'return;'. Trying to specify a return value when the function is declared void is an error, of course. :)

- To do the counting, we increment i each time through the loop, and stop when it reaches a certain value. There is a more specific, and therefore more appropriate construct for this than a while loop: the for loop. Conceptually, this represents the idea that we are doing the loop operations "for" each value of i in range. The bracketed clause after the 'for' keyword contains three items separated by semicolons:

1) An initialization (i.e. a statement executed before the loop starts). This can be used to declare a variable that is scoped to the loop.

2) An exit-condition. At the beginning of each loop, the condition is checked and if not met, the loop is broken. This works just like the bracketed clause for a while loop.

3) A post-loop operation. This action is taken at the end of each iteration of the loop. (If the loop reaches a 'continue' statement, the post-loop operation is still done, so it is *not* quite the same as putting this statement as the last one in a while loop.)

Now, the original code incremented 'i' at the *beginning* of the loop, so we need to make a few modifications to get the original semantics. :)

- Finally, there is never any need to declare a variable to store a value just to pass it to a function. Variables are primarily for storing things that you're going to need *more than once*, or to cut up complex things, or give a name to important sub-results. For simple things like calling a function with some value, you can just pass the value directly.

This gives us:

[source lang = "cpp"]
#include <iostream>

using namespace std;
void counter(int i) {
// The first ++i reflects the fact that the old loop did the increment first.
// The change to <= reflects the fact that we will now test 'i' after
// incrementing it, whereas the old loop tested it before.
// This structure is generally considered cleaner, though: not only are we
// using a more specific construct (the for-loop), but we no longer modify i
// "in the middle of" doing our other stuff; the for loop structure encourages
// us to move all the stuff that happens to our loop counter to the outsides
// of the loop, and the "real work" treats it as read-only.
for (++i; i <= 100; ++i) {
cout << i << "\t" << i*i << "\n";
}
}

int main() {
counter(0);
return 0;
}



We can, of course, go further, but let's digest this much first :)

Share this post


Link to post
Share on other sites
never.

For historical reasons main always returns an int. You see some programs (mostly back in the good old days) programs return a 0 if the program succeeded and some other number (an error code) if there is an error.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this