Results 1 to 10 of 10

Thread: Why is this program not working?

  1. #1

    Question Why is this program not working?

    Ok, eventually this program is going to encode messages (I am learning how to program) but for some reason it isn't stopping at my getline command, can you tell me why?
    Here is the code:

    #include <iostream>
    #include <string>

    using namespace std;


    int main()
    {
    bool progTerm = false;
    while(progTerm == false)
    {
    char instring[1001];
    char outstring[1001];
    int choice = 0;
    cout << "Welcome to Caeser, Please type the number of your selection:" << endl;
    cout << "1. Encode Message" << endl << "2. Decode Message" << endl << "3. Exit" << endl;
    cin >> choice;
    switch(choice)
    {
    case 1:
    cout << "Type your message(max of 1000 characters):" << endl;
    cin.getline(instring, 1001, '\n');
    cout << instring;
    break;
    case 2:

    break;
    case 3:
    progTerm = true;
    break;
    }


    }
    return 0;
    }

    Also, here is the .exe file, as you can tell it skips right by asking for the string.
    http://home.comcast.net/~dwcnmv/program.exe
    --------------------------------------------------------------------
    E, the modern pi.

  2. #2
    Senior Member
    Join Date
    Feb 2003
    Posts
    282
    My sence is that cin is buffered. When you are presented with the choice you type 1 and hit enter, the enter is still in the buffer, and this makes getline think it is done

    try placeing a cin.ignore(80,'\n'); above each getline

    EDIT you may instead want to just call cin.ignore(80, '\n'); before the switch.

    Also try useing cin.get instead

  3. #3
    The problem isn't what journy101 said. The problem lies in the fact that you have an infinite loop condition. Look at your while(progTerm == false) test. The ONLY way progTerm is true is if the user enters 3 as his choice, which means that's the only time you can break the loop. Since you're in that infinite loop when you type in some text, it outputs the instring infinitely. The break statement after you ouput the instring only breaks you from the switch statement, not the while loop (which will continue to execute because there isn't a way to break out of it).

    If you need some more help, feel free to ask.

    -noix-

  4. #4

    reply

    Thanks for the input guys, umm... i'm sure the loop thing contributed to it, but this works just fine: cin.ignore(80,'\n'); it made it stop at the string input. Thanx again.
    --------------------------------------------------------------------
    E, the modern pi.

  5. #5
    hmm, it's bad programming practice not to address bugs such as the infinite loop. However, they are sometimes a necessary evil; even I use them when I code games, but keep in mind that your code is extremely buggy. Even though you found a little workaround, it doesn't mean the bug is gone.

    -noix-

  6. #6
    Senior Member
    Join Date
    Jun 2003
    Posts
    236
    Nitro,

    Why is it buggy to have infinite loops?If your program never si supposed to end then that is fine. Here is a case I used, I developed an application that was to read packets and alert on them. It was never supposed to stop unless it received a system signal. Therefor I just set my signal handlers in the beginning and went into an infinite loop.

    In this case there is not even an infinite loop,(except you might want to flush the cin buffer), if coded right it will exit when the user enters 3.


    dwcnmv
    Did you get this working now??
    That which does not kill me makes me stronger -- Friedrich Nietzche

  7. #7

    yea

    Yea it works with just doing the buffer thing thanx for the ideas, lol I am trying to figure out how to work pgp so I can make my own little pgp thing for fun. Very confusing though
    --------------------------------------------------------------------
    E, the modern pi.

  8. #8
    Senior Member
    Join Date
    Jun 2003
    Posts
    236
    You should look into Botan. Its a c++ library of encryption functions, maybe a little to difficult for a beginner but it can do all sorts of encrytion styles, you should try symmetric before you do start doing public/private keys like pgp.
    That which does not kill me makes me stronger -- Friedrich Nietzche

  9. #9
    I didn't necessarily mean it was buggy to have infinite loops. I even said that I use them for real-time apps. The part that is buggy is when you don't have a way out of the loop, or you do, but your program can't ever get there due to some other reason. Anyway, good luck with your encryption app.

    -noix-

  10. #10
    AO's MMA Fanatic! Computernerd22's Avatar
    Join Date
    Mar 2003
    Location
    Miami, FL
    Posts
    795
    Why is this program not working?

    Cause you dont know what the hell you're doing.

Posting Permissions

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