(char)x

This is a discussion on (char)x within the C++ Programming forums, part of the General Programming Boards category; Whats wrong with this program? It compiles but it doesnt work. I liked the program to replace the first letter ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Location
    France
    Posts
    749

    (char)x

    Whats wrong with this program? It compiles but it doesnt work.
    I liked the program to replace the first letter of the string that the user type with an uppercase letter.
    How can i put the ascii value that the letter at String[0] represents into "int x;"?
    I tried every possible way that i could think of so i post here the simplest.

    Code:
    #include <iostream>
    #include <strings.h>
    #include <cstdio>
    #include <cstdlib>
    using namespace std;
    
    
    int main()
    {
     char String1[222];
     cout << "Enter string : ";
     cin  >> String1;
     char String[229];
     int   x;
     int   y;
     int   z;
     int   v;
     strncpy(String, String1, 222);
    
     if       ( String[0] >=  (char)97 || String[0] <= (char)122)
                   
              {
                String[0]  =   (char)x;
                x - 32     ==  y;
                String[0]  =   (char)y;
              }    
                  
                cout << "\n" << String << endl;
    
     system("PAUSE");
    }
    Thanks in advance

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,167
    1. you don't need two strings, you can just modify the input string.

    2. i don't get what the four integers are for

    3. you don't need all the casts in:
    Code:
     if       ( String[0] >=  (char)97 || String[0] <= (char)122)
    there is a logical error in there too, the two clauses should be connected with AND
    avoid magic numbers too while ur at it:
    Code:
    if (String[0] >= 'a' && String[0] <= 'z')
    or better still:
    Code:
    if (islower(String[0]))
    simpler still (since toupper() doesn't do anything if the character is already in uppercase):
    the whole if could be replaced by:
    Code:
    String[0] = toupper(String[0]);
    4. I'm clueless about whats happening here:
    Code:
    String[0]  =   (char)x;
                x - 32     ==  y;
                String[0]  =   (char)y;
    did you mean:
    Code:
    String[0] -= 32;
    ?

    5. why have different lengths for the two strings?
    Last edited by cyberfish; 12-12-2007 at 11:07 PM.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    This is C++, so I'd recommend using std::string instead of raw char arrays.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Location
    France
    Posts
    749
    "4. I'm clueless about whats happening here:
    Code:

    String[0] = (char)x;
    x - 32 == y;
    String[0] = (char)y;

    did you mean:
    Code:

    String[0] -= 32;"
    ****************************************
    I was trying to substract 32 from the ascii value of a lowercase letter to get the
    uppercase correspondent.

    Thanks for you both , i take good note of your responses.

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Assignment goes right-to-left: the value on the right is assigned to the variable on the left. == is a comparison operator, = is for assignment.

    If you feel you need to cast x to char each time you use it, why didn't you declare it as a char in the first place? Anyway, a char is just an int with a smaller range and instead of a numeric value, a character symbol is used to represent it on output.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Registered User
    Join Date
    Dec 2007
    Location
    France
    Posts
    749
    I didnt declared x as a char because i wanted to get the ascii value of the letter,
    and i didnt know how to do it by "String[0] = (char)x;"
    But now i know that "Assignment goes right-to-left" so i should have done it back to front.

    Cheers

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You don't need an int to get an ASCII value. A char type stores a small integer (the ASCII value) and only when it is output it will be treated in a special way (a character symbol is displayed). You can do all the math with it:

    Code:
        char ch = 'a'; //== 97
        ch -= 32; //OK
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  8. #8
    Registered User
    Join Date
    Dec 2007
    Location
    France
    Posts
    749
    Wow thanks Anon, thats some useful information , i didnt know about that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is this safe?
    By caduardo21 in forum C Programming
    Replies: 55
    Last Post: 06-30-2005, 09:01 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21