Hey all,

I know this is a very newbish questions, but I couldn't readily find the answer on this board so I figured I would ask.

Consider the following code snippet:

for(i=0;i<5;i++)
{
      int number;
      int counter;

      cout << "Enter a number less than 10:"
             << endl;
      cin >> number;

      counter = number;

      while(number < 10)
      {
             number ++;
             cout << number << " and " << counter << endl;
      }
}

Now lets assume that the value of number changes within the while loop (as it does above): will the value of counter automatically change as well? If it does, what is the best way to create a variable that will hold the original value of number for one loop and then at the start of the next loop, take on the new value of number?

Again, I know this is probably a very simple question, but any help would be greatly appreciated.

-D

Recommended Answers

All 5 Replies

>>will the value of counter automatically change as well?
No because they are two different variables. Line 10 only initializes the value of counter to be the same as number. From there on the value of counter does not change.

>>what is the best way to create a variable that will hold the original value of number
You don't have to do anything more because your program already does that.

> will the value of counter automatically change as well?
a) no
b) did you try it?

> will the value of counter automatically change as well?
a) no
b) did you try it?

Unfortunately I throught of this question at work this morning while trying some of the practice problems on this site. I don't have access to a complier at work so I couldn't just try it out. That is the only reason that I asked here. Thank you for the responses and sorry that it was such a newbish question.

-D

>>Unfortunately I throught of this question at work this morning while trying some of the practice problems on this site.

Oh?? Your boss doesn't mind you cheating him?

> Your boss doesn't mind you cheating him?
Apparently, there's a lot of it going around

You'd think in times like this that employees would be keeping their noses squeaky clean and up against that grindstone, making sure that their position was still useful.

But no, dangle a couple of sunny days and the chance of a self certificated week off, and they're out the door.

The fact that they might have some 'explaining to do' a few months later when they ACTUALLY get the flu never crosses their minuscule minds.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.