Valhalla Legends Archive

Programming => General Programming => C/C++ Programming => Topic started by: Eli_1 on April 01, 2004, 01:39 PM

Title: Type mis-match
Post by: Eli_1 on April 01, 2004, 01:39 PM
I'm makin a quick proggy to move the mouse cursor to where ever you specify, and on compile I got 2 errors:

Quote
ERROR: 27: Cannot convert 'int' to 'const char *' in function main()
ERROR: 27: Expected 'int', got 'const char *' in function main()
I have no idea why I'm getting this error :(

Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include <winuser.h>

int main() {
   char inputBuffer[256], *p;
   long xPos = 0, yPos = 0;
   
   printf("Input the x and y position seperated by a comma and a space (-1, -1) to quit.\nExample: \"10, 203\"\n\n");

   while ( 1 ) {
      printf("Cursor positions: ");
      fgets(inputBuffer, 256, stdin);
      inputBuffer[ strlen(inputBuffer) - 1 ] = 0; // Remove '\n'

      if (strlen(inputBuffer) > 1) {
         p = strchr(inputBuffer, ', ');
         if (p == NULL) {
            printf("Invalid string format.\n");
            continue;
         } else {
            *p = 0;
            xPos = atoi(inputBuffer);
            p += 2;
            strcpy(inputBuffer, *p);
            yPos = atoi(inputBuffer); // <--- ERROR HERE :-(
            if (xPos == -1 && yPos == -1)
               break;

            SetCursorPos(xPos, yPos);
         }
      }
   }
   printf("Quiting.\n");
   return 0;
}
Title: Re:Type mis-match
Post by: iago on April 01, 2004, 01:45 PM
hm, I thought you were using atoi wrong, but it looks ok:

QuoteNAME
      atoi,  atol, atoll, atoq - convert a string to an integer.

SYNOPSIS
      #include <stdlib.h>

      int atoi(const char *nptr);
      long atol(const char *nptr);
      long long atoll(const char *nptr);
      long long atoq(const char *nptr);

DESCRIPTION
      The atoi() function converts the initial  portion  of  the string  pointed  to  by nptr to int.
Title: Re:Type mis-match
Post by: Eli_1 on April 01, 2004, 01:47 PM
Yea, that's what I checked when it first happened too, but note that it got past the first atoi() usage which is almost identical.

[Edit] Maybe I should be using atol() because my xPos and yPos are declared as longs? I'll try that and get back.

Answer: Nope... :(
Title: Re:Type mis-match
Post by: Telos on April 01, 2004, 02:17 PM
I suggest using the SCAN Formatted function to get your input...
Title: Re:Type mis-match
Post by: Zakath on April 01, 2004, 02:30 PM
The error is not with atoi(). It lies in your usage of the dereferencing operator *. On the previous line you attempted to copy from an int to a string. The syntax SHOULD be:


strcpy( inputBuffer, p );
Title: Re:Type mis-match
Post by: Eli_1 on April 01, 2004, 02:34 PM
That code is there because atoi(*p) didn't work either.

I thought *p ment the value of the variable, and p was the address in memory for it?  :-\

Add-on:
I looked at the prototype and it wants the pointer to it and not the actuall data, doesn't it?
Title: Re:Type mis-match
Post by: Zakath on April 01, 2004, 05:41 PM
No, you misunderstood me. The error is not on the line with atoi, it's on the previous line where you misuse strcpy.
Title: Re:Type mis-match
Post by: vile on April 08, 2004, 02:00 PM
You shouldn't use strcpy(). It's vulnerable to buffer overflows.
Title: Re:Type mis-match
Post by: Skywing on April 08, 2004, 02:01 PM
Quote from: vile on April 08, 2004, 02:00 PM
You shouldn't use strcpy(). It's vulnerable to buffer overflows.
strcpy is fine as long as you use it properly...
Title: Re:Type mis-match
Post by: vile on April 08, 2004, 02:13 PM
As long as you have some other way of checking the boundaries of the buffer, yes (a for loop checking the size of the buffer you're passing to most likely). But that is a lot more typing than using strncpy().