Advertisement

I did my first program, I need help.

Started by September 20, 2002 06:14 AM
5 comments, last by Cinnamon 22 years, 1 month ago
I did a little program with what I learned until now. If you are advanced, you will probably find that the structure is bad. Anyway, I will improve over time. I need help with a part of my little program, when I use a loop so that the characters prints with a time interval. #include <stdio.h> #include <windows.h> #include <conio.h> void menuoption() { int min=0, max=50, i=0; char graph; printf("hit a symbol on your keyboard to choose your menu''s skin\t"); scanf("%c", &graph); printf("\r"); for(i=min; i<=max; i++) { printf("%c", graph); } printf("\n%c%c%c Chapter One: Melchior Hut %c%c%c\n", graph, graph, graph, graph, graph, graph); for(i=min; i<=max; i++) { printf("%c", graph); } } void message() { int speed=0; int x=0; int min=1; int max=12; char text01[]="abcdefghijkl"; printf("Choose the message speed between 1 and 3\t"); scanf("%d", &speed); switch(speed) { case 1: x=75; break; case 2: x=125; break; case 3: x=200; break; default: x=125; /*How do I tell the computer here "start back at scanf("%d", &speed);???*/ break; } /* here is the only stupid way I found to make time interval between each characters*/ printf("Melchior: "); Sleep(500); printf("H"); Sleep(x); printf("e"); Sleep(x); printf("l"); Sleep(x); printf("l"); Sleep(x); printf("o"); Sleep(x); printf(" "); Sleep(x); printf("y"); Sleep(x); printf("o"); Sleep(x); printf("u"); Sleep(x); printf("n"); Sleep(x); printf("g"); Sleep(x); printf(" "); Sleep(x); printf("t"); Sleep(x); printf("r"); Sleep(x); printf("a"); Sleep(x); printf("v"); Sleep(x); printf("e"); Sleep(x); printf("l"); Sleep(x); printf("e"); Sleep(x); printf("r"); Sleep(x); printf("\nCecil: "); Sleep(500); printf("W"); Sleep(x); printf("h"); Sleep(x); printf("o"); Sleep(x); printf(" "); Sleep(x); printf("a"); Sleep(x); printf("r"); Sleep(x); printf("e"); Sleep(x); printf(" "); Sleep(x); printf("y"); Sleep(x); printf("o"); Sleep(x); printf("u"); Sleep(x); printf("?"); Sleep(x); printf("?"); Sleep(x); printf("?\n"); Sleep(x); /* The problem is here. I wanted to improve the way to do time interval between each characters. When I compile, there is no error, but when I run, there''s a bug I can''t find.*/ while(min<=max) { Sleep(x); printf("%s\n",text01[min++]); min++; } } fermer() { do { } while(!kbhit()); return EXIT_SUCCESS; } void main() { menuoption(); printf("\n\n"); message(); printf("\n\n"); fermer(); }
Your problem seems to be that rather than printing a single character, you are trying to print a string, which doesn't make much sense.

This function prints the message (szMsg) with delay milliseconds between each character.


    void delay_print(const char* szMsg,int delay){  char* pMsg;   for( pMsg = szMsg ; *pMsg != 0x00; pMsg++)  {    printf( "%c", *pMsg );    Sleep(delay);    }}    


EDIT: Just realized it might not be obvious how to use this:


  switch(speed){case 1:x=75;break;case 2:x=125;break;case 3:x=200;break;default:x=125;/*How do I tell the computer here "start back at scanf("%d", &speed);???*/break;}/* here is the only stupid way I found to make time interval between each characters*/printf("Melchior: "); Sleep(500);delay_print("Hello young traveller",x);  


Incidentally, if you want to make sure the user does enter a value, enclose the whole lot in a loop:


  int x = -1;do{printf("Choose the message speed between 1 and 3\t");scanf("%d", &speed);switch(speed){case BLAH1:break;case BLAH2:break;default:  x = -1;  break;}} while( x == -1 );<SPAN CLASS=editedby>[edited by - JuNC on September 20, 2002 7:53:13 AM]</SPAN>  
Advertisement
Okay you''ve made a couple of mistakes here:

a) The line:

printf("%s\n",text01[min++]);

%s means string and the value text01[min++] is a character
changing the line to:

printf("%c\n",text01[min++]);

corrects the first bug.

b) Arrays in C are zero based, this means that the first character in text01 is text01[0], text01[1] is the second character in the string. Therefore you want your while loop to run from 0-11 not 1-12. Changing the starting values of min and max to 0 and 11 respectively fixes that bug.

c) You''ve got min++ twice in your while loop, this won''t crach the program but it does mean that 2 is added to min every iteration instead of 1. Either remove the ''++'' from

printf("%c\n",text01[min++]);

Or lose the second min++ line entirely.

As a sidenote on strings one important fact to remember is that C strings always end in a NULL character (backslash zero /0), therefore if you want to have a string which contains 30 letters then the array must be 31 characters long, 30 characters for the letters and 1 extra character to store the \0 terminating character. The line:

char myString[12]="abcdefghijkl";

Will therefore cause an error since you are not allocating enough space to hold the NULL character.
Thank you very much every one! I learned a lot with your help! You were pretty precise and I appreciate it!

Someday I will be the one who will help people on this forum.
Now I try to study pointers because I didn''t understand Junc''s exemple. I did some tries all the night. I was able to do easy program with pointers, but that one give me painful time.

#include <stdio.h>
#include <windows.h>

void Operation(int *i);

void Operation(int *i)
{
int min=0;
int max=10000;

for(*i=min; *i<=max; *i++);
{
printf("%d", *i);
Sleep(2000);
}
}

void main()
{
int a=0;
Operation(&a);
printf("%d\n\n", a);

}


My idea was to print a number range using pointers. Unfortunnatelly, it didn''t work. I tried different combinaisons, but the only result I got was a head ache...
The problem is in your for loop, where you''re trying to increment the value of i like so: *i++

Your compiler reads this as "the value of (i + 1)", but you want "(the value of i) + 1". Change it to (*i)++ and you should be ok.

I suppose you could also write it as ++*i, but that''s just damn ugly.
Advertisement
Be warned that (*i)++ != ++*i

(*i)++: increments after (return the value of i then increment)
++*i : increments before (increment then return the value of i)

This topic is closed to new replies.

Advertisement