Member Avatar for RenFromPenn

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:

#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" 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);

Member Avatar for RenFromPenn

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'

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.

Member Avatar for RenFromPenn

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.

#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.

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.

Member Avatar for RenFromPenn

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

#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);	
 }

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.

Member Avatar for RenFromPenn

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.

Member Avatar for RenFromPenn

*Sigh* Okay, but could we please get back to trying to work this out. I'm at a total lose. I really appreciate the help that has been given and I just need to ask you all to show me a bit more kindness so that I can get this thing working.

>I just need to ask you all to show me a bit more kindness so that I can get this thing working.

Kindness is not what you need nor will it get that thing working.
What you need is to understand how scanf() works.

scanf() always leave behind the ENTER key, when you press ENTER/RETURN. Which will be read by the next call.
If you insist in using it, then you need to accommodate that behavior.

A possible solution would be to declare another "waste key char" and using in conjunction with the first format , since you know you'll have to press enter to summit data.

char enter_key;
scanf("%d%c", &num, &enter_key);

Of course, that will not prevent the user from entering unexpected input and screwing it. That's for the best scenario.

Member Avatar for RenFromPenn

I don't insist upon using scanf. If you have a better suggest, then I am perfectly happy to hear it.

Trying you suggestion did cause each line to display. I was able to enter a number, a character, but not the floating point. When The line came up it already had a value there. The program also didn't assemlbe everything and print it as it's supposed to.

--

Okay, this time I tried it and the floating point line didn't have a value already. It, however, wouldn't let me type anything there.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.