943,778 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1452
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Jan 4th, 2009
0

Please help me fix these errors

Expand Post »
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:

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "string.h"
  4.  
  5. int main()
  6. {
  7. void assemble(char*input, int num, char let, float flo);
  8. char input[100];
  9. int num;
  10. char let;
  11. float flo;
  12.  
  13. printf("\nPlease enter an integer:\n");
  14. scanf("%d", &num);
  15. printf("\nPlease enter a character:\n");
  16. scanf("%c", &let);
  17. printf("\nPlease enter a floating point number:\n");
  18. scanf("%f", &flo);
  19.  
  20. assemble(input, num, let, flo);
  21. puts(input);
  22. return 0;
  23. }
  24.  
  25. void assemble(char*input, int num, char let, float flo)
  26. {
  27. input = sprintf(input, "%d, %c, %f", num, let, flo);
  28. return (input);
  29. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
RenFromPenn is offline Offline
40 posts
since Dec 2008
Jan 4th, 2009
0

Re: Please help me fix these errors

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"

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.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Jan 4th, 2009
0

Re: Please help me fix these errors

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'
Reputation Points: 10
Solved Threads: 0
Light Poster
RenFromPenn is offline Offline
40 posts
since Dec 2008
Jan 4th, 2009
0

Re: Please help me fix these errors

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():
Quote ...
Remove the comas and spaces between "%d, %c, %f"
That was alright the way that you had it.
Last edited by Aia; Jan 4th, 2009 at 2:11 pm.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Jan 4th, 2009
0

Re: Please help me fix these errors

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.

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "string.h"
  4. void assemble(char*input, int num, char let, float flo);
  5.  
  6. int main()
  7. {
  8. char input[100];
  9. int num;
  10. char let;
  11. float flo;
  12.  
  13. printf("\nPlease enter an integer:\n");
  14. scanf("%d", &num);
  15. printf("\nPlease enter a character:\n");
  16. scanf("%c", &let);
  17. printf("\nPlease enter a floating point number:\n");
  18. scanf("%f", &flo);
  19.  
  20. assemble(input, num, let, flo);
  21. puts(input);
  22. return 0;
  23. }
  24.  
  25. void assemble(char*input, int num, char let, float flo)
  26. {
  27. input = sprintf(input, "%d, %c, %f", num, let, flo);
  28. return (input);
  29. }
Reputation Points: 10
Solved Threads: 0
Light Poster
RenFromPenn is offline Offline
40 posts
since Dec 2008
Jan 4th, 2009
0

Re: Please help me fix these errors

You didn't eliminate the return. assemble returns a void.

  1. void assemble(char *input, int num, char let, float flo)
  2. {
  3. sprintf(input, "%d, %c, %f", num, let, flo);
  4. }

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.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Jan 4th, 2009
0

Re: Please help me fix these errors

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 *'"

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "string.h"
  4. void assemble(char*input, int num, char let, float flo);
  5.  
  6. int main()
  7. {
  8. char input[100];
  9. int num;
  10. char let;
  11. float flo;
  12.  
  13. printf("\nPlease enter an integer:\n");
  14. gets(num);
  15. printf("\nPlease enter a character:\n");
  16. gets(let);
  17. printf("\nPlease enter a floating point number:\n");
  18. gets(flo);
  19.  
  20. assemble(input, num, let, flo);
  21. puts(input);
  22. return 0;
  23. }
  24.  
  25. void assemble(char *input, int num, char let, float flo)
  26. {
  27. sprintf(input, "%d, %c, %f", num, let, flo);
  28. }
Reputation Points: 10
Solved Threads: 0
Light Poster
RenFromPenn is offline Offline
40 posts
since Dec 2008
Jan 4th, 2009
0

Re: Please help me fix these errors

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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Jan 4th, 2009
0

Re: Please help me fix these errors

Click to Expand / Collapse  Quote originally posted by Salem ...
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.
If I knew what I was doing, then I wouldn't be asking for help. In answer to your question, no I don't have a manual. All I have is the help file and it isn't really any help at all.

I don't suppose that you could offer something useful instead of just trying to make me look stupid.
Reputation Points: 10
Solved Threads: 0
Light Poster
RenFromPenn is offline Offline
40 posts
since Dec 2008
Jan 4th, 2009
0

Re: Please help me fix these errors

Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Need Help..!!
Next Thread in C Forum Timeline: any one have idea about compiler control directives





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC