0

I am suppose to create a program such that when 'y' is entered it will execute the code in the first do-while loop. however, when 'y' is entered it just skips over the chunk of codes!

Enter a phone symbols:
a
2

Continue (Y/y): y

Enter a phone symbols:

Continue (Y/y):

Is there anyway I can solve this without using cin.ignore as it changes the layout. Thanks in advance!

#include <iostream>
#include <cstdlib>
#include <cctype>
#include <iomanip>
#include <cstring>

const int MAX = 100;

using namespace std;

int getInt(char);
bool isValidChar(char);

int main()
{
    int num;
    int j = 0;
    char name, conti;
    char alpha[MAX];

    do {
        cout << "Enter a phone symbols: " << endl;
        cin.getline(alpha, MAX);
        // cin.ignore(100, '\n');

        while (alpha[j] != '\0')
        {
            name = alpha[j];

            if (isValidChar(name) == true)
            {

                num = getInt(name);

                if (num == -1)
                {
                    cout << "-";
                }
                else
                {
                    cout << num;
                }
            }
            else
            {
                cout << " - Invalid Char " << name << " found - rejected";
            }

            j++;
        } // end while

        cout << endl;

        do {
            cout << "\nContinue (Y/y): ";
            cin >> conti;
            cout << "\n" << endl;
            conti = tolower(conti);

            if (conti == 'n')
            {
                exit(0);
            }

        } while (conti != 'n' && conti != 'y');

    } while (conti == 'y');

}

int getInt(char input)
{
    int result;
    char value;
    value = tolower(input);
    if ((value >= 'a' && value <= 'c'))
    {
        result = 2;
    }
    else if ((value >= 'd' && value <= 'f'))
    {
        result = 3;
    }
    else if ((value >= 'g' && value <= 'i'))
    {
        result = 4;
    }
    else if ((value >= 'j' && value <= 'l'))
    {
        result = 5;
    }
    else if ((value >= 'm' && value <= 'o'))
    {
        result = 6;
    }
    else if ((value >= 'p' && value <= 's'))
    {
        result = 7;
    }
    else if ((value >= 't' && value <= 'v'))
    {
        result = 8;
    }
    else if ((value >= 'w' && value <= 'z'))
    {
        result = 9;
    }
    else if (value == ' ')
    {
        result = -1;
    }
    return result;
}

bool isValidChar(char value)
{
    if (isalpha(value) || value == ' ')
    {
        return true;
    }
    else
    {
        return false;
    }
}
David G
  • 94,763
  • 41
  • 167
  • 253
Isabella Chan
  • 41
  • 1
  • 10
  • That inner nested do-while loop isn't needed. Also, use `std::string`. – David G Jan 30 '14 at 23:57
  • 1
    I got the code to work by taking out the unneeded nested do-while loop, resetting `j` to `0` after the inner `while` loop, and adding `ss.ignore()` after `cin >> conti`. The `ignore()` call is needed as unformatted input will not be able to proceed if there is a new line stuck in the beginning of the stream. [**You can find your working code here.**](http://ideone.com/2ZaMIb) – David G Jan 31 '14 at 00:14
  • Hi 0x499602D2 sorry it is still not working. the output that i have got is this. Enter a phone symbols: call heng 2255 Continue (Y/y): Press any key to continue . . . – Isabella Chan Jan 31 '14 at 02:58
  • Yeah thats my fault. Change the code from `cin.getline(alpha, MAX, ' ');` to `cin.getline(alpha, MAX);`. – David G Jan 31 '14 at 03:01

1 Answers1

0

What do you mean as: Is there anyway I can solve this without using cin.ignore as it changes the layout.?

Your layout is contolled, basicly by this line:

cout << "\nContinue (Y/y): ";
cin >> conti;
cout << "\n" << endl;   // <-- this one
conti = tolower(conti);

If you supress it, there will be no empty line. Otherwise, if you change it to

cout << endl;

Just one empty line.

Amadeus
  • 10,199
  • 3
  • 25
  • 31