Valhalla Legends Forums Archive | C/C++ Programming | Type mis-match

AuthorMessageTime
Eli_1
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()
[/quote]
I have no idea why I'm getting this error :(

Code:
[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;
}
[/code]
April 1, 2004, 7:39 PM
iago
hm, I thought you were using atoi wrong, but it looks ok:

[quote]NAME
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.
[/quote]
April 1, 2004, 7:45 PM
Eli_1
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... :(
April 1, 2004, 7:47 PM
Telos
I suggest using the SCAN Formatted function to get your input...
April 1, 2004, 8:17 PM
Zakath
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:

[code]
strcpy( inputBuffer, p );
[/code]
April 1, 2004, 8:30 PM
Eli_1
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?
April 1, 2004, 8:34 PM
Zakath
No, you misunderstood me. The error is not on the line with atoi, it's on the previous line where you misuse strcpy.
April 1, 2004, 11:41 PM
vile
You shouldn't use strcpy(). It's vulnerable to buffer overflows.
April 8, 2004, 7:00 PM
Skywing
[quote author=vile link=board=30;threadid=6125;start=0#msg54077 date=1081450800]
You shouldn't use strcpy(). It's vulnerable to buffer overflows.
[/quote]
strcpy is fine as long as you use it properly...
April 8, 2004, 7:01 PM
vile
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().
April 8, 2004, 7:13 PM

Search