Usiing While Loop to display the contents of an array

Apr 17, 2013 at 10:19am
Usiing While Loop to display the contents of an array except for numbers less than 20
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  #include <iostream>
using namespace std;
voidprintfunction(int d[],int b)
int main()
{
    int  d[],int b;
    cout << "int b";
    d > 20;
    while(d>b)
    {
        d++
        cout<<d<<"/n";
    }
    return 0;
}
Apr 17, 2013 at 10:38am
And what is the problem?:)
Apr 17, 2013 at 11:03am
My problem is that being new to this whole thing of C++, my tutor want me to use a while-loop to display the contents of an array for numbers above or equal to 20. The above code I tried failed and throws errors. Any ideas or help?????
Thaks Vlad
Last edited on Apr 17, 2013 at 11:08am
Apr 17, 2013 at 11:10am
At line 6, although that's technically a legal way to declare two variables, that's an odd way of doing it. Is there any particular reason you're using a comma there? Do you understand what the comma means in C++?

At line 7, it's going to output the string "int b" to the standard output. Is that really what you intended?

At line 8, what are you trying to do? Because that line does nothing.

At line 9, your condition looks like it makes no sense. d is a pointer, not an integer value, because at line 6 you declared it to be an array of ints. Even if d were an int, you haven't initialised d or b, so they both contain undefined values. What's the point of comparing two undefined values?

Also, your while loop will only execute the code inside it if d is greater than b. However, inside that loop, you make d even greater (by incrementing it at line 11). Thus, d will never stop being bigger than b, so your loop will run infinitely.

(Well, actually, it will loop until d is the maximum possible value am int can hold, after which it will suddenly become very negative, and the loop will exit. Is that what you intended?)
Last edited on Apr 17, 2013 at 11:13am
Apr 17, 2013 at 11:29am
The compiler is your friend. You should read its messages because it tries to help you.

I think your compiler for example issued an error or warning message for this line

voidprintfunction(int d[],int b)

I think you meant

void printfunction(int d[],int b);

and also you need to place a semicolon in the end of the line.

So read the compiler messages and if some message of the compiler is not clear then ask here your question.
Last edited on Apr 17, 2013 at 11:30am
Topic archived. No new replies allowed.