Newbie ... looping problem

This is a discussion on Newbie ... looping problem within the C++ Programming forums, part of the General Programming Boards category; I wrote this basic encrytion program and everything seems to work find until the end of the program when it ...

  1. #1
    Registered User StupidIntel's Avatar
    Join Date
    Mar 2004
    Posts
    7

    Newbie ... looping problem

    I wrote this basic encrytion program and everything seems to work find until the end of the program when it asks the user if they want to restart the program.

    It'll print out, "Enter a string no longer than 20 characters long:"... but it doesn't wait for input... just continues with the program.

    Does anyone know what's causing this and how I can fix it?

    Code:
    #include <iostream.h>
    #include <ctype.h>
    #include <stdlib.h>
    
    int main()
    {
       char ans;
       do
       {
          //Declarations
            char string[21];
            char decrypt;
            int i;
    
          //Get string from user
            cout<<"Enter a string no longer than 20 characters long:"<<endl;
            cin.get(string,20);
            cin.ignore(80,'\n');
            cout<<endl;
    
          //encrypt 
            for(int a = 0; a <= 20; a++)
            {
               if(isupper (string[a]))
                  i = (int) 'A';
               else if(islower ((int) string[a]))
                  i = (int) 'a';
    
               if(isalpha(string[a]))
               {
                  string[a] = string[a] - i;
                  string[a] = string[a] + 1;
                  string[a] = string[a] % 26;
                  string[a] = string[a] + i;
               }
            }
    
          //Print out the encrypted string
            cout<<"The encrypted string is: "<<string<<endl<<endl;
    
          //Ask for decrypt
            cout<<"Would you like to decrypt this string? (y,n) ";
            cin>>decrypt;
            cout<<endl;
    
          //Decrypt and output
            if(decrypt != 'n') //decrypt?
            {
               for(int j = 0; j <= 20; j++) //decrypt individual characters
               {
                  if(isalpha(string[j]))
                  {
                     if(isupper (string[j]))
                        i = (int) 'A';
                     else if(islower ((int) string[j]))
                        i = (int) 'a';
    
                     string[j] = string[j] - i;
                     string[j] = string[j] - 1;
                     if(string[j] < 0)
                     string[j] = 25;
                     string[j] = string[j] + i;
                  }
               }
               cout<<endl<<endl<<"The decrypted string is: "<<string<<endl<<endl;
            }
    
          //Run again code
            cout<< "Would you like to run this program again? (y,n) ";
            cin>>ans;
            cin.sync();
    
       }while(ans != 'n');
       return 0;
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > cout<<"Would you like to decrypt this string? (y,n) ";
    > cin>>decrypt;
    > cout<<endl;

    Try adding a cin.ignore() here like you did for the cin.get() above:
    cout<<"Would you like to decrypt this string? (y,n) ";
    cin>>decrypt;
    cin.ignore(80,'\n');
    cout<<endl;

  3. #3
    Registered User StupidIntel's Avatar
    Join Date
    Mar 2004
    Posts
    7
    another thing... @ the bottom of the program I had getch(), not cin.sync() ... I changed it when i had a friend look at the code. The program worked for him, but it's still not working on my computer. If it's working for you guys... I'm thinking it's a compiler thing... but I know so little about this that I'm not sure what to do.

  4. #4
    Registered User StupidIntel's Avatar
    Join Date
    Mar 2004
    Posts
    7
    Quote Originally Posted by swoopy
    > cout<<"Would you like to decrypt this string? (y,n) ";
    > cin>>decrypt;
    > cout<<endl;

    Try adding a cin.ignore() here like you did for the cin.get() above:
    cout<<"Would you like to decrypt this string? (y,n) ";
    cin>>decrypt;
    cin.ignore(80,'\n');
    cout<<endl;
    Tried that, didn't work.

  5. #5
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Code:
    cout <<"Enter a string no longer than 20 characters long:"<<endl;
    cin.get(string,20);
    The problem is at that line, right? By default, your cin.get() will stop getting input when reading a newline; therefore, try removing endl;
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  6. #6
    Registered User StupidIntel's Avatar
    Join Date
    Mar 2004
    Posts
    7
    Quote Originally Posted by alphaoide
    Code:
    cout <<"Enter a string no longer than 20 characters long:"<<endl;
    cin.get(string,20);
    The problem is at that line, right? By default, your cin.get() will stop getting input when reading a newline; therefore, try removing endl;
    That doesn't do it either...

  7. #7
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    How about using cin.getline() instead of cin.get()? Also, after each "cin >> (something);", try either putting an ignore or else just call getline() again to extract the '\n' at the end of the input.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  8. #8
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    The following works in VC++ .NET. I have to change the includes to standard one because my compiler couldn't find .h versions. You have to fix the loop which goes through each character. Your code always loop 21 times (0 to 20) when the string's character could be less than that. So check, the length and loop accordingly. I didn't fix anything related to the cin.get() issue because there's nothing to fix for me. So your problem could be compiler specific. Try www.bloodshed.net
    Code:
    #include <iostream>
    #include <cctype>
    #include <cstdlib>
    
    using namespace std;
    
    int main()
    {
       char ans;
       do
       {
          //Declarations
            char string[21];
            char decrypt;
            int i;
    
          //Get string from user
            cout<<"Enter a string no longer than 20 characters long:"<<endl;
            cin.get(string,20);
            cin.ignore(80,'\n');
            cout<<endl;
    
          //encrypt 
            for(int a = 0; a < strlen(string); a++)
            {
               if(isupper (string[a]))
                  i = (int) 'A';
               else if(islower ((int) string[a]))
                  i = (int) 'a';
    
               if(isalpha(string[a]))
               {
                  string[a] = string[a] - i;
                  string[a] = string[a] + 1;
                  string[a] = string[a] % 26;
                  string[a] = string[a] + i;
               }
            }
    
          //Print out the encrypted string
            cout<<"The encrypted string is: "<<string<<endl<<endl;
    
          //Ask for decrypt
            cout<<"Would you like to decrypt this string? (y,n) ";
            cin>>decrypt;
            cout<<endl;
    
          //Decrypt and output
            if(decrypt != 'n') //decrypt?
            {
               for(int j = 0; j < strlen(string); j++) //decrypt individual characters
               {
                  if(isalpha(string[j]))
                  {
                     if(isupper (string[j]))
                        i = (int) 'A';
                     else if(islower ((int) string[j]))
                        i = (int) 'a';
    
                     string[j] = string[j] - i;
                     string[j] = string[j] - 1;
                     if(string[j] < 0)
                     string[j] = 25;
                     string[j] = string[j] + i;
                  }
               }
               cout<<endl<<endl<<"The decrypted string is: "<<string<<endl<<endl;
            }
    
          //Run again code
            cout<< "Would you like to run this program again? (y,n) ";
            cin>>ans;
            cin.sync();
    
       }while(ans != 'n');
       return 0;
    }
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  9. #9
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    oh, here's the screenshot
    Code:
    Enter a string no longer than 20 characters long:
    abcdefg
    
    The encrypted string is: bcdefgh
    
    Would you like to decrypt this string? (y,n) y
    
    
    
    The decrypted string is: abcdefg
    
    Would you like to run this program again? (y,n) y
    Enter a string no longer than 20 characters long:
    kj dpoja kj sf
    
    The encrypted string is: lk eqpkb lk tg
    
    Would you like to decrypt this string? (y,n) y
    
    
    
    The decrypted string is: kj dpoja kj sf
    
    Would you like to run this program again? (y,n) n
    Press any key to continue
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  10. #10
    Registered User StupidIntel's Avatar
    Join Date
    Mar 2004
    Posts
    7
    I got it working!

    I replaced cin.sync @ the end of the program w/ cin.ignore and i put a cin.ignore after every cin and that fixed the problem.

    I also used your idea, alphaoide, thanks!

  11. #11
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Quote Originally Posted by StupidIntel
    I got it working!

    I also used your idea, alphaoide, thanks!
    Code:
    for(int a = 0; a < strlen(string); a++)
    change that into
    Code:
    length = strlen(string);
    for (int a = 0; a < length; a++)
    that way function strlen needs not to be called repeatedly
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  12. #12
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>I put a cin.ignore after every cin and that fixed the problem.
    Yes, that's because cin.get() leaves the '\n' in the input stream; so the next time cin.get() gets run, it finds the '\n' and returns an empty string - again without extracting the '\n'. getline() acts the same as get(), except it removes the '\n' so you don't have to do the ignore. Of course, I believe you still would need the ignore()'s after each cin >> (although I could be mistaken about that).
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  13. #13
    Registered User StupidIntel's Avatar
    Join Date
    Mar 2004
    Posts
    7
    Thanks for the info!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structure Array Looping Problem
    By Vitamin_C in forum C++ Programming
    Replies: 2
    Last Post: 12-17-2005, 03:22 PM
  2. Newbie in problem with looping
    By nrain in forum C Programming
    Replies: 6
    Last Post: 11-05-2005, 12:53 PM
  3. Problem with looping.
    By rina in forum C Programming
    Replies: 9
    Last Post: 10-05-2005, 02:21 AM
  4. newbie Windows problem...
    By gcn_zelda in forum Windows Programming
    Replies: 9
    Last Post: 06-07-2003, 03:14 PM
  5. newbie coding problem
    By rippascal in forum C++ Programming
    Replies: 10
    Last Post: 01-08-2002, 11:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21