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

**URL:** <https://boards.straightdope.com/t/any-c-programmers-out-there-i-need-a-little-help/209420>\
**Category:** Factual Questions\
**Created:** [October 24, 2003, 11:51pm UTC](https://boards.straightdope.com/t/any-c-programmers-out-there-i-need-a-little-help/209420 "2003-10-24T23:51:01Z")\
**Posts on this page:** 8\
**Page:** 2

<div class="post-metadata">

**Author:** ![LtningBug](https://avatars.discourse-cdn.com/v4/letter/l/73ab20/32.png) [@LtningBug](https://boards.straightdope.com/u/LtningBug)\
**Post date:** [October 25, 2003, 3:58pm UTC](https://boards.straightdope.com/t/any-c-programmers-out-there-i-need-a-little-help/209420/21 "2003-10-25T15:58:30Z")

</div>

**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:

```auto

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.

---

<div class="post-metadata">

**Author:** ![Computer\_Guru](https://avatars.discourse-cdn.com/v4/letter/c/ed655f/32.png) [@Computer\_Guru](https://boards.straightdope.com/u/Computer_Guru)\
**Post date:** [October 25, 2003, 4:39pm UTC](https://boards.straightdope.com/t/any-c-programmers-out-there-i-need-a-little-help/209420/22 "2003-10-25T16:39:03Z")

</div>

> [@](#):
>
> \*Originally posted by LtningBug \*  
> \*\ ***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:
> 
> ```auto
> 
> 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.

---

<div class="post-metadata">

**Author:** ![Computer\_Guru](https://avatars.discourse-cdn.com/v4/letter/c/ed655f/32.png) [@Computer\_Guru](https://boards.straightdope.com/u/Computer_Guru)\
**Post date:** [October 25, 2003, 5:18pm UTC](https://boards.straightdope.com/t/any-c-programmers-out-there-i-need-a-little-help/209420/23 "2003-10-25T17:18:07Z")

</div>

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]

---

<div class="post-metadata">

**Author:** ![Rysto](https://avatars.discourse-cdn.com/v4/letter/r/ecccb3/32.png) [@Rysto](https://boards.straightdope.com/u/Rysto)\
**Post date:** [October 25, 2003, 6:20pm UTC](https://boards.straightdope.com/t/any-c-programmers-out-there-i-need-a-little-help/209420/24 "2003-10-25T18:20:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Computer\_Guru](https://avatars.discourse-cdn.com/v4/letter/c/ed655f/32.png) [@Computer\_Guru](https://boards.straightdope.com/u/Computer_Guru)\
**Post date:** [October 25, 2003, 7:25pm UTC](https://boards.straightdope.com/t/any-c-programmers-out-there-i-need-a-little-help/209420/25 "2003-10-25T19:25:07Z")

</div>

> [@](#):
>
> \*Originally posted by Rysto \*  
> \*\*+ and - have the same precedence in C++(and in math). So the answer is 35. \*\*

Thank you very much.

---

<div class="post-metadata">

**Author:** ![Mort\_Furd](https://avatars.discourse-cdn.com/v4/letter/m/8e8cbc/32.png) [@Mort\_Furd](https://boards.straightdope.com/u/Mort_Furd)\
**Post date:** [October 25, 2003, 7:30pm UTC](https://boards.straightdope.com/t/any-c-programmers-out-there-i-need-a-little-help/209420/26 "2003-10-25T19:30:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![HPL](https://avatars.discourse-cdn.com/v4/letter/h/3da27b/32.png) [@HPL](https://boards.straightdope.com/u/HPL)\
**Post date:** [October 28, 2003, 5:43am UTC](https://boards.straightdope.com/t/any-c-programmers-out-there-i-need-a-little-help/209420/27 "2003-10-28T05:43:19Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Xerxes](https://avatars.discourse-cdn.com/v4/letter/x/d2c977/32.png) [@Xerxes](https://boards.straightdope.com/u/Xerxes)\
**Post date:** [October 29, 2003, 12:10pm UTC](https://boards.straightdope.com/t/any-c-programmers-out-there-i-need-a-little-help/209420/28 "2003-10-29T12:10:27Z")

</div>

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.

[Previous page](https://boards.straightdope.com/t/any-c-programmers-out-there-i-need-a-little-help/209420.md?page=1)
