lundi 22 février 2016

If and else Statements in While loop" for C++

I have a question regarding if & else statements in a while loop.

I wanted to establish a few things in my program:

  • wanted user to only input 4 letter characters without the use of numbers and symbols.
  • converting each those letters into ints(Ascii val.)

To make it easier to understand if confused,

Program 1st asks for 4 letter word. User places input. If input contains at least:

  • a non-letter character, then Program asks User for new Input.

If input only contain letters Program checks input for # of letters.

If input doesn't have ___ :

  • 4 letters, Program asks User for new Input.

Otherwise Program proceeds with determining Int value of each letter


Okay so heres my Program so far: ** not sure if this correct for

#include <iostream>

using namespace std;

int main() {
    string input;
    int input0 = input[0];  
    int input1 = input[1];  
    int input2 = input[2];
    int input3 = input[3];
    cout << "\nEnter a 4-letter word (Keep it clean!).\n";
  while(cin>>input){
    cout << endl << input << " has " << input.length() << " letters." << endl; 
        if (int(input[0]) > 64 || int(input[0]) < 91 || int(input[0]) > 96 || int(input[0]) < 123 || 
            int(input[1]) > 64 || int(input[1]) < 91 || int(input[1]) > 96 || int(input[1]) < 123 ||
            int(input[2]) > 64 || int(input[2]) < 91 || int(input[2]) > 96 || int(input[2]) < 123 ||
            int(input[3]) > 64 || int(input[3]) < 91 || int(input[3]) > 96 || int(input[3]) < 123) {
                if (input.length()!=5 && input.length()>3)
                    cout << "\n the int value of the " << input[0] << " is " << int(input[0]) << endl;
                    cout << "\n the int value of the " << input[1] << " is " << int(input[1]) << endl;
                    cout << "\n the int value of the " << input[2] << " is " << int(input[2]) << endl;
                    cout << "\n the int value of the " << input[3] << " is " << int(input[3]) << endl;
                else cout << input << "is not a 4-letter word.\nPlease try again." << endl;
        }
        else cout << input << " contains number(s) and or symbol(s).\nPlease try again." << endl;
  }
}

I got 2 errors:

  • error: expected '}' before 'else'
  • error: 'else' without a previous 'if'

Aucun commentaire:

Enregistrer un commentaire