What does this loop do?

This is a discussion on What does this loop do? within the C++ Programming forums, part of the General Programming Boards category; Can someone tell me what the following loop would do? S is an array and this program uses ctype.h.. while ...

  1. #1
    Unregistered
    Guest

    Question What does this loop do?

    Can someone tell me what the following loop would do? S is an array and this program uses ctype.h..

    while (s[i])
    {
    if (isalpha(s[i]))
    digit=toupper (s[i])-'A'+10;
    else if (isdigit(s[i]))
    digit=s[i]-'0';
    else cout << "You have entered an illegal character." << endl;
    sum = sum * ibase + digit;
    i++;
    }

  2. #2
    Unregistered
    Guest
    what type are the variables digit, ibase, and sum?

  3. #3
    Unregistered
    Guest
    if this:

    digit=toupper (s[i])-'A'+10;

    were this:

    ibase = toupper (s[i])-'A'+10;

    then the loop might "change" a string consisting of alphanumeric values only to a lump sum that could then be used as a data check when sending data from one computer to another to assure that transmission occurred without error.

  4. #4
    Unregistered
    Guest
    sum,digit,ibase,obase are all integers... sum=0 at start

  5. #5
    Barjor
    Guest
    Looks like some sort of encrypting function. Adds 10 to asci value on letters, don't modify numbers and the "key" would be ibase
    ~Barjor

  6. #6
    Unregistered
    Guest
    This loop is actually part of a base conversion program and I can't understand how it converts a number in one base to another:

    #include <iostream.h>
    #include <ctype.h> //Using for classifying and mapping codes from character target set.
    void conversion(int,int);

    void main ()
    {
    int i; char s[80];
    int sum=0,digit,ibase,obase;
    cout << "Enter the base of the number you are entering: " << endl;
    cin >> ibase;
    cout << "Enter the base you want your input converted to: " << endl;
    cin >> obase;
    cout << "Enter the number you would like to convert: " << endl;
    cin >> s;
    i=0;

    while (s[i])
    {
    if (isalpha(s[i]))//check if letter is entered
    digit=toupper (s[i])-'A'+10;//convert to upper case character
    else if (isdigit(s[i]))
    digit=s[i]-'0';
    else cout << "You have entered an illegal character." << endl;
    sum = sum * ibase + digit;
    i++;
    }
    conversion(sum,obase);
    cout << " Is the number in the new base" << endl;
    int wait;
    cin >> wait;
    return;
    }

    void conversion (int x, int base)
    {
    char digit []="0123456789ABCDEF";
    int a,b;
    a=x/base;
    b=x%base;
    if (a) conversion (a,base);
    cout << digit[b];
    }

  7. #7
    Unregistered
    Guest
    amazing what a little more information can do.

    Let's say you are in base 16 and the user enters A1B. A in base 16 generally represents the int 10, with 0-9 being their usual counterpart and B represents the int 11. To the compiler a letter is really a number. Often the conversion is done using an agreed upon table like ASCII or unicode or whatever. To turn A into 10 you would make sure the letter is in caps, subtract the ASCII value for A from A and then add 10. Let's say the ASCII value for A is 65. Then it would be A = 65 - 65 + 10; or just A = 10. Likewise B would be B = 66 - 65 + 10 = 11. For digits you just take the ASCII value for that digit and substract the ASCII value for zeor from it to the the integer equivalent for the digit. And so on.

    If digit is defined as an int the the compiler will assign A and whatever s[i] is the integer equivalent and provide the appropriate value for that element of s[]. This can then be used to calculate the value of the of the placeholder. In the above example the placeholders would translate to base 10 as follows 11(16^0) + 1(16^1) + 10(16^2). This step is represented (at least in theory) by the sum = sum * ibase + digit line, but I don't think that is correct. I think it should be something like sum += digit(pow(ibase, i)) if the original string is reversed.

    Then once you have the number converted to base 10 you can use the conversrion() function to convert to the new base, whatever it may be.

  8. #8
    Unregistered
    Guest

    Thumbs up

    Thanks now i understand. Wouldn't their be an easier way to rewrite this program using enumeration type?

  9. #9
    Unregistered
    Guest
    Sure there are other ways, and yes, I am sure some of them will use enumerated types. Are they easier? Only you can decide that. What would be easier is if the author of the code you posted used comments, but that is asking too much when you think only you are going to use the code and you already "know" what you are doing.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  3. loop in a linked linked lists
    By kris.c in forum C Programming
    Replies: 6
    Last Post: 08-26-2006, 12:38 PM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-19-2005, 11:30 PM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 08:14 AM

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