Thread: Using strchr() and moving the cursor up one row

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    98

    Using strchr() and moving the cursor up one row

    My first question is this:
    Is there a way that I can use strchr() to find the next character occurance, not the first or last, or for it to find the character after a certain point? I tried
    Code:
    char input[]="abcdabcdabcd";
    int x=5;
    strchr(input[x], 'd')
    but it returns an error message: error C2664: 'strchr' : cannot convert parameter 1 from 'char' to 'const char *'.
    My second question is:
    Is it possible to move the cursor(in the command prompt screen) up one row? I have this really old Turbo C++ book that says I must use ANSI.SYS, but thats for DOS(from like before 1990).
    Thanks

  2. #2
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    try &input[x].

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > I have this really old Turbo C++ book
    But which compiler do you have.
    And which operating system for that matter.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This is the help page from Turbo C/C++ ver. 1:
    Code:
    strchr   Finds c in str.
    
     Syntax:
     char *strchr(const char *s, int c);
    
     Prototype in:
     string.h
    
     Remarks:
    strchr scans a string in the forward direction, looking for a specific
    character. strchr finds the first occurrence of the character c in the
    string s.
    
    The null-terminator is considered to be part of the string, so that, for
    example,
    
        strchr(strs,0)
    
    returns a pointer to the terminating null character of the string strs.
    
     Return Value:
    Returns a pointer to the first occurrence of the character c in s. If c does
    not occur in s, strchr returns null.
    
     Portability:
    strchr is available on UNIX systems and is defined in ANSI C.
    
     Example:
     #include <string.h>
     #include <stdio.h>
    
    int main(void)
     {
        char string[15];
        char *ptr, c = 'r';
    
        strcpy(string, "This is a string");
        ptr = strchr(string, c);
        if (ptr)
           printf("The character %c is at position: %d\n", c, ptr-string);
        else
           printf("The character was not found\n");
        return 0;
     }
    I'm assuming you either have an early version of Turbo C/C++, or are thinking of getting it.

    For moving the cursor, you can use the non-standard conio.h include file.

    Adak
    Last edited by Adak; 09-25-2006 at 08:50 PM. Reason: Added [code][/code] tags -- learn to use them yourself.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    98
    I am using Visual C++ 6.0, in Windows XP. Thanks for the help!
    But can anyone tell me how to move the cursor upwards?
    Last edited by Mavix; 09-26-2006 at 11:17 AM.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    You may need a getxy first, then gotoxy with a --y index -
    Code:
    // Sets the position of the text-cursor, starts at 0, 0 on the 
    // top left
    void GotoXY	( int  x, int  y ) 
    { 
        COORD Cursor_Position; 
    
        Cursor_Position.X = x; 
        Cursor_Position.Y = y; 
    
        SetConsoleCursorPosition( GetStdHandle ( STD_OUTPUT_HANDLE ), Cursor_Position ); 
    } 
    
    // Gets the current position of the text-cursor
    void GetXY	( int &x, int &y )
    {
    	CONSOLE_SCREEN_BUFFER_INFO XYInfo;
    
    	GetConsoleScreenBufferInfo( GetStdHandle( STD_OUTPUT_HANDLE ), &XYInfo );
    
    	x = XYInfo.dwCursorPosition.X;
    	y = XYInfo.dwCursorPosition.Y;
    }

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    [edit]
    Code:
    // Gets the current position of the text-cursor
    void GetXY	( int &x, int &y )
    [/edit]
    C doesn't have references. Your code is C++.


    Quzah.
    Last edited by quzah; 09-27-2006 at 12:29 AM. Reason: Editing for clarification for people who don't know what a reference is.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed