Why doesn't this print?

This is a discussion on Why doesn't this print? within the C Programming forums, part of the General Programming Boards category; I'm new to c and this program is meant to read from a txt file and extract the time from ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    1

    Cool Why doesn't this print?

    I'm new to c and this program is meant to read from a txt file and extract the time from it and print it in a large format made of hashtags, this is the following code, it compiles but doesn't print and i wasn't sure why
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <time.h>
    #include <string.h>
    #include <stdlib.h>
    #include <windows.h>
    
    /* simple function to pause */
    /* adapted from cplusplus.com */
    int wait (float seconds) {
      clock_t endwait;
      endwait = clock () + seconds * CLOCKS_PER_SEC ;
      while (clock() < endwait) {};
      return(0);
    }
    
    void PrintNumber(char n, int r)
    
    {
    switch(atoi(&n)) {
    
    /* For 0 */
    
    case '0':
    switch(r){
    case 0:
    case 6:
    printf("#####");
    break;
    case 1:
    case 2:
    case 3:
    case 4:
    case 5:
    printf("#   #");
    break;}
    
    /* For 1 */
    
    
    case '1':
    switch(r){
    case 0:
    case 1:
    case 2:
    case 3:
    case 4:
    case 5:
    case 6:
    printf("  #  ");
    break;}
    
    /* For 2 */
    
    
    case '2':
    switch(r){
    case 0:
    case 3:
    case 6:
    printf("#####");
    break;
    case 1:
    case 2:
    printf("    #");
    break;
    case 4:
    case 5:
    printf("#    ");
    break;}
    
    /* For 3 */
    
    
    case '3':
    switch(r){
    case 0:
    case 3:
    case 6:
    printf("#####");
    case 1:
    case 2:
    case 4:
    case 5:
    printf("    #");
    break;}
    
    /* For 4 */
    
    
    case '4':
    switch(r){
    case 0:
    case 1:
    case 2:
    printf("#   #");
    break;
    case 3:
    printf("#####");
    break;
    case 4:
    case 5:
    case 6:
    printf("    #");
    break;}
    
    /* For 5 */
    
    
    case '5':
    switch(r){
    case 0:
    case 3:
    case 6:
    printf("#####");
    break;
    case 1:
    case 2:
    printf("#    ");
    break;
    case 4:
    case 5:
    printf("    #");
    break;}
    
    /* For 6 */
    
    
    case '6':
    switch(r){
    case 0:
    case 3:
    case 6:
    printf("#####");
    break;
    case 1:
    case 2:
    printf("#    ");
    break;
    case 4:
    case 5:
    printf("#   #");
    break;}
    
    /* For 7 */
    
    
    case '7':
    switch(r){
    case 0:
    printf("#####");
    case 1:
    case 2:
    case 3:
    case 4:
    case 5:
    case 6:
    printf("    #");
    break;}
    
    /* For 8 */
    
    
    case '8':
    switch(r){
    case 0:
    case 3:
    case 6:
    printf("#####");
    break;
    case 1:
    case 2:
    case 4:
    case 5:
    printf("#   #");
    break;}
    
    /* For 9 */
    
    
    case '9':
    switch(r){
    case 0:
    case 3:
    case 6:
    printf("#####");
    break;
    case 1:
    case 2:
    printf("#   #");
    break;
    case 4:
    case 5:
    printf("    #");
    break;}
    }
    }
    
    
    int main() {
    
      FILE *gpsHandle;
      char gpsString[105];
      char str[7] = "$GPGGA";
      char number[3];
      int a,i,j;
      int b=1;
        
      /* initialise connection to GPS receiver */
      gpsHandle = fopen("gpsdata.txt","r");
    
      /* abort here if the file didn't open */
      if (gpsHandle==NULL) {
        printf("Couldn't open GPS data file\n");
        return(0);
                            }
    
      /* loop trying to get data */
      while (!feof(gpsHandle)) {
        
        /* read data line */
        fgets(gpsString,100,gpsHandle);
    
            /* Tests first 6 characters in Data file individually to see if they are $GPGGA */
            for(a=0;a<6;++a)
        {
            if(str[a] != gpsString[a])
            {
                /* If the first six characters are not $GPGGA sets b=0 and the next line is tested  */
                b=0;
            }
        }
        /* If the first six characters are $GPGGA sets b=1  */
        if(b==1)
    
            /* Performs following operation while b=1 */
            while(1)
            {
                if(gpsString[a]==',')
                {
                    char time[5]="0000";
                    
                    for(i=0;i<5;++i)
                        time[i]=gpsString[a+i];
    
                    for(j=0;j<7;++j)
                        for(i=a;i<a+4;++i)
                            PrintNumber(time[i],j);
                            
                            break;
                            
                            b=1;
            
                        
                
                }
    
            }
            
    
    
                                }
                                
    
      return(0);
    
                }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    First lesson, learn how to indent code.
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <time.h>
    #include <string.h>
    #include <stdlib.h>
    #include <windows.h>
    
    /* simple function to pause */
    /* adapted from cplusplus.com */
    int wait(float seconds)
    {
      clock_t endwait;
      endwait = clock() + seconds * CLOCKS_PER_SEC;
      while (clock() < endwait) {
      };
      return (0);
    }
    
    void PrintNumber(char n, int r)
    {
      switch (atoi(&n)) {
    /* For 0 */
      case '0':
        switch (r) {
        case 0:
        case 6:
          printf("#####");
          break;
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
          printf("#   #");
          break;
        }
    
    /* For 1 */
      case '1':
        switch (r) {
        case 0:
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
        case 6:
          printf("  #  ");
          break;
        }
    
    /* For 2 */
      case '2':
        switch (r) {
        case 0:
        case 3:
        case 6:
          printf("#####");
          break;
        case 1:
        case 2:
          printf("    #");
          break;
        case 4:
        case 5:
          printf("#    ");
          break;
        }
    
    /* For 3 */
      case '3':
        switch (r) {
        case 0:
        case 3:
        case 6:
          printf("#####");
        case 1:
        case 2:
        case 4:
        case 5:
          printf("    #");
          break;
        }
    
    /* For 4 */
      case '4':
        switch (r) {
        case 0:
        case 1:
        case 2:
          printf("#   #");
          break;
        case 3:
          printf("#####");
          break;
        case 4:
        case 5:
        case 6:
          printf("    #");
          break;
        }
    
    /* For 5 */
      case '5':
        switch (r) {
        case 0:
        case 3:
        case 6:
          printf("#####");
          break;
        case 1:
        case 2:
          printf("#    ");
          break;
        case 4:
        case 5:
          printf("    #");
          break;
        }
    
    /* For 6 */
      case '6':
        switch (r) {
        case 0:
        case 3:
        case 6:
          printf("#####");
          break;
        case 1:
        case 2:
          printf("#    ");
          break;
        case 4:
        case 5:
          printf("#   #");
          break;
        }
    
    /* For 7 */
      case '7':
        switch (r) {
        case 0:
          printf("#####");
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
        case 6:
          printf("    #");
          break;
        }
    
    /* For 8 */
      case '8':
        switch (r) {
        case 0:
        case 3:
        case 6:
          printf("#####");
          break;
        case 1:
        case 2:
        case 4:
        case 5:
          printf("#   #");
          break;
        }
    
    /* For 9 */
      case '9':
        switch (r) {
        case 0:
        case 3:
        case 6:
          printf("#####");
          break;
        case 1:
        case 2:
          printf("#   #");
          break;
        case 4:
        case 5:
          printf("    #");
          break;
        }
      }
    }
    
    
    int main()
    {
    
      FILE *gpsHandle;
      char gpsString[105];
      char str[7] = "$GPGGA";
      char number[3];
      int a, i, j;
      int b = 1;
    
      /* initialise connection to GPS receiver */
      gpsHandle = fopen("gpsdata.txt", "r");
    
      /* abort here if the file didn't open */
      if (gpsHandle == NULL) {
        printf("Couldn't open GPS data file\n");
        return (0);
      }
    
      /* loop trying to get data */
      while (!feof(gpsHandle)) {
    
        /* read data line */
        fgets(gpsString, 100, gpsHandle);
    
        /* Tests first 6 characters in Data file individually to see if they are $GPGGA */
        for (a = 0; a < 6; ++a) {
          if (str[a] != gpsString[a]) {
            /* If the first six characters are not $GPGGA sets b=0 and the next line is tested  */
            b = 0;
          }
        }
    
        /* If the first six characters are $GPGGA sets b=1  */
        if (b == 1)
          /* Performs following operation while b=1 */
          while (1) {
            if (gpsString[a] == ',') {
              char time[5] = "0000";
              for (i = 0; i < 5; ++i)
                time[i] = gpsString[a + i];
    
              for (j = 0; j < 7; ++j)
                for (i = a; i < a + 4; ++i)
                  PrintNumber(time[i], j);
    
              break;
              b = 1;
            }
          }
      }
    
      return (0);
    }
    Second lesson, read the manual page before using functions.
    switch(atoi(&n))
    Just because the compiler is "happy" that you managed to pass a parameter with the correct type to atoi(), it doesn't follow that atoi() itself is going to be happy at all when it fails to find an expected \0 to mark the end of the string.

    You should have had
    Code:
    switch (atoi(&n))
    ....
        default:
          fprintf(stderr,"Huh? What!\n");
          break;
    Then you would have seen output caused by failure, as opposed to nothing at all.


    If you're trying to do a char to int conversion on a single char, then
    n - '0'
    is all you need.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Tree doesn't print it's elements
    By cr33p in forum C Programming
    Replies: 2
    Last Post: 08-17-2011, 10:27 AM
  2. %g doesn't print as many decimal places as %f
    By octoc in forum C Programming
    Replies: 1
    Last Post: 03-31-2008, 12:16 PM
  3. [Not Urgent at all] - Print from c++ Console doesn't work.
    By zerodevice in forum C++ Programming
    Replies: 17
    Last Post: 08-03-2006, 04:09 PM
  4. this code doesn't print any thing with gcc ??
    By agarwaga in forum C Programming
    Replies: 1
    Last Post: 10-17-2005, 11:06 AM
  5. Doesn't print out?
    By cockroach007 in forum C Programming
    Replies: 8
    Last Post: 11-17-2001, 07:30 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21