Please help me fix these errors

Reply

Join Date: Dec 2008
Posts: 40
Reputation: RenFromPenn is an unknown quantity at this point 
Solved Threads: 0
RenFromPenn RenFromPenn is offline Offline
Light Poster

Please help me fix these errors

 
0
  #1
Jan 4th, 2009
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. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,029
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 176
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Please help me fix these errors

 
0
  #2
Jan 4th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 40
Reputation: RenFromPenn is an unknown quantity at this point 
Solved Threads: 0
RenFromPenn RenFromPenn is offline Offline
Light Poster

Re: Please help me fix these errors

 
0
  #3
Jan 4th, 2009
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'
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,029
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 176
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Please help me fix these errors

 
0
  #4
Jan 4th, 2009
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():
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 40
Reputation: RenFromPenn is an unknown quantity at this point 
Solved Threads: 0
RenFromPenn RenFromPenn is offline Offline
Light Poster

Re: Please help me fix these errors

 
0
  #5
Jan 4th, 2009
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. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,029
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 176
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Please help me fix these errors

 
0
  #6
Jan 4th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 40
Reputation: RenFromPenn is an unknown quantity at this point 
Solved Threads: 0
RenFromPenn RenFromPenn is offline Offline
Light Poster

Re: Please help me fix these errors

 
0
  #7
Jan 4th, 2009
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. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,851
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Please help me fix these errors

 
0
  #8
Jan 4th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 40
Reputation: RenFromPenn is an unknown quantity at this point 
Solved Threads: 0
RenFromPenn RenFromPenn is offline Offline
Light Poster

Re: Please help me fix these errors

 
0
  #9
Jan 4th, 2009
Originally Posted by Salem View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,029
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 176
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Please help me fix these errors

 
0
  #10
Jan 4th, 2009
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC