• Log in or sign up for an account here: https://www.sythe.org/login

What's wrong with my code?

Joined
Jan 27, 2007
Sythe Gold
0
Messages
1,225
Location
/bin/sh
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. :(
 
What's wrong with my code?
does the compiler shows errors, give us details..
 
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
    }
}
 
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
    }
}
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.
 
Runescape Gamblingrunescape gambling
Back
Top