Solved

C++ Problem

11 answers Last reply Best Answer
More about problem
  1. Best answer
    I would stop staring and start debugging, you might get further. ;)

    Seriously though, without going into a huge debugging lesson, you have several ways to solve this problem. It's also a good chance to learn _necessary_ basic debugging skills since this is obviously a simple "learning to program" app.

    1) Brute force using IDE - if you have a nice IDE & debugger, set a breakpoint somewhere early, where you know the code still works. Then just step through the code line by line, making sure the results are what you expected as you progress.

    OR

    2) Brute force using code - sprinkle those "cout" statements all over the place to 'tell' you the state of your variables and your location in the code. Then just follow along when you run it so you know the code is progressing as you expected.

    OR

    3) Break it down into the smallest possible program that works, then gradually put it back together piece by piece.

    Some other tips - look for some of the the obvious problems first -- Variables initialized properly, semi-colons in the right places (especially during flow-control like do's and while's), code flowing the way you expect it to, case sensitivity, variable naming conflicts, yada yada yada.

    Also make sure you're not testing variables without initializing them first.

    Using these tips, you should have this debugged in just a few mins. :)

    Good luck!
  2. Have you got your compillers and debugging options right.
    I dont really work with c++ more of a c# python and java person and so if there was to be any errors check where it is. trust me you may have put one of the stupidest mistakes like grammer.
    Always hated checking my grammer ;)
  3. I will go through every step you guys have given me...and yes I am learning, but it's more or less of I wanted to get the do{}while() loop down so that I will feel comfortable moving on in my book. I also have to get a minor thing with arrays down also. But thanks.
  4. Well I found out that the problem was something with the gets() function. I don't know what.
  5. gets() is a bit of a dodgy function. It doesn't care that your username array is 50 elements in size, it will just keep on reading every character that gets entered and overwriting memory outside your buffer. It's dangerous, and you should use another function.

    Now the actual problem is probably being cause by gets() leaving a newline character at the end of the input stream once it is finished reading. Newline and EOF characters are what it uses to delimit input, but it doesn't actually add them to the buffer. When the next call to gets() is made, it grabs the newline character still sitting in your input stream and immediately stops reading. Of course it still leaves that newline character there for the next call as well ;)
  6. Use getline(cin, buffer) for input, not gets().
  7. it worked fine on my compiler...i use an old turbo c++ compiler though :-p
    check your compiler...the code seems fine and should've worked.
  8. shattered space said:
    Well I found out that the problem was something with the gets() function. I don't know what.


    Use anything other then gets(); very unreliable C function. getline(cin, buffer) is very, very much preferred.

    And take this as a lesson in debugging and stepping through code. If you do any serious SW work, you'll be doing more of that then actual coding. [Trust me on this].
  9. It compiled fine for me but you need to work on your style, try using std::string as opposed to c strings it makes input and output a lot easier, also try making your variable names a bit shorter and camel case them instead of securityanswer type securityAnswer, and use spacing especially between control structures, it will make it a lot easier to spot errors next time.
  10. Best answer selected by shattered space.
Ask a new question

Read More

Programming Apps