Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

farmerdoug

macrumors 6502a
Original poster
Sep 16, 2008
541
0
Can I convert numbers from 1 to say 20 to strings from 001 to 020 without if statements?
 
no its not homework, and I don't want to use while statements. I want do you with some sort of string formating.
 
Hehehehe, it does sound like homework :)

There are formatting APIs that can help you, but the question is what is the intent of the exercise.
 
no its not homework, and I don't want to use while statements. I want do you with some sort of string formating.

Well, OK you should have said you don't want to use while statements. It's probably slower and will take more RAM but look at NSNumberFormatter assuming you are in Cocoa (similar exists in Java and probably other library-rich environments).
 
I don't want to fill up my code with unnecessary while, if or for statements every time I have to make a file name.

I'm in c on Xcode
 
That's still one more function in a program loaded with them. I take it the answer to my original question is either no or probably not.
 
Is this homework by any chance? If so say so up front. Other than that I'd say look at while statements...

It's worse... it's actual work. farmerdoug has posted a series of threads in the programming forum in which he manages to make the rest of us shake our heads and/or try to get across some of the principles of asking for help, most of which haven't sunk in apparently.
 
I'm not sure what is language appropriate here but guys like lee, lloyd and chown have been very helpful. You can go hide under a rock.
 
That's still one more function in a program loaded with them. I take it the answer to my original question is either no or probably not.

Why are you trying to avoid if/while statements? They are core logic constructs and any code would be expected to be full of them. They are well understood by programmers and compilers optimise them well.

I suggested the alternative for C above but I would not that this is much more expensive to call as it is general purpose and has to parse the format string. The only reason I can think of for avoiding these core constructs would be some amazingly constrained embedded system and in that case I doubt sprintf is available due to it's resource requirements.

I'm not sure what is language appropriate here but guys like lee, lloyd and chown have been very helpful. You can go hide under a rock.

Well that's lovely. You badly state your requirements, failing to list key constraints. I work with you to figure out what you really want (as opposed to what you say you want) and then post the correct solution and all you can do is insult me. Don't expect any more help until your attitude improves.
 
I'm not sure what is language appropriate here but guys like lee, lloyd and chown have been very helpful. You can go hide under a rock.

The answer to your original question is "yes." The members you identified above have indeed been extremely helpful- only after they've had to lecture you about things like posting complete code and a hands-on lesson in how to debug.

We're not going to do your work for you. If you want to know how to add leading zeros, you're welcome to post your first stab at your problem so we can give you suggestions.
 
I didn't post my any original stab because I know how to do this with for statements, etc. I'm looking for some printf format or string routine that will this semi automatically.

this
sprintf(snum,"%3d", i + 1);
strcpy(file,cube);
strcat(file,snum);

adds blanks.
 
I didn't post my any original stab because I know how to do this with for statements, etc. I'm looking for some printf format or string routine that will this semi automatically.

this
sprintf(snum,"%3d", i + 1);
strcpy(file,cube);
strcat(file,snum);

adds blanks.


I see you are choosing to not apologise for you're earlier unwarranted attack on me. Therefore I won't tell you the solution. Especially as it's in the link I posted earlier and you seem to be too lazy to read the entire document and find it.

The ability to read and understand documentation is a core programming skill. Learn it.
 
Robbie
Sorry, that reply was to miles. And I didn't look at the link because I thought that if I had to I would just as soon go with if statements.
 
with an if statement

Code:
char ** makedatasetlist( char *root, int num, int reads)
{ 
    char * cube, snum[5];
    char **file;
    int i;
    
    if ( (cube =(char *)calloc(200, sizeof(char)))== NULL)
		printf("No memory for cube\n");

    if ( (file =(char **)calloc(reads - 1, sizeof(char*)))== NULL)
		printf("No memory for filenames\n");
        for( i = 0; i < reads; i++)
            if ( (file[i] =(char *)calloc(100, sizeof(char)))== NULL)
                printf("No memory for filenames\n");

        strcpy(cube,root);
        if ( num < 10)
        strcat( cube, "00");
        else
        strcat(cube,"0");
        sprintf(snum,"%d", num);
        strcat(cube,snum);
        strcat(cube,"_f");
    
    for (i = 0; i < reads - 1; i ++)
    {   
        strcpy(file[i],cube);
        if ( i < 10)
        strcat( file[i], "00");
        else
        strcat(file[i],"0");
        
        sprintf(snum,"%d", i);
        
        strcat(file[i],snum);
        strcat(file[i],".fits");
        printf("%s\n", file[i]);
    }
    
    return file;
}
 
Robbie
Sorry, that reply was to miles. And I didn't look at the link because I thought that if I had to I would just as soon go with if statements.
There we go. The correct format string for sprintf is "%03d". I would still use a couple of if statements to protect against numbers <0 and >999 myself...
 
No, I was on a train. I would have posted that after the post above the one with lots of code. As I said it was in the link I posted to sprintf. Reading the documentation is the most powerful tool any programmer has. I honestly expected you would already have that answer by now.
 
Kinky. And I'm flattered... but I'm just not that into you.

Edit: We should adopt the SSCCE mantra in this forum. Its much simpler than going through this with every new poster.

Or we could just come to some kind of silent agreement not to help the same people(person?) with the same s*%# over and over.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.