Thread: Assistance with C Programming.

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    45

    Assistance with C Programming.

    Hi folks,

    I have 3 questions to ask:

    1) How can I use the printf(); function to align my text to the right of the command box.
    2) How can I translate single integers in an array into a full integer ie. [1,2,4] would translate to 124 (1 hundered and twenty 4).
    3) How can I use functions in a header file, and then make them accessible to every program that uses the header.

    Thanks in advance,
    mintsmike

  2. #2
    Registered User
    Join Date
    Nov 2006
    Location
    japan
    Posts
    126

    Thumbs down

    This smell like homework... Try by yourself and then ask what you don't know
    Mac OS 10.6 Snow Leopard : Darwin

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    45
    This is not homework, rather a personal programming task.

    I have tried everything that I know, yet I do not have the required knowledge to complete the things that i mentioned.

    I am trying to learn from the knowledge of the community.

    PS. I do not ask for help with my homework on the internet, despite the fact that it is highly unreliable, I have an intelligence quotient of 148.

    Thanks,
    mintsmike

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    printf by default right align fields... so you need only specify the width
    Format Specification Fields: printf and wprintf Functions (CRT)

    124 = 1*100 + 2*10+4 - havn't you learned it in the school yet?

    for the 3rd answer - read a book on C-language...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    45
    Quote Originally Posted by vart View Post
    printf by default right align fields... so you need only specify the width
    Format Specification Fields: printf and wprintf Functions (CRT)

    124 = 1*100 + 2*10+4 - havn't you learned it in the school yet?

    for the 3rd answer - read a book on C-language...
    Ok, as for the 124, the character can be a variable amount of length, i.e 4 ints, 5 ints, 6 ints.

    As for the printf(); It does not default align them for me.

    The lines are printed to the left of the screen. I would like them to the right.

    Thanks in advance

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by mintsmike
    1) How can I use the printf(); function to align my text to the right of the command box.
    Standard C has no concept of a "command box" or command prompt, so the above question cannot be answered without resorting to something platform specific. Of course, if you know the dimensions of the command prompt, you could pad the output with spaces such that it is as desired.

    Quote Originally Posted by mintsmike
    2) How can I translate single integers in an array into a full integer ie. [1,2,4] would translate to 124 (1 hundered and twenty 4).
    I am not sure what you are trying to do. You might want to give some example input and output.

    Quote Originally Posted by mintsmike
    3) How can I use functions in a header file, and then make them accessible to every program that uses the header.
    Declare the functions in the header (i.e., place the function prototypes in the header), then include the header wherever you want to use those functions. We actually have an FAQ answer on this, but it appears to be very outdated and written for C++.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    45
    Sorry, by command box I meant the MSDOS terminal. Is there any function that I can use to find the length of the MSDOS terminal?

    Say a sample array of 5 elements. Let it be

    ['h', 'e', 'l', 'l', 'o' ].

    This above does not spell hello.
    I want to translate it into one variable, such as str input=hello;

    Thanks in advance,
    mintsmike

  8. #8
    Registered User
    Join Date
    Mar 2009
    Posts
    45
    Is anyone going to help me or am I going to have to take my custom to another forum

  9. #9
    Registered User Maz's Avatar
    Join Date
    Nov 2005
    Location
    Finland
    Posts
    194
    I honestly do not KNOW the answer considering MS DOS terminal. Although I assume ncurses library may be ported to work with your operating system, and it propably has the means to do the aligning.

    However the last bit is something you will not get an answer. C really does not work like that. The characters you'll have are actually bits in memory. Each character consists of 8 bits, which can be handled as decimal, octal or hexadecimal value. Basic datatype char, will be 8 bits wide. Eg, it is the 8 bits stored in memory, at certain address.

    How you then handle strings in C, is actually handling char arrays. Eg, some amount of chars, placed in memory addresses following each other. The beginning of the string is then handled as pointer - which actually is the address of first character. End of the string is marked by adding 8 bits which all are set to zero at the end of this continuous memory area containing characters.

    Functions handling strings are built upon these assumptions. What they really do, is:

    Get the address of first character from user.
    Read on the 8 bit wide values, untill encounter a byte where all 8 bits are zero.
    Manipulate/copy/compare read bytes to perform desired operation.

    This is efficient way - no extra cpu cycles are used to investigate things more than needed. But this also is really errorprone. Forget the terminating null byte (8 zero bits), and string function will keep reading untill first such slot is found by coincidence - whether the memory was reserved for the process or not.

    Numeric data types, lets say ints, follow the logic, but there is no built in functions for handling "null terminated number series". If you want to handle series of numbers, then you need to play with the amount of memory your serie of numbers uses.

    So basically, C itself can automatically only handle one character, one number, or one address.
    (Or actually, initialization of arrays can actually be done by handling more than one number at a time, but I guess that is not what you meant).

    So when I sum this up, only way to really handle more than one "item" at a time, is to use pointers.

  10. #10
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Sorry, by command box I meant the MSDOS terminal. Is there any function that I can use to find the length of the MSDOS terminal?
    Again - this isn't standard C. The rumors about a Windows port of ncurses appear to be false, as nobody actually links to one or names one - they just say there "is" on. Some entries on sourceforge looked promising though.

    CodeProject: JLib - A Windows Console Library. Free source code and programming help

    That might be good. If that doesn't have the functionality you need just search for a "Windows Console Library". I couldn't find any information about an Official Microsoft library - but your compiler may have one. conio.h. I'd look for you, but I don't have a windows c compiler installed on any of my machines.

    Declare the functions in the header (i.e., place the function prototypes in the header), then include the header wherever you want to use those functions. We actually have an FAQ answer on this, but it appears to be very outdated and written for C++.
    Only the declarations go in the header - meaning you still have to write the actual code elsewhere - you would do that in a library, and then link it with the rest of your project, in addition to #including the header. For standard functions you just include the header because the standard library is linked by default.

    Is anyone going to help me or am I going to have to take my custom to another forum
    This board gets a lot of questions with people wanting to do advanced things when they don't fully understand the basics. It's frustrating on both ends - you can't get good a good answer, but honestly, we don't have an answer to give you without explaining a lot of stuff. Most of us are on the boards very briefly - I only pop in when I have a minute or two at work to check on things. Maz, on the other hand, had time to explain a lot, and was kind enough to do so. It's not because we think you're stupid or because we're mean - it's just that your questions are based on faulty assumptions to begin with. That's hard to correct without sounding rude.

  11. #11
    Registered User
    Join Date
    Jan 2008
    Posts
    290
    Quote Originally Posted by sean
    The rumors about a Windows port of ncurses appear to be false, as nobody actually links to one or names one - they just say there "is" on.
    The OP probably couldn't care less, but in case you want to know, PDCurses (PDCurses - Public Domain Curses) has a Windows port.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hello,i need assistance in completing the code
    By toader in forum C++ Programming
    Replies: 1
    Last Post: 06-22-2009, 03:32 AM
  2. Variable Timer assistance
    By rich2852 in forum C Programming
    Replies: 2
    Last Post: 01-21-2009, 05:43 AM
  3. Any assistance would be greatly appreciated
    By iiwhitexb0iii in forum C Programming
    Replies: 18
    Last Post: 02-26-2006, 12:06 PM
  4. Need some more assistance
    By Thantos in forum Windows Programming
    Replies: 6
    Last Post: 08-14-2003, 12:13 PM
  5. Need assistance for major text base Arena RPG project
    By Ruflano in forum C++ Programming
    Replies: 0
    Last Post: 04-04-2002, 11:11 AM