|
-
March 3rd, 2004, 07:17 PM
#1
Help me out with this code please!
Hey guys, the problem I'm having here is that the cin.getline statement won't work the second time the loop reaches it.
There are 3 loops, and when the parent loop (the first one) loops a second time, it just skips right past the cin.get and doesn't pause for input! Why does it do this? I hope someone can help!
PS: It does work the first time!
Code:
//--Variable-Declarations-----------------------------------
char Name[3][9] = {" "}; //--01--
int Grades[3][5][6] = {0}; //--02--
string Categories[5] = {"Test Grade",
"Homework Grade",
"Quiz Grade",
"Lab Grade",
"Final Exam Grade"}; //--03--
int MAX[5] = {3, 5, 3, 6, 1}; //--04--
//----------------------------------------------------------
//--Input-Data--------------------------------
for(int i = 0; i != 3; i++) {
cout << "Please input the name for student " << i + 1 << ": ";
cin.getline(Name[i], 9);
for(int ii = 0; ii != 5; ii++) {
for(int iii = 0; iii != MAX[ii]; iii++) {
cout << '\t' << Name[i] << "::" << Categories[ii]
<< ' ' << iii + 1 << ": ";
cin >> Grades[i][ii][iii];
}
cout << '\n';
}
cout << '\n';
}
-
March 4th, 2004, 08:39 AM
#2
Re: Help me out with this code please!
Code:
//--Variable-Declarations-----------------------------------
char Name[3][9] = {" "}; //--01--
int Grades[3][5][6] = {0}; //--02--
string Categories[5] = {"Test Grade",
"Homework Grade",
"Quiz Grade",
"Lab Grade",
"Final Exam Grade"}; //--03--
int MAX[5] = {3, 5, 3, 6, 1}; //--04--
//----------------------------------------------------------
//--Input-Data--------------------------------
for(int i = 0; i != 3; i++) {
cout << "Please input the name for student " << i + 1 << ": ";
cin.getline(Name[i], 9);
for(int ii = 0; ii != 5; ii++) {
for(int iii = 0; iii != MAX[ii]; iii++) {
cout << '\t' << Name[i] << "::" << Categories[ii]
<< ' ' << iii + 1 << ": ";
cin >> Grades[i][ii][iii];
cin.ignore(); // Added!
}
cout << '\n';
}
cout << '\n';
}
The reason you need to add that, is that when you get an int from cin, the '\n' character after the number is left in the input buffer. This will cause unexpected behaviour when you start inputting text again. It works the first time since there is no '\n' in the buffer when the application starts.
-
March 4th, 2004, 01:01 PM
#3
You should also avoid using variable names like "i", "ii", and so on. You're just making it more confusing for yourself.
i = StudentIdx
ii = CategoryIdx
iii = GradeIdx
making the final assignment:
Grades[StudentIdx][CategoryIdx][GradeIdx]
It's actually possible to read the code, then. If you did not do array accesses in each of the loops, it'd take someone hours to figure out what the **** "ii" meant. Get in the habit now, saves time later, and keeps people from skipping over your questions because they don't want to deal with decoding it!
-
March 4th, 2004, 02:35 PM
#4
it's a simple variable used only during the existance of the for loop. The loop isn't that big, and I didn't feel like craming huge variable names in there (like the ones you made up) which would only make the code look messy and longer than necessary.
Now, if the variables had a larger scope, I would assign them meaningful names.
I appreciate your suggestion, but I think the I's are very self explanatory considering they only exist on a few contiguous lines of code.
-
March 4th, 2004, 02:58 PM
#5
Originally posted by Linenoise
You should also avoid using variable names like "i", "ii", and so on. You're just making it more confusing for yourself.
i = StudentIdx
ii = CategoryIdx
iii = GradeIdx
making the final assignment:
Grades[StudentIdx][CategoryIdx][GradeIdx]
It's actually possible to read the code, then. If you did not do array accesses in each of the loops, it'd take someone hours to figure out what the **** "ii" meant. Get in the habit now, saves time later, and keeps people from skipping over your questions because they don't want to deal with decoding it!
Where did you learn C++ programming? Every book I have read and pretty much all programmers I've ever talked to, very simple variables like 'i' and 'y', ect.. are used alot in for loops. Even I do it.
Making long variable names in a for loop is.... well kind of wasteful and, in fact, it may confuse others looking at your code since simple variable names are widely used within the for loop's declaration.
Also, what do you mean it may take them 'hours'? Just simply looking at the declaration of the for loop and you know what it is.
That may be your style, so just don't tell him he should use your way, especially since it seems the more widely used method is what he is using. You could just give him it as a suggestion, but I think they way you worded your post, it looks like someone would get confused unless they used your method(which obviously isn't true).
-
March 4th, 2004, 03:05 PM
#6
Originally posted by kasracer
Where did you learn C++ programming? Every book I have read and pretty much all programmers I've ever talked to, very simple variables like 'i' and 'y', ect.. are used alot in for loops. Even I do it.
Making long variable names in a for loop is.... well kind of wasteful and, in fact, it may confuse others looking at your code since simple variable names are widely used within the for loop's declaration.
Also, what do you mean it may take them 'hours'? Just simply looking at the declaration of the for loop and you know what it is.
That may be your style, so just don't tell him he should use your way, especially since it seems the more widely used method is what he is using. You could just give him it as a suggestion, but I think they way you worded your post, it looks like someone would get confused unless they used your method(which obviously isn't true).
Well it is a matter of style, but just because everyone else does it does not make it correct. You should make your variable names mean something, and not just 'i' 'n' 'ii' 'foo' 'bar'
-
March 4th, 2004, 06:26 PM
#7
Originally posted by Mick
Well it is a matter of style, but just because everyone else does it does not make it correct. You should make your variable names mean something, and not just 'i' 'n' 'ii' 'foo' 'bar'
We're talking about variables used as an index in a for loop, not something very important
-
March 4th, 2004, 06:43 PM
#8
We're talking about variables used as an index in a for loop, not something very important
And don't you think that something like iLoop is more clear than just i???
And as Mick said
Well it is a matter of style, but just because everyone else does it does not make it correct. You should make your variable names mean something, and not just 'i' 'n' 'ii' 'foo' 'bar'
-
March 4th, 2004, 07:04 PM
#9
Originally posted by defiler_z
And don't you think that something like iLoop is more clear than just i???
No, I think i is quite clear.
-
March 4th, 2004, 07:17 PM
#10
Originally posted by kasracer
No, I think i is quite clear.
That's fine to each his own. But you wouldn't be doing it anywhere near me. You cannot defend your position. Sorry.
-
March 4th, 2004, 09:54 PM
#11
Originally posted by kasracer
Where did you learn C++ programming? Every book I have read and pretty much all programmers I've ever talked to, very simple variables like 'i' and 'y', ect.. are used alot in for loops.
Variable names should generally express what the variable represents - or we could just as well be using memory addresses instead of identifiers. There are some examples of loop counters which have no other meaning than being a loop counter - they are traditionally called "i" (for "iterator" or "index"). That tradition goes back to the days of early FORTRAN and BASIC dialects, where variable names were restricted to one character and a digit - there's no need to keep that up in modern languages. And names like "x" and "y" are fine if that is the meaning of the variable - the x and y coordinates in a bitmap, for example. Yes, C textbooks often use names like "i" and "j" when they want to demonstrate some general concept and the variable names don't matter - but that doesn't mean you should use those names in real-world applications. Or do you name your functions f(), g() and foo() just because you saw that in many textbooks?
Originally posted by kasracer
Even I do it.
Even you?! Well, in that case... 
Originally posted by kasracer
Making long variable names in a for loop is.... well kind of wasteful
Wasteful? Wasting what? A few bytes of source code space on a multi-GB harddisk?
Originally posted by kasracer
and, in fact, it may confuse others looking at your code
Short, meaningless names are much more likely to confuse others than long, meaningful names.
Originally posted by kasracer
since simple variable names are widely used within the for loop's declaration.
Widely used in generic examples - but you need to make the transition from textbook examples to the real world. Especially in the OP's code, the loop variables have the meaning of StudentId, CategoryId and GradeId (as pointed out by Linenoise), so it should be more than natural to name them like that.
-
March 4th, 2004, 11:17 PM
#12
Originally posted by Mick
You cannot defend your position. Sorry.
I already have
Originally posted by gstercken
Variable names should generally express what the variable represents - or we could just as well be using memory addresses instead of identifiers. There are some examples of loop counters which have no other meaning than being a loop counter - they are traditionally called "i" (for "iterator" or "index"). That tradition goes back to the days of early FORTRAN and BASIC dialects, where variable names were restricted to one character and a digit - there's no need to keep that up in modern languages. And names like "x" and "y" are fine if that is the meaning of the variable - the x and y coordinates in a bitmap, for example. Yes, C textbooks often use names like "i" and "j" when they want to demonstrate some general concept and the variable names don't matter - but that doesn't mean you should use those names in real-world applications.
If it ain't broke, don't fix it
Originally posted by gstercken
Or do you name your functions f(), g() and foo() just because you saw that in many textbooks?
Obviously I don't. We're talking about variables used for very simple loop counting. I've specifically said within for loops.
Originally posted by gstercken
Short, meaningless names are much more likely to confuse others than long, meaningful names.
It's in a ****ing for loop, how the **** is someone going to get confused? Honestly, they would have to be quite bad at programming to get confused from a for loop counter.
Originally posted by gstercken
Widely used in generic examples - but you need to make the transition from textbook examples to the real world. Especially in the OP's code, the loop variables have the meaning of StudentId, CategoryId and GradeId (as pointed out by Linenoise), so it should be more than natural to name them like that.
In several Open-Source projects I've looked out, I've never seen people actually use variable names like that within loops. In Linux I never seen it either. **** I run Gentoo at home and I can look at alot of the code going to be emerged and as it's emerging and it's always something like 'i'. ****, even on the MSDN most examples and programs made for people do the samething.
-
March 5th, 2004, 11:43 AM
#13
I like kasracer's point. If it ain't broke, don't fix it! lol
I mean really, this is just about as pointless as arguing over religion. No one will change their minds, the only thing that will happen is endless debates with no one listening or considering others' opinions.
I do agree with the debaters when they say "to each his own".
Everyone has their own styles, and that's respectable. A style in programming is kind of, to me anyway, a signature that says "hey, I made this!"
I'm not a very professional programmer, heck I can't even program windows yet... only console. But, I do know enough to know that if you can't identify what a variable does, regardless of it's name, ONLY within a for loop, then you must be totally stupified.
Consider a 4 line or even 20 line FOR loop. If you decide to use incremental counters with single byte names, then so be it! If the person reading it can't bother to scroll up to the local definition in the FOR syntax and figure out that it is the counter, they have problems!
Some would say more meaningful names equal better readability, others would say it makes the code look more compact, therefore resulting in a greater difficulty on readability.
I'm going to stick with MY style, because it is my signature. I think everyone else should do the same. Honestly, if we keep talking about this, is ANYONE going to change their religion? I don't think so lol
So, case closed! Let it all go guys!
The point of this thread was for my inquiry, which was properly answered for the most part, until this debate arrived. Thanks guys, I really appreciate the help... but I'm afraid things are getting a bit out of hand! I'm not moderator, but just a suggestion 
Take care guys! Thanks again!
-
March 5th, 2004, 12:04 PM
#14
Originally posted by MrDoomMaster
I'm not a very professional programmer, heck I can't even program windows yet... only console.
Whether you only write console programs isn't the test to see if you are a good programmer.
There are many excellent professional programmers that only write console programs, and they don't know anything about Windows programming.
You can write very complex console programs that have nothing to do with GUI. Linux is a "console" program itself.
Regards,
Paul McKenzie
-
March 5th, 2004, 12:35 PM
#15
It is my opinion that (almost) everybody uses single-character identifiers for for-loops, and the widely spread identifiers i, j and k for loops shouldn't confuse anyone.
Howerver, one thing I would note on the OP's code is the use of i, ii and iii as iterators. Now those can be cofusing, and in anything but a very short loop, it could lead to one **** of a debugging session. It's way to easy to mistype ii into iii or vice-versa. Plus, reading the code, you have to actually make an extra effort to perfectly distinguish whether it's i, ii or iii being used. My recommendation for the for loops would be to use single-character identifiers for the for loops (since it's widely spread and (almost) everybody knows what it means), but use different letters (for ease of debugging).
Originally posted by gstercken
There are some examples of loop counters which have no other meaning than being a loop counter - they are traditionally called "i" (for "iterator" or "index").
Somehow, I always thought that the use of i and j as iterators was because they are usually used to iterate in an array (1 or 2 dimension), and during design, those arrays are usually represented as tables, looking like matrices (usually when arrays of simple things, like integers). Since matrices use the ij notation to refer to an element, I always thought it was natural to refer to an element of an array this way inside for loops. Never thought of it as meaning index or iterator (much less jindex and kindex ) ... my 2 cents.
After three days without programming, life becomes meaningless.
- The Tao of Programming, book 2
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|