| | |
Please help me fix these errors
![]() |
•
•
Join Date: Dec 2008
Posts: 40
Reputation:
Solved Threads: 0
Hi,
I am trying to run this program, but I am getting two error messages that prevent it from running. I don't really understand what the problem is or how to fix it. Could someone please help? Here are the errors:
"error C2664: 'sprintf' : cannot convert parameter 2 from 'int' to 'const char *' Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast"
"error C2562: 'assemble' : 'void' function returning a value: see declaration of 'assemble'"
And here is the code:
I am trying to run this program, but I am getting two error messages that prevent it from running. I don't really understand what the problem is or how to fix it. Could someone please help? Here are the errors:
"error C2664: 'sprintf' : cannot convert parameter 2 from 'int' to 'const char *' Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast"
"error C2562: 'assemble' : 'void' function returning a value: see declaration of 'assemble'"
And here is the code:
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <string.h> #include "string.h" int main() { void assemble(char*input, int num, char let, float flo); char input[100]; int num; char let; float flo; printf("\nPlease enter an integer:\n"); scanf("%d", &num); printf("\nPlease enter a character:\n"); scanf("%c", &let); printf("\nPlease enter a floating point number:\n"); scanf("%f", &flo); assemble(input, num, let, flo); puts(input); return 0; } void assemble(char*input, int num, char let, float flo) { input = sprintf(input, "%d, %c, %f", num, let, flo); return (input); }
Please help me fix these errors is the perfect title to be ignore. Next time avoid such "Please, help me" titles.
>"error C2664: 'sprintf' : cannot convert parameter 2 from 'int' to 'const char *' Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast"
>"error C2562: 'assemble' : 'void' function returning a value: see declaration of 'assemble'"
The prototype should be outside of main;
The use of scanf() to read from user will return unsuspected results, especially after you enter a character and the return "ENTER" key.
scanf("%c", &let);
>"error C2664: 'sprintf' : cannot convert parameter 2 from 'int' to 'const char *' Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast"
sprintf() returns an int representing the number of successful written or a negative if it fails. The assignment to input, therefore, will not work, since input is a string. Remove the comas and spaces between "%d, %c, %f" >"error C2562: 'assemble' : 'void' function returning a value: see declaration of 'assemble'"
void assemble(char*input, int num, char let, float flo) assemble returns a void, but you are telling it to return a string; return (input); The prototype should be outside of main;
void assemble(char*input, int num, char let, float flo); The use of scanf() to read from user will return unsuspected results, especially after you enter a character and the return "ENTER" key.
scanf("%c", &let);
Last edited by Aia; Jan 4th, 2009 at 1:15 pm.
•
•
Join Date: Dec 2008
Posts: 40
Reputation:
Solved Threads: 0
I tried your suggestions and now I get these errors:
error C2440: '=' : cannot convert from 'int' to 'char *' Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
error C2562: 'assemble' : 'void' function returning a value: see declaration of 'assemble'
error C2440: '=' : cannot convert from 'int' to 'char *' Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
error C2562: 'assemble' : 'void' function returning a value: see declaration of 'assemble'
When you make modifications base on suggestions it is alright, and imperative that you provide the code where the changes has been made.
Disregard this part I told you before, somehow I thought I was dealing with sscanf():
That was alright the way that you had it.
Disregard this part I told you before, somehow I thought I was dealing with sscanf():
•
•
•
•
Remove the comas and spaces between "%d, %c, %f"
Last edited by Aia; Jan 4th, 2009 at 2:11 pm.
•
•
Join Date: Dec 2008
Posts: 40
Reputation:
Solved Threads: 0
Here is what I have now with assemble outside of main. I am still getting the same errors that I mentioned in my last post.
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <string.h> #include "string.h" void assemble(char*input, int num, char let, float flo); int main() { char input[100]; int num; char let; float flo; printf("\nPlease enter an integer:\n"); scanf("%d", &num); printf("\nPlease enter a character:\n"); scanf("%c", &let); printf("\nPlease enter a floating point number:\n"); scanf("%f", &flo); assemble(input, num, let, flo); puts(input); return 0; } void assemble(char*input, int num, char let, float flo) { input = sprintf(input, "%d, %c, %f", num, let, flo); return (input); }
You didn't eliminate the return. assemble returns a void.
However that will not give you the expected result, because of the use of scanf() in main.
C Syntax (Toggle Plain Text)
void assemble(char *input, int num, char let, float flo) { sprintf(input, "%d, %c, %f", num, let, flo); }
However that will not give you the expected result, because of the use of scanf() in main.
Last edited by Aia; Jan 4th, 2009 at 3:11 pm.
•
•
Join Date: Dec 2008
Posts: 40
Reputation:
Solved Threads: 0
Well, you were right. It finally ran, but it still didn't work properly. It displayed the line, "Please enter an integer:", so I did. I pressed enter and then it displayed both of the next two lines, "Please enter a character: Please enter a floating point number:"
I tried changing the program as follows to do away with scanf, but that resulted in this error message: "error C2664: 'gets' : cannot convert parameter 1 from 'int' to 'char *'"
I tried changing the program as follows to do away with scanf, but that resulted in this error message: "error C2664: 'gets' : cannot convert parameter 1 from 'int' to 'char *'"
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <string.h> #include "string.h" void assemble(char*input, int num, char let, float flo); int main() { char input[100]; int num; char let; float flo; printf("\nPlease enter an integer:\n"); gets(num); printf("\nPlease enter a character:\n"); gets(let); printf("\nPlease enter a floating point number:\n"); gets(flo); assemble(input, num, let, flo); puts(input); return 0; } void assemble(char *input, int num, char let, float flo) { sprintf(input, "%d, %c, %f", num, let, flo); }
•
•
Join Date: Dec 2008
Posts: 40
Reputation:
Solved Threads: 0
•
•
•
•
Do you have a manual for the C library functions?
Are you using it?
At the moment, you just seem to be typing in stuff without even beginning to think about what you're really saying.
I don't suppose that you could offer something useful instead of just trying to make me look stupid.
![]() |
Similar Threads
- class struct fix 2 new errors need advice (C++)
- Couple of errors in my code (Java)
- C Program help - Conway's Life - Stumped by some errors. (C)
- CHKDSK help - drive is dirty and I cannot fix any errors (Windows NT / 2000 / XP)
- errors in phpbb (PHP)
- CHKDSK hangs; won't fix HD errors--XP Pro (Storage)
- Errors with Linux file system (*nix Hardware Configuration)
- Random errors on new comp (Viruses, Spyware and other Nasties)
Other Threads in the C Forum
- Previous Thread: Need Help..!!
- Next Thread: any one have idea about compiler control directives
| Thread Tools | Search this Thread |
#include adobe ansi api array asterisks binarysearch changingto char character cm copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory dynamic execv feet fflush fgets file fork forloop frequency function getlasterror givemetehcodez global grade graphics gtkgcurlcompiling hacking highest histogram homework i/o include incrementoperators infiniteloop input interest kernel keyboard kilometer linked linkedlist linux linuxsegmentationfault list locate logical_drives looping loopinsideloop. lowest match matrix meter microsoft mqqueue mysql number odf owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research reversing scanf segmentationfault sequential shape socket socketprograming stack standard string systemcall threads turboc unix user voidmain() wab windows.h windowsapi






