Thread: Moving cursor to the end of line

  1. #1
    Registered User
    Join Date
    May 2008
    Location
    IR, Iran
    Posts
    103

    Moving cursor to the end of line

    I have wrote this:
    Code:
    #include <stdio.h>
    #include <conio.h>
    
    void main()
    {
    	/* variables define here */
    	char lgn, beep = 0x07, ctrl_char;
    	
    	/* Start of program */
    	printf("Welcome to Students DataBase\n");
    	
    	/* Main page */
    	main:
    		printf("you want to login as Teacher or Student (T/S):");
    		
    		lgn_check:
    		lgn = getche();
    		if (lgn == 0x08){
    			printf("%c:", beep);
    			goto lgn_check;
    		}
    		else if (lgn == 0x0D || lgn == 0x03){
    			printf("%c", beep);
    			goto lgn_check;
    		}
    		ctrl_char = getch();
    		
    		ctrl_char_check:
    		if (ctrl_char == 0x08){
    			printf("\b");
    			lgn = getche();
    			ctrl_char = getch();
    			
    			if (ctrl_char == 0x08)
    				goto ctrl_char_check;
    		}
    		
    		switch (lgn){
    			case 'T':
    			case 't':
    				printf("\nyou loged in as Teacher.\nPlease enter your password:");
    				goto teacher;
    				break;
    			case 'S':
    			case 's':
    				printf("\nyou loged in as Student.\n");
    				goto student;
    				break;
    			default:
    				printf("\n%cError: unknown command\n", beep);
    				goto main;
    		}
    	// End of main loop
    	
    	teacher:
    		/* teacher commands */
    	// End of teacher loop
    	
    	student:
    		/* student commands */
    	// End of student loop
    }
    when I press "enter" key the cursor go to start of line. but I want it stay where it is.

    thanks to all

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You need to clean up your code a bit.
    Void main is bad: http://cpwiki.sourceforge.net/Void_main
    Goto is bad. Use loops.
    Don't use "magic numbers" such as 0x08, use appropriate characters (like '\n' or so).
    And then you are using non-standard functions such as getche (avoid them).

    As for the cursor... if I'm thinking right, then it's platform specific or may be found somewhere in the depths of some conio.h header or some other unportable non-standard functions.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    May 2008
    Location
    IR, Iran
    Posts
    103
    Quote Originally Posted by Elysia View Post
    You need to clean up your code a bit.
    Void main is bad: http://cpwiki.sourceforge.net/Void_main
    Goto is bad. Use loops.
    Don't use "magic numbers" such as 0x08, use appropriate characters (like '\n' or so).
    And then you are using non-standard functions such as getche (avoid them).

    As for the cursor... if I'm thinking right, then it's platform specific or may be found somewhere in the depths of some conio.h header or some other unportable non-standard functions.
    you said use loops, which loop can act like goto!?
    another question, why goto is bad?
    Last edited by behzad_shabani; 05-30-2008 at 12:40 PM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    you said use loop, which loop can act like goto!?
    All of them, but a while loop or a do while loop look most appropriate here. Your teacher and student labels should be replaced by functions, with the corresponding gotos replaced by function calls.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by behzad_shabani View Post
    you said use loop, which loop can act like goto!?
    If you want to repeat something a certain number of times, a for-loop.
    If you want to do something "zero or more times until some condition", then a while-loop is generally the right thing.
    If you want to do soemthign "at least once, until some condition", then do-while-loop is generally the right thing.

    The goto's in the switch-statements should probably be function calls, rather than goto's.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by laserlight View Post
    Your teacher and student labels should be replaced by functions, with the corresponding gotos replaced by function calls.
    Did you read my mind, or did I read yours?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  2. read to end of line problem
    By one1082 in forum C++ Programming
    Replies: 3
    Last Post: 11-07-2003, 04:30 PM
  3. writing new line at end of file
    By deleeuw in forum C++ Programming
    Replies: 4
    Last Post: 09-23-2003, 10:23 PM
  4. How can I read until the end of an input line?
    By Bad_Scooter in forum C++ Programming
    Replies: 4
    Last Post: 07-19-2003, 09:29 PM
  5. moving a line in videomode 320*200
    By GanglyLamb in forum Game Programming
    Replies: 4
    Last Post: 12-27-2002, 05:35 PM