Adblock breaks this site

What's wrong with my code?

Discussion in 'Programming General' started by -------owned-------, Aug 18, 2008.

  1. -------owned-------

    -------owned------- Guru
    Banned

    Joined:
    Jan 27, 2007
    Posts:
    1,225
    Referrals:
    0
    Sythe Gold:
    0
    What's wrong with my code?

    Code:
    #include <stdio.h>
    #include <unistd.h>
    char string[50];
    char string2[60];
    int count = 0;  
    void SlowPrint(char thestring[]) {
             while (thestring[count] != '\0') { 
        count++;  
        printf(thestring[count]);
        sleep(50);
    
    }
        return;
           }
    int main(char** argv) {
    scanf("%s", &string2 );
    strcpy(string,string2);
    SlowPrint(string);
    }
    
    It just won't work as supposed. :(
     
  2. Guru Programmer

    Guru Programmer Guest

    Referrals:
    0
    What's wrong with my code?

    does the compiler shows errors, give us details..
     
  3. Nullware

    Nullware Guru

    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0
    What's wrong with my code?

    The Sleep() function has a capital letter and also requires a certain library file to be included. I am using Dev-CPP/MingW compiler and it required that I use #include<windows.h>.

    Also, you were complicating yourself where you didn't have to with your printing function so I reworked it to be simpler. Hopefully this will be of help to you and feel free to ask more questions.:)

    Code:
    #include <stdio.h>
    #include <windows.h>
    
    char strOriginal[50];
    char strCopy[50];
    
    void SlowPrint(char strArray[]) {
             for(int i = 0; strArray[i] ; i++) { // Loop through our string array parameter
                 printf("%c", strArray[i]); // Print out the character the loop is at
                 Sleep(1000); // Wait for 1000 milliseconds
             }
             Sleep(2000); // Wait for 2000 milliseconds now that it has been typed
             
    }
    int main() {
        while (1) { // Keep looping so the program doesn't simply close after one go
        scanf("%s", strOriginal ); // Get the user to input a string
        strcpy(strCopy, strOriginal); // Copy that string into a second variable
        SlowPrint(strCopy); // Slowly print out the string
        }
    }
    
     
  4. -------owned-------

    -------owned------- Guru
    Banned

    Joined:
    Jan 27, 2007
    Posts:
    1,225
    Referrals:
    0
    Sythe Gold:
    0
    What's wrong with my code?

    Thank you. :)
    I used the Dev-C++ IDE too.
    I didn't need to include windows.h.
    The problem was that I didn't include "%c" in the printf.
    (Someone on an IRC helped me).
    Thanks for reworking my function.
     
< When I try to download VB | My First Simple Browser >


 
 
Adblock breaks this site