CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Array help

  1. #1
    Join Date
    Dec 2010
    Posts
    4

    Array help

    This is my first class with C++ and I'm having some trouble with this array.

    I an array int guesses[100] and I set up a loop so that the user enters different integers into each element. the user input would be guesses[guess]

    i need to compare the current user input with previous inputs in the array. so i set up another loop
    for (int i=0 ; i <= MAX_GUESSES ; i++)

    then i compared

    if ( guesses[guess] == guesses[i] ) {

    ........

    }

    This didn't work but I don't know why.help me out here. and please try to remember that I'm only just learning this.

  2. #2
    Join Date
    Dec 2010
    Posts
    4

    Re: Array help

    Also, the code is attached:
    Attached Files Attached Files

  3. #3
    Join Date
    Aug 2008
    Posts
    902

    Re: Array help

    Oh my. I can't believe you are using GOTO in a "C++" program like this. There are time in which it's use may be nesisarry, but for a beginner like you, you can go ahead and make a mental note to never ever use GOTO, ever.

    Also,

    Code:
    #include <stdlib.h>
    #include <time.h>
    Those are C header files. If you really must use C functions, then use:

    Code:
    #include <cstdlib>
    #include <ctime>

  4. #4
    Join Date
    Oct 2007
    Posts
    132

    Re: Array help

    In your inner loop (when you are comparing the guess) you are looping from 0 - MAX_GUESSES -- but you only want to loop through the guesses that exist (i.e. you have only filled out guesses from 0 - guess, not 0 - MAX_GUESSES


    Also, remember that c++ arrays are zero-based indexes, so you want to loop 0 through (MAX_GUESSES - 1) in your outer loop, or you will run into trouble since guesses[MAX_GUESSES] is not defined. You can fix this simply by using "<" rather than "<=" in your outer loop.

  5. #5
    Join Date
    Dec 2010
    Posts
    4

    Re: Array help

    just like english might not be your first language, this isn't mine. my prof lets me use them without deducting points so why not? i don't understand why stroustrup would have implemented this in the language when it is somewhat 'forbidden'.

  6. #6
    Join Date
    Aug 2008
    Posts
    902

    Re: Array help

    Quote Originally Posted by cjpaul View Post
    just like english might not be your first language, this isn't mine. my prof lets me use them without deducting points so why not? i don't understand why stroustrup would have implemented this in the language when it is somewhat 'forbidden'.
    Well, I question how good of a professor he is then. They are there, but that doesn't mean they should be used. If you are using them for something, you should have a real good reason for it. GOTO's inevitably lead to spaghetti code, and in addition, virtually anything that can be done with a GOTO statment can be done much neater with a loop. I can pretty much guarantee you you will never be hired as a C++ programmer (if that's your goal) if you simply ignore advice and create code littered with GOTOs. It's bad for a reason. I wouldn't even consider your code to be a real example of C++. It's really just BASIC in disguise.

    EDIT: As a side note, GOTO isn't a C++ language feature, it's a C language feature. God knows why it was even kept in C, but the only reason it's there in C++ is because it maintains backwards comparability with C.
    Last edited by Chris_F; December 12th, 2010 at 05:18 AM.

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Array help

    Quote Originally Posted by cjpaul View Post
    just like english might not be your first language, this isn't mine. my prof lets me use them without deducting points so why not?
    Then your professor isn't teaching you programming. You should get your money back (if you paid for the course). I don't know of one professor (and I know many) that would encourage goto, and most of them would deduct points from any code that uses it.

    Secondly, code littered with "goto" will not be looked at by most people that may want to help you. The reason is as what Chris_F stated -- your code will be spaghetti.

    http://en.wikipedia.org/wiki/Spaghetti_code

    No one is going to waste time untying all of those goto knots. The code with "goto" all over the place has no structure, no logical paths, etc. So if you want to get any substantial help here, in other programming forums or websites, or even from other students/professors, ditch using "goto" and use structured concepts.

    You should be learning structured programming, and using the proper looping constructs (for, while, do-while), not goto.
    i don't understand why stroustrup would have implemented this in the language when it is somewhat 'forbidden'.
    You and your professor should read E. Dijkstra's "Goto Considered Harmful".

    Also, "goto" exists in most languages -- Stroustrup adopted it from 'C', and 'C' adopted it from most other mid to high-level languages at the time 'C' was being developed. It is a legacy command that has been around a long time, but should rarely, if ever be used, and if the program is a beginner app such as yours, it shouldn't be used at all.

    Regards,

    Paul McKenzie

  8. #8
    Join Date
    Dec 2010
    Posts
    4

    Re: Array help

    Hey Paul,

    Thanks for actually responding politely.

    My prof has genuinely expressed how we should stay away from goto statements, and has told us all about why they are bad for a program. He does deduct points if they are used in higher level classes.

    And I doubt I will read that book as this will most likely be my only programming class throughout my schooling. If I do ever move on with this subject, I will take the time to learn proper coding.

    Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured