Having trouble making program exit when two 'q's are enter consecutively

Discussion in 'OT Technology' started by gotkilled, Jan 22, 2006.

  1. gotkilled

    gotkilled New Member

    Joined:
    May 15, 2005
    Messages:
    7
    Likes Received:
    0
    I'm having trouble making my loop run continously when values other than the lower case q is entered. If a lower case q is entered, it should output "...enter q again to quit". After the initial q is entered and another value, other than q is entered, the program should loop again and also output the value of that character in ASCII. It's kinda hard to explain...but I'll try to clarify the best I can.

    The ASCII value of 'q' is 113.
    'q' is a lower-case letter with alphabet position 17.
    To quit, enter another 'q' now.
    J
    The ASCII value of 'J' is 74.
    'J' is an upper-case letter with alphabet position 10.
    q
    The ASCII value of 'q' is 113.
    'q' is a lower-case letter with alphabet position 17.
    To quit, enter another 'q' now.
    q

    Code:
                                                                                                                                                                                                                                                                   
    #include <iostream>
    using namespace std;
    int main ()
    {
        char value;
        bool done=0;
        bool qvalue=0;
        cout<<"Enter characters one at a time.\nTo quit the program, enter 'q' twice in a row.\n";
        cout<<"Please enter a value: ";
        do{
        
        cin>>value;
        int position=static_cast<int>(value);//ASCII value is declared as int position
        cout<<"The ASCII value of '"<<value<<"' is "<<static_cast<int>(value)<<".\n";//value inputted is stored in value and is converted to ASCII through static_cast<int>(value)
    
        
        if (value>='A' && value<='Z')//if both are true, then it will be upper-case
        cout<<"'"<<value<<"' is an upper-case letter with alphabet position "<<position-64<<".\n";
    
        else if (value>='0' && value<='9')//if both are true, then it will be a numerical value
        cout<<"'"<<value<<"' is a digit.\n";    
    
        else if (value>='a' && value<='z') //if one is true, then it will be lower-case
        {cout<<"'"<<value<<"' is a lower-case letter with alphabet position "<<position-96<<".\n";
            if (value=='q')
                cout<<"To quit, enter another 'q' now.\nPlease enter a value: ";
                cin>>value;
                    if (value='q')
                        done=1;
        }
    
            
        else//if none of the above are true, then the value is automatically a special character
        cout<<"'"<<value<<"'is a special character.\n";
        }while (done!=1);
    
    
        return 0;    
        
        
    
    }
    
    
     
  2. peerk

    peerk New Member

    Joined:
    Mar 14, 2005
    Messages:
    984
    Likes Received:
    0
    if (value='q')

    should be

    if (value=='q')
     
  3. samm

    samm Next in Line

    Joined:
    Dec 22, 2000
    Messages:
    2,630
    Likes Received:
    0
    Location:
    San Jose, CA
    You should put curly braces around your conditionals, this works for me:

    Code:
    #include <iostream>
    using namespace std;
    
    int main () {
        char value;
        bool done=0;
        bool qvalue=0;
        cout << "Enter characters one at a time.\nTo quit the program, enter 'q' twice in a row.\n";
        cout << "Please enter a value: ";
        
        do {
            cin >> value;
            
            //ASCII value is declared as int position
            int position=static_cast<int>(value);
            
            //value inputted is stored in value and is converted to ASCII through static_cast<int>(value)
            cout << "The ASCII value of '" << value << "' is " << static_cast<int>(value) << ".\n";
        
            //if both are true, then it will be upper-case
            if (value>='A' && value<='Z') {
                cout << "'" << value << "' is an upper-case letter with alphabet position " << position-64 << ".\n";
            } else if (value>='0' && value<='9') {
                //if both are true, then it will be a numerical value
                cout << "'" << value << "' is a digit.\n";
            } else if (value>='a' && value<='z') {
                //if one is true, then it will be lower-case
                cout << "'" << value << "' is a lower-case letter with alphabet position " << position-96 <<".\n";
                if (value=='q') {
                    cout << "To quit, enter another 'q' now.\nPlease enter a value: ";
                    cin>>value;
                    if (value=='q') {
                        done=1;
                    }
                }
            } else {
                //if none of the above are true, then the value is automatically a special character
                cout << "'" << value << "'is a special character.\n";
            }
        } while (done!=1);
    
    
        return 0;
    }
    
     
  4. deusexaethera

    deusexaethera OT Supporter

    Joined:
    Jan 27, 2005
    Messages:
    19,712
    Likes Received:
    0
    Ah, the cursed double-equal-sign error! I remember it well from my school days. You know how I finally got good at seeing when I made that mistake?

    ...I debugged my own code instead of cheating by posting my assignments on OffTopic.

    EDIT: If you're teaching yourself C++, then I apologize for my rant. Otherwise, learn how to debug your own code.
     

Share This Page