Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   For Loops in this Code (http://www.programmingforums.org/showthread.php?t=12969)

SonicReducer Apr 9th, 2007 9:54 PM

For Loops in this Code
 
What exactly does this program do? I don't get why the for loops would add one to the number the user types in if the number is less than the maximum number. I got this code from an ebook by the way, and I get compiler errors when I try to compile and run it in DevC++.

:

#include <iostream>
using namespace std;
const int MAX = 3;
int main ()
{
  int testScore[MAX];
  for (int i = 0; i < MAX; i++)
  {
      cout << "Enter test score #" << i + 1 << ": ";
      cin >> testScore[i];
  }
  for (i = 0; i < MAX; i++)
  {
      cout << "Test score #" << i + 1 << ": "
        << testScore[i] << endl;
  }
  return 0;
}


ReggaetonKing Apr 9th, 2007 9:59 PM

the second for loop does not declare your variable i as an integer value.
:

#include <iostream>
using namespace std;
const int MAX = 3;
int main ()
{
  int testScore[MAX];
  for (int i = 0; i < MAX; i++)
  {
      cout << "Enter test score #" << i + 1 << ": ";
      cin >> testScore[i];
  }
  for (int i = 0; i < MAX; i++)
  {
      cout << "Test score #" << i + 1 << ": "
        << testScore[i] << endl;
  }
  return 0;
}


SonicReducer Apr 9th, 2007 10:19 PM

Thanks, I can compile the program now. But my question was really about the code. i equals 0, if it's less than MAX add 1? So if I typed in 22 it would become 23? Wouldn't that mess things up?

Also, when I run the program it closes immeadiately after I've entered the 3 numbers, so any number that it puts out, I can't see. How can I fix this?

DaWei Apr 9th, 2007 10:25 PM

:

      cin >> testScore[i];
Test that statement for success. If the user enters a non-numeric, it'll break your program. Refer to your documentation

The 1 is being added strictly for presentation purposes. Arrays begin with element 0. Adding one to the number results in
:

1.  score from array 0
2.  score from array 1
3.  score from array 2

rather than:
:

0.  score from array 0
1.  score from array 1
2.  score from array 2


Don't post, "I got errors," or "It doesn't work." State the errors precisely and indicate the lines on which they occurred, if possible. If you get it running, and "It doesn't work," explain how it fails to meet your expectations.

Have a look at the "How to Post a Question" thread, it's a sticky right at the top of this forum.

SonicReducer Apr 10th, 2007 1:07 AM

Quote:

Originally Posted by DaWei (Post 126533)
Don't post, "I got errors," or "It doesn't work." State the errors precisely and indicate the lines on which they occurred, if possible. If you get it running, and "It doesn't work," explain how it fails to meet your expectations.

Have a look at the "How to Post a Question" thread, it's a sticky right at the top of this forum.

Sorry about that, but I didn't really care about the code working or not. I just wanted to understand the for loops. I think I've got it now, thanks for yet another good reply.

DaWei Apr 10th, 2007 4:36 AM

Look at your code again. The number you enter (testscore [i]) is not getting one added to it. The COUNTER (i) is getting one added to it.

Programs run until they complete, then terminate. If you run your program from the command line, then the window belongs to command.exe. When your program completes, command.exe will put out another prompt in the same window. Your output will still be showing.

If you run your program in its own window (double-clicking its icon, say), then it will terminate and the window will close.

If the IDE (if you have one) provides the window, then IT will stay open, but if it spawns a window specifically for that execution, the window will close.

Note that the key word is "terminate". You may add an input statement to the end of your program so that it doesn't terminate until you provide it input. It'll sit there and wait for that, giving you plenty of time to look. You can use "cin.get ()."

You will need to clear the input before "cin.get ()" in case your user has pounded in some extraneous stuff. You can use cin.sync (), although there is some controversy regarding its standardized applicability to a basic_istream, or you may use cin.ignore (). See your documentation.


All times are GMT -5. The time now is 2:03 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC