C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-06-2007, 05:37 AM   #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.
sifeet is offline   Reply With Quote
Old 11-06-2007, 05:49 AM   #2
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
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.
matsp is offline   Reply With Quote
Old 11-06-2007, 05:56 AM   #3
Registered User
 
Join Date: Sep 2006
Posts: 3,148
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.
Adak is offline   Reply With Quote
Old 11-06-2007, 06:00 AM   #4
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
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.
matsp is offline   Reply With Quote
Old 11-06-2007, 06:59 AM   #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;   
}
sifeet is offline   Reply With Quote
Old 11-06-2007, 07:09 AM   #6
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
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.
matsp is offline   Reply With Quote
Old 11-06-2007, 07:12 AM   #7
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,681
> 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.

Salem is offline   Reply With Quote
Old 11-06-2007, 07:12 AM   #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
maverix is offline   Reply With Quote
Old 11-06-2007, 07:13 AM   #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
maverix is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 01:52 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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