Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 9th, 2007, 8:54 PM   #1
SonicReducer
Newbie
 
Join Date: Mar 2007
Posts: 7
Rep Power: 0 SonicReducer is on a distinguished road
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;
}
SonicReducer is offline   Reply With Quote
Old Apr 9th, 2007, 8:59 PM   #2
ReggaetonKing
Sexy Programmer
 
ReggaetonKing's Avatar
 
Join Date: Nov 2005
Location: New Jersey
Posts: 891
Rep Power: 3 ReggaetonKing is on a distinguished road
Send a message via AIM to ReggaetonKing
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;
}
__________________
I would love to change the world, but they won't give me the source code!
ReggaetonKing is offline   Reply With Quote
Old Apr 9th, 2007, 9:19 PM   #3
SonicReducer
Newbie
 
Join Date: Mar 2007
Posts: 7
Rep Power: 0 SonicReducer is on a distinguished road
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?
SonicReducer is offline   Reply With Quote
Old Apr 9th, 2007, 9:25 PM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
      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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Apr 10th, 2007, 12:07 AM   #5
SonicReducer
Newbie
 
Join Date: Mar 2007
Posts: 7
Rep Power: 0 SonicReducer is on a distinguished road
Quote:
Originally Posted by DaWei View Post
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.
SonicReducer is offline   Reply With Quote
Old Apr 10th, 2007, 3:36 AM   #6
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
EXECryptor software protection Jean5 C++ 35 Oct 10th, 2006 7:10 PM
Little help whoawhoayoyo Assembly 8 Apr 18th, 2006 7:10 PM
How to post a question nnxion C++ 10 Jun 3rd, 2005 11:53 AM
How to post a question nnxion C++ 0 Jun 3rd, 2005 8:55 AM
How to post a question nnxion C 0 Jun 3rd, 2005 8:55 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:28 AM.

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