Thread: compiler warnings

  1. #1
    Unregistered
    Guest

    compiler warnings

    Hey guys - im now on day 4 of programming in C - and I am having some difficulties with this one program. It takes numbers as input and converts them into their co-inciding words.

    I am reading the numbers in using fgets and reading them in as characters because the book doesn't want to teach me scanf - says its too unreliable - so we use sscanf. Anyways - the warnings are below the code.
    **************************************

    while (1) {
    char current_letter;
    if (input_number[counter] == NULL) {
    break;
    }
    current_number = 0;
    current_letter = input_number[counter];
    sscanf(current_letter, "%d", &current_number);


    *********************************
    gcc -o numlet numlet.c
    numlet.c: In function `main':
    numlet.c:31: warning: comparison between pointer and integer
    numlet.c:36: warning: passing arg 1 of `sscanf' makes pointer from integer without a cast

    **********************************
    I thought that this would go away if I assingned the character to a different variable everytime it went through - but it didn't.

    Any help would be greatly appreciated for this pathetic newbie

    ps: This isn't for a class - just self learnin - so Im not gonna be using it for school - got that from my last thread

    thanks in advance

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Please post the whole program, otherwise i can't help you

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    7
    as per request:

    Code:
    /* Program to convert numbers into words */
    
    #include <stdio.h>
    #include <string.h>
    
    /* Mess of if statements because I don't know what else to do */
    
    int current_number;
    
    
    
    int main ()
    
    {
    
    int number_array[10];
    char input_number[10];
    int counter;
    /* Get input from user */
    
    printf("please type in a number: ");
    fgets(input_number, sizeof(input_number), stdin);
    counter = 0;
    
    
    current_number = 0;
    counter = 0;
    
    while (1) {
    char current_letter;
    if (input_number[counter] == NULL) {
       break;
     }
     current_number = 0;
     current_letter = input_number[counter];
       sscanf(current_letter, "%d", &current_number);
    
        switch (current_number) {
          case 0:
            printf("zero ");
            continue;
          case 1:
            printf("one ");
            continue;
          case 2:
            printf("two ");
            continue;
          case 3:
            printf("three ");
            continue;
          case 4:
            printf("four ");
            continue;
          case 5:
            printf("five ");
            continue;
          case 6:
            printf("six ");
            continue;
          case 7:
            printf("seven ");
            continue;
          case 8:
            printf("eight ");
            continue;
          case 9:
            printf("nine ");
            continue;
          default:
            continue;
        }
    
    ++counter;
    }
    
    
    
    return(0);
    
    }

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    OK...

    >sscanf(current_letter, "%d", ¤t_number);
    What is the highlighted variable? You haven't declared one with that name.
    This use of sscanf() is incorrect too. The first parameter is supposed to be a pointer to the string, you have passed a single character.

    Also, your use of the array input_number isn't correct. If you use sscanf() with %d it will read all possible numbers until it reaches a seperator (whitespace normally). So therefore, you if a user entered 123, you int variable would get assigned 123, not 1.

    An easier way is to not use sscanf() at all. You can simply loop through each of the characters in the input_number array, testing each one against the numeric ASCII value. Like so

    Code:
    for (........er=0;input_buffer[counter] != '\0';counter++)
    {
      switch (input_number[counter])
      {
          case '1': printf ("one "); break;
         .....
          case '\0': break;
          default: break;
      }
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to call a compiler?
    By castlelight in forum C Programming
    Replies: 3
    Last Post: 11-22-2005, 11:28 AM
  2. Compiler "Warnings"
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 04-24-2005, 01:09 PM
  3. Help With finding a compiler
    By macman in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 04-15-2005, 08:15 AM
  4. Compiler questions
    By DvdHeijden in forum C++ Programming
    Replies: 6
    Last Post: 01-17-2005, 03:00 PM
  5. Have you ever written a compiler?
    By ammar in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 12-27-2004, 07:10 AM