Any C++ programmers out there? I need a little help.

Computer Guru, isalpha will return true (well, nonzero, at least) if the character is a letter, and 0 if the character is not a letter.

However, isalpha only works on character values. The value stored in loan is a float value. The cin object is doing the work of converting a bunch of characters that the user entered (such as ‘-’, ‘4’, ‘8’, ‘0’, ‘.’, and ‘8’) to a float number (in this case, -480.8).

In order for isalpha to be relevent, you would need to write your own number parser:



float ftNumber = 0.0;
// Was there a '-' at the beginning?
bool fIsNegative = false;
// Are we done parsing the number?
bool fDone = false;
// There must be at least one digit.
int iDigits = 0;
int iDivision = 0;
char szInput[256];
char * pchParsing = szInput;
cout << "Enter a number: ";
// Parsing the number one char at a time with .get() would
// be fun, but wouldn't allow the user to backspace and
// such.
cin.getline(szInput, sizeof(szInput));

// I'm not sure getline null terminates the string if the 
// number of characters read is equal to the size of the buffer
// (a lot of string functions don't)
szInput[sizeof(szInput)-1] = 0;

// Make sure we're only dealing with uppercase letters
_strupr(szInput);

// Move past leading whitespace
while(*pchParse && isspace(*pchParse))
    ++pchParse;

if ('-' == *pchParse)
{
    fIsNegative = true;
    ++pchParse;
}

do
{
    if (isdigit(*pchParse))
    {
        ftNumber *= 10;
        ftNumber += *pchParse - '0';
        ++iDigits;
        if (iDivision)
            ++iDivision;
    }
    else if ('.' == pchParse && 0 == iDivision)
    {
        iDivision = 1;
    }
    else if ('E' == pchParse)
    {
        // I leave this as an excercise for the reader
    }
    else
        fDone = true;
} while (!fDone && *(++pchParse));

--iDivision;
ftNumber *= pow(10, -iDivision);

if (fIsNegative)
    ftNumber *= -1.0;


Well, you get the idea. Suffice it to say, you probably don’t want to parse the float yourself.

So, instead of using isalpha, you would want to use the isdigit() one. I’m sorry, but I hardly use Ctype. I believe I’ve used it before, but I’m still new to it. They don’t teach you everything at school.

I just have a quick question. I always get mixed up in order of precedence stuff. I believe stuff that you add before you subtract, but then I remember in discrete math and some of my computer classes, they said whatever comes first (I think).

LIke, lets’ say 10-5+30. Now, is that -25, or 35. I don’t know why I get confused like this.

[/hijack]

  • and - have the same precedence in C++(and in math). So the answer is 35.

Thank you very much.

Jimminy, folks, what’s wrong with taking a string as input and then using atoi or atof on it?

Thanks for all your help.

Now I have another problem.

I’ve created a program and it nearly works, except that when I input the data, nothing happens. No crashing, no infinite loops, no coninuation, nothing. I have to control-C out.

The Relevent code:

(Two while loops above that check if loan and interest numbers are positive and re-prompts if they aren’t)

monpay = loan * .05;

//Loop calculates loan payments and how long it will take to pay
//off loan
do
{
//Caculate applied interest
apint = loan * intrate /12;
//Calculate modified Monthly Payment
mdmonpay = monpay - apint;
//Calculate loan payment
loan = loan - mdmonpay;
//Increment the month counter for each
//time through the loop

            ++month;
    }
    //Continue to do so until loan is payed off
    while(loan &gt;= 0);

    //Print the resulting time
    cout&lt;&lt;endl&lt;&lt; "The amount of time till the loan
     is repayed is "&lt;&lt;month&lt;&lt;" months. ", endl;

Again, any help would be apperciated.

I’m trying not to state the obvious, but why not output the value of ‘loan’ every time you get to the bottom of the loop; it’s clearly not doing what you expect - seeing what the value is on each iteration could help you pinpoint the problem.