Thread: one question :)

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    2

    Question one question :)

    hello...
    i wanted to ask what function i need to use to make my cmd type text like letter by letter :/
    i know on bat it possible , i wanted to ask if on c prog. possible too?


    another question;
    i tried search way to make "if and else" work with letters lol but mm couldnt find ^_6

    need help from u guys ;D
    thanks

  2. #2
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    428
    i wanted to ask what function i need to use to make my cmd type text like letter by letter :/
    i know on bat it possible , i wanted to ask if on c prog. possible too?
    Do you mean like a typewriter one letter at a time rather than the whole line?
    If so you could write a function that takes a parameter that is the phrase to write and then uses a loop to print each letter individualy using the loop control to change the array index. Put a timer/delay function in the loop and dont use '/n' until the end of the line.

    i tried search way to make "if and else" work with letters lol but mm couldnt find ^_6
    are you encapsalating the letter in single quotes?
    Code:
    char a = 'A'
    if(a == 'A') // this should evaluate as true
    Virtual reality hello world http://www.rodneybrothers.com/vr/vrh...rld/index.html in html and javascript.
    Viewable with dodocase, google cardboard, OR, and other compatible VR gear.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    2
    Quote Originally Posted by Lesshardtofind View Post
    Do you mean like a typewriter one letter at a time rather than the whole line?
    If so you could write a function that takes a parameter that is the phrase to write and then uses a loop to print each letter individualy using the loop control to change the array index. Put a timer/delay function in the loop and dont use '/n' until the end of the line.





    are you encapsalating the letter in single quotes?
    Code:
    char a = 'A'
    if(a == 'A') // this should evaluate as true
    can u explain me please how to do the text timer thing im beginner and i like tosee how to do this i would appreciate for some example thnx a lot

  4. #4
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    428
    There are timer libraries or the crudest fashion would be make the computer count to 1000 in between calls. I am not big on giving code because it leads to copy paste and not understanding what is going on. I dont even program C so u could call me a beginner as well. You just have to break down your goal into logical steps and remember google is your best friend. If you are about to ask a one setence question google the same question and see what pops up. I learn new functions libraries and implimentation everyday via google and a collection of books.
    Virtual reality hello world http://www.rodneybrothers.com/vr/vrh...rld/index.html in html and javascript.
    Viewable with dodocase, google cardboard, OR, and other compatible VR gear.

  5. #5
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Quote Originally Posted by mbtm0 View Post
    what function i need to use to make my cmd type text like letter by letter :/
    Use fputc() (or putchar()) to output a single character, fflush() to make sure it is delivered to output (instead of cached by the C library), and sleep(), usleep(), or nanosleep() to delay between outputs.

    Since you didn't mention your OS, you'd better first write a helper header file, msleep.h, to handle delays in milliseconds:
    Code:
    #ifndef   MSLEEP_H
    #define   MSLEEP_H
    
    #ifdef _WIN32
    
    /* Use Windows Sleep() function.
    */
    
    #include <windows.h>
    
    static void msleep(const unsigned int  milliseconds)
    {
        return Sleep(milliseconds);
    }
    
    #else
    
    /* Use Linux/BSD sleep() and/or usleep() function(s).
    */
    
    #ifndef _BSD_SOURCE
    #define _BSD_SOURCE 1
    #endif
    #include <unistd.h>
    
    static void msleep(const unsigned int  milliseconds)
    {
        if (milliseconds >= 1000U)
            sleep(milliseconds / 1000U);
        if (milliseconds % 1000U)
            usleep(1000UL * (milliseconds % 1000U));
    }
    
    #endif
    
    #endif /* MSLEEP_H */
    Here is an example function that uses the above header file. It prints the given string to standard output, one character at a time, with the specified delay in milliseconds (thousands of a second) after each character:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include "msleep.h"
    
    void slowput(const char *string, const unsigned int ms)
    {
        /* Return immediately if NULL. */
        if (!string)
            return;
    
        /* Character loop. */
        while (*string != '\0') {
    
            /* Output character. */
            putchar(*string);
            fflush(stdout);
    
            /* Next character.. */
            string++;
    
            /* Wait before the next character. */
            msleep(ms);
        }
    
        return;
    }
    
    int main(int argc, char *argv[])
    {
        unsigned int  milliseconds;
        char  dummy;
        int   arg;
    
        if (argc < 2 || !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help") || !strcmp(argv[1], "/?")) {
            fprintf(stderr, "\nUsage: %s milliseconds string(s)..\n\n", argv[0]);
            return 1;
        }
    
        if (sscanf(argv[1], " %u %c", &milliseconds, &dummy) != 1) {
            fprintf(stderr, "%s: Invalid number of milliseconds.\n", argv[1]);
            return 1;
        }
        if (milliseconds < 1U) {
            fprintf(stderr, "%s: Milliseconds is too small. Use at least 1.\n", argv[1]);
            return 1;
        }
    
        for (arg = 2; arg < argc; arg++) {
            slowput(argv[arg], milliseconds);
    
            /* Append space; newline after last one. */
            if (arg < argc - 1)
                putchar(' ');
            else
                putchar('\n');
        }
    
        return 0;
    }
    I also included the main() for an example program. It takes the delay between characters (integer milliseconds) as the first command-line parameter, and outputs the other command-line parameters using the slowput() function.

    Compile it using e.g.
    Code:
    gcc -W -Wall -O3 filename.c -o program
    and run via e.g.
    Code:
    ./program 100 'Hello, world!'

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-23-2011, 09:00 AM
  2. *szString = things question/memory question
    By Jang in forum C Programming
    Replies: 3
    Last Post: 01-20-2011, 04:59 AM
  3. Newbish Question file reading question....
    By kas2002 in forum C Programming
    Replies: 23
    Last Post: 05-17-2007, 12:06 PM
  4. java question, how do you interpret this question.
    By indigo0086 in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 11-02-2006, 10:30 AM
  5. Self regiserting DLLs question and libraries question.
    By ApocalypticTime in forum Windows Programming
    Replies: 2
    Last Post: 03-22-2003, 02:02 PM