range of characters

This is a discussion on range of characters within the C++ Programming forums, part of the General Programming Boards category; howdy, the following code will select a random char however the ASCII values 91 thru 96 are not letters. how ...

  1. #1
    In The Light
    Join Date
    Oct 2001
    Posts
    598

    range of characters

    howdy,

    the following code will select a random char however the ASCII values 91 thru 96 are not letters. how would i ignore ASCII values 91 thru 97.

    Code:
    #include <stdio.h>
    #include <iostream.h>
    #include <stdlib.h>
    #include <time.h>
    
     
    int main ()
    {
      system("clear");
      cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
      int c;
      int i;
      int count= 0;
      int under = 0;
      int over = 0;
      srand(time(0));
      
        for( c = 0; c < 20; c++){
          int i = (rand()%57)+65;
          char letter  = (i) ;//get ASCII code
          cout<<"Letter: "<<letter<<" ASCII code: "<<i<<endl;
          if(i > 90 && i < 97){
    	cout<<"  ------#"<<endl;      
    	count++;}
          if(i < 90){
    	under++;}
          if(i >97){
    	over++;}
        }
      cout<<"\nfail numbers: "<<count<<endl;
      cout<<"Under: "<<under<<endl;
      cout<<"Over: "<<over<<endl;
      return 0;
    }

    Thanks

    M.R.

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    What do you mean by ignore?

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
        for( c = 0; c < 20; c++){
          int i = (rand()%57)+65;
          if(i > 90 && i < 97){
             cout<<"  ------#"<<endl;      
             count++;
             if(i < 90){
                under++;}
             if(i >97){
                over++;}
          }
          else{
              char letter  = (i) ;//get ASCII code
              cout<<"Letter: "<<letter<<" ASCII code: "<<i<<endl;
          }
        }

  4. #4
    In The Light
    Join Date
    Oct 2001
    Posts
    598

    ignore

    howdy,

    What do you mean by ignore?
    what i want to do is exclude the values 91 - 96 from the random number selection allowing the values 65 - 90 ASCII upper case characters and values 97-122 ASCII lower case characters.

    i hope this makes my question a bit clearer.

    BTW i havent tryed seoopy's code but it looks like it may work

    M.R.

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    72
    Hi itld,
    Since the range that you wish to exclude is continious its prety easy. here is a little maths about the task
    R[65..122] -> |R| = 122-65 = 57
    but X%Y gives a number between [0..y-1] so the value is 58
    R[65..122]^[91..96] ->58-6

    Code:
      for(c = 0; c < 20000 ; c++){
        int i = (rand()%(58-6))+65;
        if (i>90)
          i+=6;
        char letter  = (i) ;//get ASCII code
        cout<<"Letter: "<<letter<<" ASCII code: "<<i<<endl;
      }
    that fragmet will give you values between 'A'..'Z' and 'a','z'

    damyan

  6. #6
    In The Light
    Join Date
    Oct 2001
    Posts
    598

    Smile twilight zone

    howdy damyan,
    when i saw you name i thought i had been mysteriously tranported over to loobians house.
    again you hit it right on the head.
    i just got the one about header files ill study it and post back latter.

    thanks
    M.R.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 11:39 PM
  2. HELP!!!!emergency Problem~expert please help
    By unknowppl in forum C++ Programming
    Replies: 9
    Last Post: 08-21-2008, 07:41 PM
  3. Replies: 10
    Last Post: 07-10-2008, 04:45 PM
  4. How do you check how many characters a user has entered?
    By engstudent363 in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 07:05 AM
  5. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 05:54 PM

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