Thread: why no print___

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    168

    why no print___

    train.h
    Code:
    //PARSEPARA:parse para by step by step
    int parseP(const char *a,float *p)
    {
         float start,end,step;
         int pCount = 0;
         int pNum = 0;
         pNum = sscanf(a,"%f-%f-%f",&start,&end,&step);
         //printf("start:[%f]\n",start);
         
         if ( pNum < 3 ) 
         {
              printf("Error:format of windowSize,K-value,noequalPunish and percentRatio must be start-end-step.\n");
              exit(0);
         }
         if ( end < start )
         {
              printf("Error: end must be more than start in [%s]\n",a);
              exit(0);
         } else if ( step <= 0 )
         {
              printf("Error: step must be more than 0 in [%s]\n",a);
              exit(0);
         }
         
         //float j;
         while ( start <= end )
         {
               *p = start;
               start = start+step;
               p++;
               pCount++;
         }
         
         return pCount;
         //printf("pNum:%f\n",step);
    }
    zero.cpp
    Code:
    #include "train.h"
    int main( int argc,char *argv[])
    {
        float a[30] = {'\0'};
        parseP("0-5-1",a);
        
        for ( int i = 0; a[i] != '\0' ; i++ )
        {
            printf("a[%d]:%f\n",i,a[i]);
        }
        return 0;
    }



    If parseP("0-5-1",a); print is nothing
    if parseP("1-5-1",a); print is as follows:
    a[0]:1.000000
    a[1]:2.000000
    a[2]:3.000000
    a[3]:4.000000
    a[4]:5.000000


    why ? how to solve this problem?

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your test condition prevents it from printing:

    Code:
    for(... a[i] != '\0' ; ...)
    a[] is an array of floats, but '\0' is a char, and that char evaluates to 0. so when the first value in a[0] is zero, it stops looping right away.

    Are you trying to print out a[] or the contents of parseP[]?
    Last edited by Adak; 12-28-2009 at 12:36 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Towr of Hanoi move the disc
    By WatchTower in forum C Programming
    Replies: 9
    Last Post: 07-17-2009, 03:48 AM
  2. merging linked lists
    By scwizzo in forum C++ Programming
    Replies: 15
    Last Post: 09-14-2008, 05:07 PM
  3. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  4. Scope And Parameter Passing
    By djwicks in forum C Programming
    Replies: 6
    Last Post: 03-28-2005, 08:26 PM
  5. What kind of programs should I start writing?
    By Macabre in forum C++ Programming
    Replies: 23
    Last Post: 04-12-2003, 08:13 PM