some c++ code

Discussion about everything. New games, 3d math, development tips...
Guest

some c++ code

Post by Guest »

Code: Select all

#include <iostream>
#include <string.h>

using namespace std;

bool isInt(const char *c)
{
    int i = -1;
    if(strlen(c) > 0)
    {
        for(i;i<(strlen(c));i++)
        {
            if(!(isdigit(c[i]))) { return false; }
        }
        return true;
    }
    return false;
}

int main(void)
{

    const char *blah = "1";
    if(isInt(blah))
    {
        cout << "Yep" << endl;
    }
    if(!isInt(blah))
    {
        cout << "Nope" << endl;
    }

    return 0;
}
does anyone know whats wrong with this code, I compiled it in gcc mingw, with no errors or warnings, and it doesn't display either yep or nope...

I'm trying to make a basic language for 2d games with allegro, then I will build a GUI for it like GameMaker, but using allegro for the gaming engine.
I will probably have prebuilt stuff in it, such as prebuilt gravity functions, and collision functions. If I can just get past this string manipulation. I'm sure this is something simple, probably so simple I'm just overlooking it. Any help would be great.

Thanks...
Guest

Post by Guest »

don't say change

int i = -1 to int i = 0, because that doesn't work either.
disanti
Posts: 367
Joined: Sat Jan 17, 2004 1:36 am
Location: California, US
Contact:

Post by disanti »

Try replacing:
if(!isInt(blah))

with:
else

and you should get a nice 'nope' string. :P
I could help more if I knew what the isdigit function did. Anyway, all the other code looks good to me.
________
PERUVIAN RECIPES
Last edited by disanti on Sun Feb 27, 2011 11:11 pm, edited 1 time in total.
Guest

Post by Guest »

nothing happened, its the same, Thats how I had it the first time. It didn't work so i changed it to the above. in short. I already tried that... didn't work...
Guest

Post by Guest »

isdigit() is an std function. it determines whether the character is an integer or not.
bitplane
Admin
Posts: 3204
Joined: Mon Mar 28, 2005 3:45 am
Location: England
Contact:

Post by bitplane »

is "for(i;" valid?
that function is less than an inch long. you can debug it with 3 printfs maximum, one in each if block and one in the for loop.
Submit bugs/patches to the tracker!
Need help right now? Visit the chat room
Guest

Post by Guest »

absolutely its valid, all it needs for that argument is an integer. As you can see I assigned int i. You don't have to declare it within the function....
bitplane
Admin
Posts: 3204
Joined: Mon Mar 28, 2005 3:45 am
Location: England
Contact:

Post by bitplane »

hmm i thought it was a line of code to execute not an integer, and to use "for(;". i could be wrong tho i guess, i keep my for loops orthadox
is it still not working?
Submit bugs/patches to the tracker!
Need help right now? Visit the chat room
etcaptor
Posts: 871
Joined: Fri Apr 09, 2004 10:32 pm
Location: Valhalla
Contact:

Re: some c++ code

Post by etcaptor »

Anonymous wrote: does anyone know whats wrong with this code, I compiled it in gcc mingw, with no errors or warnings, and it doesn't display either yep or nope...
No, displays "Yep" but in command prompt from a while, after that dissapears, because command prompt closes. You must put some key input command for pause. :wink:
ImageImage
Site development -Rock and metal online
--- etcaptor.com ------freenetlife.com
Guest

Post by Guest »

No it doesn't. I ran it through the command prompt, even with a pause it displays nothing.
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

Funny, it works fine for me, but it isn't going to be doing what you want...

Code: Select all

  for(i;i<(strlen(c));i++) 
The first issue is that you are comparing a signed value and an unsigned value. Try this code. You may be surprised.

Code: Select all

   cout << (-1 < 1u) << endl;
The signed int is promoted to unsigned so that the numbers can be compared. The value -1 for an unsigned is wrapped around to 4294967295, which is obviously larger than 1.

The second problem is that you are going to index out of bounds. You most definately need to start i at 0. If you don't the first call to isdigit will be looking at the character before the string begins, which could be anything.

Try compiliing your code with the warning level set to maximum. With gcc you want to pass the -Wall parameter on the compile line. It will catch lots of errors...

Travis
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

i thought it was a line of code to execute not an integer
The thing that is at the beginning of a for loop is called an init-expression. It can be anything that is a valid C/C++ statement.
bitplane
Admin
Posts: 3204
Joined: Mon Mar 28, 2005 3:45 am
Location: England
Contact:

Post by bitplane »

exactly, is "i;" a valid statement? looks wasteful and wrong to me anyhow
Submit bugs/patches to the tracker!
Need help right now? Visit the chat room
Guest

Post by Guest »

thats because your usually putting int i = 0, in that place. I have already declared i. THAT IS NOT THE PROBLEM. ONTO to something else. Please.
Guest

Post by Guest »

Sorry, that wasn't was I was looking for, It may work but the thing Is. That const char * c is going to be a line of code. thats why I have to loop through it.
Post Reply