# Thread: What does this loop do?

1. ## 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. what type are the variables digit, ibase, and sum?

3. 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. sum,digit,ibase,obase are all integers... sum=0 at start

5. 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. 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];
}