Thread: need help with my do...while

  1. #1
    Registered User
    Join Date
    Dec 2019
    Posts
    1

    need help with my do...while

    Hello!

    I have completed a basic program, and it consists of a while loop and it runs sufficiently. I am trying to add a loop so that the user is prompted to rerun the program. This additional loop is causing unwanted behavior. I think I am just tired, but I had it fixed then broke it again. Now I am stuck.

    suggestions are appreciated.

    Code:
    int main()
    {
        char temple[15][5] = { ".-","-" };
        char input[25];
        char letnum[15] = { 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
                         'Q','R','S','T','U','V','W','X','Y','Z','1','2','3','4','5','6',
                         '7','8','9','0',' ' };
        int i = 0, j, k;
        cout << "Please enter a message to be converted to temple Code,";
        cout << "\nand no more than 50 characters in length: ";
    
        cin.getline(input, 15);
        while (input[i] != '\0')
        {
            for (j = 0; j < 15; j++)
            {
                if (toupper(input[i]) == letnum[j])
                {
                    for (k = 0; k < strlen(temple[j]); k++)
                        cout << temple[j][k];
                    cout << " ";
                }
            }
            i++;
        }
    
        cout << endl;
        system("pause");
        return 0;
    }
    I want to opt the user to run or not run the program, so I have this:

    Code:
    int main()
    
    {
        char again;        //User loop option
    
        do     // do ... while
        {
        char temple[15][5] = { ".-","-...",};
        char input[25];
        char letnum[15] = { 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
                         'Q','R','S','T','U','V','W','X','Y','Z','1','2','3','4','5','6',
                         '7','8','9','0',' ' };
        int i = 0, j, k;
        cout << "Please enter a message to be converted to temple Code,";
        cout << "\nand no more than 50 characters in length: ";
    
        cin.getline(input, 15);
        while (input[i] != '\0')
        {
            for (j = 0; j < 15; j++)
            {
                if (toupper(input[i]) == letnum[j])
                {
                    for (k = 0; k < strlen(temple[j]); k++)
                        cout << temple[j][k];
                    cout << " ";
                }
            }
            i++;
        }
    
        cout << endl;
        cout << "Do you want to run the program again? (Y/N) ";  // prompts user
        cin >> again;       // input
    
    } while (tolower(again) == 'y');    // for case, calls 'again'
    
        system("pause");
        return 0;
    }
    When I add the do...while, it loops in an undesired way. It does not allow the user to enter values. It just skips my getline and goes to the cin>>again.
    Last edited by jessica93; 12-08-2019 at 01:03 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It may be easier if you move your original working code into a function, then call the function from main within the do while loop.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Tags for this Thread