![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Mar 2007
Posts: 7
Rep Power: 0
![]() |
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;
} |
|
|
|
|
|
#2 |
|
Sexy Programmer
|
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! |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Mar 2007
Posts: 7
Rep Power: 0
![]() |
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? |
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
cin >> testScore[i]; 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 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 |
|
|
|
|
|
#5 | |
|
Newbie
Join Date: Mar 2007
Posts: 7
Rep Power: 0
![]() |
Quote:
|
|
|
|
|
|
|
#6 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |