Thread: Help with a program

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    153

    Help with a program

    Hello once again,
    I'm trying to write a program that reads input until a '@' is typed in...also, I want to echo the input only without digits, and also convert any uppercase letters to lowercase and vice versa...I have this so far but I'm getting a couple of errors and I'm curious why....any help would be great. Thanks-Chap
    Code:
    #include <iostream>
    #include <cctype>
    using namespace std;
    int main()
    {
        char ch;
        cin.get(ch);
        while (ch != '@')
        {
    
            cout << ch;
            
            if (isdigit(ch))
               cout << " ";
            else if (tolower(ch))
               cout << ch;
            else (toupper(ch))
               cout << ch;
            cin.get(ch);
        }
        cout << "Done\n";
        cin.get();
        cin.get();
        return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > tolower(ch)
    You want
    if ( islower(ch) ) ch = toupper(ch);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    You are outputting the character twice inside the while loop. I don't think the first cout << ch; needs to be there.

    The number they type will show up when they type it, though, so if you want it to not show up as they type that is a much bigger problem.

  4. #4
    Registered User
    Join Date
    Jul 2004
    Posts
    98
    I try to edit it , now it's work:
    Code:
    #include <iostream>
    #include <cctype>
    using namespace std;
    
    int main()
    {
        char ch;
        
        while (true)
        {
            cout << "Please enter a letter (enter @ to quit): " << flush;
            cin >> ch;
            if ( ch == '@') break;
            if (isdigit(ch)) cout << " ";
            if (islower(ch)) cout << (char) toupper(ch) << endl;
            if (isupper(ch)) cout << (char) tolower(ch) << endl; 
        }
        cout << "Done\n";
        cin.get();
        cin.get();
        return 0;
    }
    EDIT:
    //#include <cctype> not add this head , prog also can work.
    Last edited by toysoldier; 09-22-2004 at 06:09 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM