C++ Help Needed ASAP!!!!

Discussion in 'Computer Science & Culture' started by BiteMe, Feb 11, 2003.

Thread Status:
Not open for further replies.
  1. BiteMe Registered Member

    Messages:
    5
    I know this is relatively simple, I just can't get it for some reason. I had to input a char var (letter) and then write the line to spit out the ASCII code...and then show the opposite case of the letter (for example, if the input letter was 'g', 'G' needs to be displayed next...) but i can't get it to show a lower or upper case letter -- the only thing that will come up is the code for the letter in the opposite case. I need to use an if-then statement to do this, please help...I need this done ASAP! Thank you!
     
  2. Google AdSense Guest Advertisement



    to hide all adverts.
  3. testify Look, a puppy! Registered Senior Member

    Messages:
    508
    Do you need to use the ASCII codes? Why don't you test if the char is upper or lower if the char is lower case use the "toupper()" function on it. If the char is upper case the the "tolower()" function. The header file used for these two functions is ctype.h. If you aren't familiar with it basically all it does is converts the character sent to the function back to and upper case or lower case, depending on what you want to use.
     
  4. Google AdSense Guest Advertisement



    to hide all adverts.
  5. pragmathen 0001 1111 Registered Senior Member

    Messages:
    452
    How about this?

    #include <ctype.h>
    #include <string.h>
    #include <stdlib.h>

    <b>char</b> temp[20];
    <b>int</b> length, i;
    cout << "Enter a letter: " << endl;
    cin >> temp;
    cout << "You entered [" << temp << "]" << endl;
    length = strlen(temp);
    <b>for</b> (i=0; i&lt;length; i++)
    {
    &nbsp;&nbsp;&nbsp;temp = toupper(temp);
    }
    cout << "Reverse is [" << temp << "]" << endl;
    getch();
     
  6. Google AdSense Guest Advertisement



    to hide all adverts.
  7. testify Look, a puppy! Registered Senior Member

    Messages:
    508
    Yeah sort of what I was thinking, but your code only change every character to upper case. If you wanted to switch each lower to upper and each upper to lower you would have to put an if statement within the for loop. Like so:

    #include <ctype.h>
    #include <iostream.h>

    void main(){
    char temp;
    cout << "Enter a letter: " << endl;
    cin >> temp;
    if(temp isupper){
    temp = tolower(temp);
    }
    if(temp islower){
    temp = toupper(temp);
    }
    cout << "Reverse is [" << temp << "]" << endl;
    }

    If you need to do it the ASCII way...just tell I am sure I can figure that out.

    Whoops! forgot to change something in mine

    Please Register or Log in to view the hidden image!

     
    Last edited: Feb 12, 2003
  8. BiteMe Registered Member

    Messages:
    5
    Yeah but...

    I have to use an if-then statement with NO loops...here's my program so far. Oh...and thanks for trying to help, I really appreciate it

    Please Register or Log in to view the hidden image!



    #include <iostream>
    using namespace std;
    void main ()

    {
    char var;
    cout <<"Enter a character: ";
    cin >> var;

    cout <<"\n";
    cout << "The code for " << var << "is: " << int(var);
    cout <<"\n";

    cout << "The upper-case form of " << var << "is: " << toupper(var) <<endl;
    cout << "\n";
    }
     
  9. testify Look, a puppy! Registered Senior Member

    Messages:
    508
    And a assume you'll just use an if statement to check if it's a lower or upper case and change it appropriately. I thought that you had to convert the char to it's ASCII value, then add/subtract a certain amount to it to get the reverse upper case/lower case ASCII value of that char.

    ie. lets say 'g' had the ASCII value of 30 and 'G' had an ASCII value of 99. You would have to convert 'g' to 30, then add the set value of 69 to the 30 to get 99, then convert that 99 value back to a char. I am not sure if that's possible. I am not too familiar with the ASCII values and what order they come in.
     
  10. pragmathen 0001 1111 Registered Senior Member

    Messages:
    452
    #include &lt;ctype.h&gt;
    #include &lt;string.h&gt;
    #include &lt;stdlib.h&gt;

    <b>void</b> main(&nbsp; )
    {
    &nbsp;&nbsp;&nbsp;<b>char</b> var[20];
    &nbsp;&nbsp;&nbsp;cout << "Enter a letter: " << endl;
    &nbsp;&nbsp;&nbsp;cin >> var;
    &nbsp;&nbsp;&nbsp;cout << "You entered [" << var << "]" << endl;
    &nbsp;&nbsp;&nbsp;<b>if</b> (islower(var[0]))
    &nbsp;&nbsp;&nbsp;{
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var[0] = toupper(var[0]);
    &nbsp;&nbsp;&nbsp;}
    &nbsp;&nbsp;&nbsp;<b>else if</b> (isupper(var[0]))
    &nbsp;&nbsp;&nbsp;{
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var[0] = tolower(var[0]);
    &nbsp;&nbsp;&nbsp;}
    &nbsp;&nbsp;&nbsp;cout << "Reverse is&nbsp;&nbsp[" << var << "]" << endl;
    &nbsp;&nbsp;&nbsp;getch();
    }

    Here is what will be output:
    Enter a letter:
    f
    You entered [f]
    Reverse is&nbsp;&nbsp;&nbsp;&nbsp;[F]

    Thanks <b>TESTIFY</b> for pointing me in the right direction and thanks <b>BiteMe</b> for such a good question!

    prag
     
  11. testify Look, a puppy! Registered Senior Member

    Messages:
    508
    I don't know why you would make the char a 20 character string? I mean if you're only using 1 character in the array why have 20 blocks set aside? Basically what I am saying is replace the var[20] with var and every var[0] with var. Then you don't have to include the string.h header either. You don't have to, it just makes the code so much cleaner.
     
  12. pragmathen 0001 1111 Registered Senior Member

    Messages:
    452
    D'oh!

    <b>void</b> main(&nbsp; )
    {
    &nbsp;&nbsp;&nbsp;<b>char</b> var;
    &nbsp;&nbsp;&nbsp;cout << "Enter a letter: " << endl;
    &nbsp;&nbsp;&nbsp;cin >> var;
    &nbsp;&nbsp;&nbsp;cout << "You entered [" << var << "]" << endl;
    &nbsp;&nbsp;&nbsp;<b>if</b> (islower(var))
    &nbsp;&nbsp;&nbsp;{
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var = toupper(var);
    &nbsp;&nbsp;&nbsp;}
    &nbsp;&nbsp;&nbsp;<b>else if</b> (isupper(var))
    &nbsp;&nbsp;&nbsp;{
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var = tolower(var);
    &nbsp;&nbsp;&nbsp;}
    &nbsp;&nbsp;&nbsp;cout << "Reverse is&nbsp;&nbsp;&nbsp;[" << var << "]" << endl;
    &nbsp;&nbsp;&nbsp;getch();
    }

    <b>TESTIFY</b>, you're right. I don't know what I was thinking with that.

    Thanks!

    prag
     
  13. ConsequentAtheist Registered Senior Member

    Messages:
    1,579
    Re: D'oh!

    It's good form to recognize and handle the case where the character entered is not alphabetic.

    Please Register or Log in to view the hidden image!

     
  14. testify Look, a puppy! Registered Senior Member

    Messages:
    508
    If the char isn't alphabetic like a '%' what would it return? Or is '%' the uppercase of '5'?
     
  15. BiteMe Registered Member

    Messages:
    5
    Thank You

    Please Register or Log in to view the hidden image!

    Just wanted to say THANK YOU for the help, I figured it out (with all of your help), and I'm sure it won't be long until I'm back with yet another problem. Thanks again.
     
Thread Status:
Not open for further replies.

Share This Page