English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

#include

using namespace std;

void Increment(int);
int main()
{
int count = 1;
while(count < 10)
{
cout << " The number after " << count << endl;
Increment(nextNumber);
cout << " is " << count << endl;
}
return 0;
}

void Increment (int nextNumber)
{
nextNumber++;
}

2007-02-16 16:15:52 · 8 answers · asked by ashcatash 5 in Computers & Internet Programming & Design

8 answers

In the while loop you never increment "count" so "count" is always less than 10 and thus will not break out of the loop.

G

2007-02-16 16:23:05 · answer #1 · answered by disgruntledpostal 3 · 0 0

The problem is that in your call to Increment, you do not pass count as the parameter. You pass a variable called nextNumber. So, each iteration of the loop goes somewhat as follows:

count = 1; // to start
while(count < 10){ // ok, go from 1 to 9, that's 9 iterations
cout << "The number after" << count << endl;
// The above line prints "The number after 1" on the first run through
Increment(nextNumber);
// The line above is your main problem. You are not increasing
// count! if you want it to iterate 9 times it must read
// Increment(count). If you want it to iterate 10 times, but still print
// "1" on the first iteration, then set "while(count <= 10) as your
// conditional statement.

If you need more explanation, hit me up at krazyzima@yahoo.com.

Hope this helps!

2007-02-17 00:25:44 · answer #2 · answered by krazyzima 2 · 0 0

I hope you know about passing by value and reference. You did pass by value. So infinite loop occurs.

You use pass by reference.

Change function increment(int nextnumber) to

void increment(int *nextnumber) {
*nextnumber++;
}

while calling use increment(&count);

That's all .. It would work..

All the best..

2007-02-17 00:23:36 · answer #3 · answered by Anonymous · 0 0

Increment count inside the while statement.

while(count < 10)
{
cout << " The number after " << count << endl;
count++;
cout << " is " << count << endl;
}

2007-02-17 14:51:56 · answer #4 · answered by Anonymous · 0 0

Do not use function to increment the numbers, you can simply use count++, instead of function. By using function for such simple operation, you are decreasing the perfomance of your program.

2007-02-17 01:57:26 · answer #5 · answered by manoj Ransing 3 · 0 0

hey,you haven't incremented count in the cout statement...

2007-02-17 01:11:01 · answer #6 · answered by James 1 · 0 0

Joke?
http://www.phim.unibe.ch/comp_doc/c_manual/CPLUSPLUS/CONCEPT/scope.html

2007-02-17 00:25:10 · answer #7 · answered by Anonymous · 0 0

Blah, it's extremely obvious. Go figure it out yourself.

2007-02-17 00:19:05 · answer #8 · answered by wiiρlυѕ 6 · 0 1

fedest.com, questions and answers