![]() |
|
#1
|
|||
|
|||
messy loop help pleaseHi, how are you all?
I'm a C++ novice and am having trouble with this fairly messy loop structure I've created. At the moment it's an infinite loop and I'm not sure why. The goto part should exit the whole loop and the break part should go back to the main, start while loop. 'c' is an integer the user enters (3 for example) which determines how many values are in the array. The loop is meant to go through the array and find the highest entry (also input by the user). I hope you can understand what it's trying to do. I'd appreciate any help on getting it to work. Thanks Sam CPP / C++ / C Code:
|
|||
|
#2
|
||||
|
||||
|
I don't feel extremely comfortable w/ C++ yet (only been using for <1 year), but I have learned a few things.
First off, don't use goto. It's not used in structured programming and usually it's use means the programmer didn't design the program well. You should never have to use a goto statement because there are better ways to accomplish the same idea. In this code, just replace the "goto end" with your cout statement; there's no value-added by using "goto". If written correctly, the loop will exit normally with no problem. Additionally, I'm careful about using breaks. Usually I only use them with switch statements or nested if/else statements. Loops should be written so the controlling argument stops the loop gracefully, e.g. when the loop condition becomes false. However, I don't see where 'n' is ever incremented. If 'n' remains equal to '0', then you will always be in a loop because CPP / C++ / C Code:
__________________
Start Programming with Python-A beginner's guide to programming and the Python language. ------------- Common Sense v2.0-Striving to make the world a little bit smarter. |
|
#3
|
|||
|
|||
|
Thanks for the input. I don't know of any other way to get out of multiple loops other than goto though. Replacing it with the cout stuff would still mean the outer loops would be executed more times. n is incremented by being equal to g after g is incremented ( the line 'n = g' ).
Any other ideas? thanks Sam |
|
#4
|
||||
|
||||
|
While loops will automatically "break" and return control to the outer loop when the stopping condition is reached (if it's only one loop then it returns control to main() ), so if your loop is coded correctly you shouldn't require a break or goto statement. (Hope that makes sense.)
You can use a forever loop instead of a while loop; it's a new feature of C++. The format is CPP / C++ / C Code:
Another thing I noticed is that your conditional for the goto line may be wrong. CPP / C++ / C Code:
__________________
Start Programming with Python-A beginner's guide to programming and the Python language. ------------- Common Sense v2.0-Striving to make the world a little bit smarter. |
|
#5
|
||||
|
||||
|
Quote:
Quote:
CPP / C++ / C Code:
Also, you might find the forever loop works better like this: CPP / C++ / C Code:
sammacs, is your task simply to find the index of the largest value in the array? If so, you are making it much more complicated than it needs to be. If n is to be the index of the largest value,
__________________
During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence? |
|
#6
|
|||
|
|||
|
Hi,
I've managed to get it to work. Thanks for all your help. I used a while loop in the end but used a similar format to what WaltP suggested. It turned out very simple in the end. Thanks again, Sam |
|
#7
|
||||
|
||||
|
Hey Walt, thanks for the catches. I was thinking of the fact that switch can be used in placed of if/else and forgot that if/else doesn't require a break.
I didn't know that forever loops were part of C from the beginning. My C textbook never mentioned it and my C++ book dedicated a whole section to them, making it sound as if forever loops were a new feature of C++. Guess I need more practice. <shrug> __________________
Start Programming with Python-A beginner's guide to programming and the Python language. ------------- Common Sense v2.0-Striving to make the world a little bit smarter. |
Recent GIDBlog
Programming ebook direct download available by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Nested for loop with function | Tori | C++ Forum | 11 | 08-Nov-2004 14:02 |
| [Header]Classifying the FOR loop | mithunjacob | C++ Forum | 0 | 20-Jun-2004 17:39 |
| Program using a for loop | tommy69 | C Programming Language | 3 | 10-Mar-2004 23:16 |
| coding a sentinel controlled loop | tommy69 | C++ Forum | 2 | 10-Mar-2004 15:52 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The