A very beginner question!

This is a discussion on A very beginner question! within the C Programming forums, part of the General Programming Boards category; Hi, I'm stuck in two exercises and need help. The first one is to write a code which prints out ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    25

    Question A very beginner question!

    Hi,
    I'm stuck in two exercises and need help.
    The first one is to write a code which prints out the ASCII-tabel. I have no clue on this question.
    The second one is to write a code which counts the characters used in a file. I can write a code which counts the number of characters in a row or in a text which is gotten from the terminal (e.x. via "getchar"). but I don't know how it would be with a file.

    Can someone plz help me with these two questions?
    tnx in advance.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Homework policy

    If you post some code that shows that you are trying, we can help you along with some suitable hints and suggestions. Don't expect us to do the homework for you.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Check out Wikipedia or just google for ASCII. Basically, when you press a key, that key has an ASCII number assigned to it. The ASCII table is the list of the values that are agreed upon, for interpreting each key press.

    If you can count the char's in a row of text from the terminal, then you need to just re-direct your input to be from a file, instead. How is the C keyword FILE used?

    I don't feel like repeating what's in your book or class notes. Read up, and post your code to try these exercises.

    Then we can specifically help you out, without getting carpal tunnel syndrome.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Adak View Post
    How is the C keyword FILE used?
    [pedant mode]FILE is not a keyword in C. It is part of the ANSI library definitions, and it's usually a pointer to an anonymous struct [that is, we don't know the internal structure of a FILE struct][/pedant mode].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    25
    Sorry, I didn't even think abt sending my code ! thoughtless done!
    Here is the code.
    I can't go any further. the only thing I've done here is to get the ascii numbers (in decimal form 0 to 127) I don't know how to define the variable "charac" for each characterin the tabel.

    Code:
    #include <stdio.h>
    #include <stdbool.h>
    int main()
    {
        
       int ascinr,i;
       char charac; 
       printf("ASCII-Number       Character\n=======       =========\n");
       for (i=0;1<127;i++){
       ascinr=i;
       
       pritf("%d%f\n" ,ascinr,charac);
       
       }
        return 0;   
    }

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
       printf("%d%f\n" ,ascinr,charac);
    Aside from a misising n, you also have the wrong format for charac: It should be %c.

    Of coruse, charac is also not set to anything, so it will print utter garbage.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,845
    > for (i=0;1<127;i++)
    1 (aka ONE) is always less than 127
    i (aka eye) might eventually make it past 127.
    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.

  8. #8
    abyss - deep C
    Join Date
    Oct 2007
    Posts
    46
    Quote Originally Posted by sifeet View Post
    Sorry, I didn't even think abt sending my code ! thoughtless done!
    Here is the code.
    I can't go any further. the only thing I've done here is to get the ascii numbers (in decimal form 0 to 127) I don't know how to define the variable "charac" for each characterin the tabel.

    Code:
    #include <stdio.h>
    #include <stdbool.h>
    int main()
    {
        
       int ascinr,i;
       char charac; 
       printf("ASCII-Number       Character\n=======       =========\n");
       for (i=0;1<127;i++){
       ascinr=i;
       
       pritf("%d%f\n" ,ascinr,charac);
       
       }
        return 0;   
    }

    Well, the %f in your printf statement says the compiler to expect a floating point value and you are giving a character. Go through your C book and identify what format specifier is to be used for characters. Also, you could look at what are escape sequences to know how to print using tabs.

    cheers
    maverix

  9. #9
    abyss - deep C
    Join Date
    Oct 2007
    Posts
    46
    Well, the %f in your printf statement says the compiler to expect a floating point value and you are giving a character. Go through your C book and identify what format specifier is to be used for characters. Also, you could look at what are escape sequences to know how to print using tabs.

    cheers
    maverix

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner: Linked List question
    By WeatherMan in forum C++ Programming
    Replies: 2
    Last Post: 04-03-2008, 08:16 AM
  2. Quick IF statement question (beginner)
    By jim.rattlehead in forum C Programming
    Replies: 23
    Last Post: 11-29-2007, 06:51 AM
  3. beginner question
    By Barrot in forum C++ Programming
    Replies: 4
    Last Post: 08-19-2005, 03:17 PM
  4. Question About External Files (Beginner)
    By jamez05 in forum C Programming
    Replies: 0
    Last Post: 08-11-2005, 08:05 AM
  5. Beginner on Win32 apps, lame question.
    By Templario in forum C Programming
    Replies: 3
    Last Post: 11-06-2002, 08:39 PM

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