Saturday, February 26, 2011
0
C Program Language (using stdlib.h to repeat)
The header stdlib.h is very important in C language because it allow the user to repeat again to the main or exit/end the program. This used when the condition is given from the program and asked to repeat again or terminate. As we go along in our topic we will learn the different ways how to used the stdlib.h header in repeating the condition. Here are some example output that instruct the user either to repeat or to exit.
As you can see above. The is asked the user to input integer and instruct the if the user will going to repeat or not. The user is being asked to input integer 1 to repeat and other integer to end. Now see the codes.
#include<stdio.h> #include<conio.h> #include<stdlib.h> main() { int num; int choice; printf("Enter Numbers: "); scanf("%d",&num); printf("\npress 1 to repeat and other # to end: "); scanf("%d",&choice); if(choice==1){ system("cls"); return main(); } else return 0; getch(); }
The codes is based on the output above. The codes used the if conditional statement.Where the word system("cls"); is for clear screen. Thus, the return main represent as repeat again and return 0 to terminate the program.
if(choice==1){ system("cls"); return main(); } else return 0;
}
Not we will used another conditional statement and by these time we will be using the switch conditional statement in returning to main or exit. Observe the codes that being used.
switch(choice){ case 1: system("cls"); return main(); break; case 2: return 0; }
Another thing is declaring variable using characters. The choices will be based on the letters option. Check this out a program using the character declaration for the variables.
As you can see above. The program asked the user to input the the letter thatrepresent every condition. Y for yes and other letter for exit.Now check the codes that was being used.
#include<stdio.h> #include<conio.h> #include<stdlib.h> main() { int num; char choice; printf("Enter Numbers: "); scanf("%d",&num); printf("\npress y to repeat and other variable to end: "); scanf("%s",&choice); if(choice=='y'){ system("cls"); return main(); } else{ return 0; } getch(); }
This post is very simple but the content are usually used by a programmer student like me to solve some problems in programming. Hope this post help you.
Subscribe to:
Post Comments (Atom)
0 Responses to “C Program Language (using stdlib.h to repeat)”
Post a Comment