Jump to content

Assigning Command Line Floats to Variables (C)

FaiL___

Hi, I posted something similar to this last week (still stuck), but I now have a different problem. I can retrieve two values from the command line (which is what I was having trouble with originally) and can print them to the screen just by doing:

printf(argv[1];

	//OR

printf(argv[2]);

 

however, when I try to assign these to variables, the variables just contain 0.000000000. I don't know why this does this, but I need to be able to assign them to variables so that I can add them together. This is the code I have so far:

 

#include <stdio.h>

#pragma warning(disable: 4996)

int main(int argc, char *argv[])
{
	double numberOne, numberTwo = 0, answer = 0;
	if(argc = 3)
	{
		/*
		First number entered in the command line
		*/

		printf("The first argument is: %s", argv[1]);
		numberOne = atof(argv[1]);
		printf("\n");
		printf("%f", &numberOne);

		/*
		Second number entered in the command line
		*/

		printf("The second argument is: %s", argv[2], "\n");
		numberTwo = atof(argv[2]);
		printf("\n");
		printf("%f", &numberTwo);


		printf("\n");
		/*
		Calculations and output
		*/

		answer = numberOne + numberTwo;
		printf("The answer of %f + %f is: %f.", &numberOne, &numberTwo, &answer);
	}
	else
	{
		printf("Something went wrong");
	}
	getchar();
}

Thanks in advance.

There are 10 types of people in the world. Those that understand binary and those that don't.

Link to comment
Share on other sites

Link to post
Share on other sites


printf("%f", &numberTwo);

You're passing the address of the float, not the float. Get rid of the &. Same goes for all the other printfs you have.

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

9 minutes ago, fizzlesticks said:



printf("%f", &numberTwo);

You're passing the address of the float, not the float. Get rid of the &. Same goes for all the other printfs you have.

I have changed this now after realising it myself, but am now having trouble because when I do:

		else if ((numberTwo == 0) && (argv[2] == 0))

if argv[2] is actually 0, it just says no it is not and skips to the next else if statement. Help?

There are 10 types of people in the world. Those that understand binary and those that don't.

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, FaiL___ said:

if argv[2] is actually 0, it just says no it is not and skips to the next else if statement. Help?

argv[2] is a char* and you're trying to compare it to an int, that doesn't work.

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, fizzlesticks said:

argv[2] is a char* and you're trying to compare it to an int, that doesn't work.

So how would I fix it?

 

There are 10 types of people in the world. Those that understand binary and those that don't.

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, fizzlesticks said:

argv[2] is a char* and you're trying to compare it to an int, that doesn't work.

Because it works with argv[1]

There are 10 types of people in the world. Those that understand binary and those that don't.

Link to comment
Share on other sites

Link to post
Share on other sites

4 minutes ago, FaiL___ said:

So how would I fix it?

You convert argv[2] to an int using atoi then compare them.

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

But if it is a letter, it will be converted into 0 again, I need to be seeing if a letter was entered or not, if a letter was entered, the float value of the argument (atof(argv[2]) is 0, so I check to see if argv[2] is 0, if it is, fine, if not, not fine, it works for argv[1], but not argv[2]. I am leaving it for the night now as it is 1.30am where I am, but in the morning, I will add my modified code, which is more organised and I think might actually be very close to working. Cheers anyway though.

There are 10 types of people in the world. Those that understand binary and those that don't.

Link to comment
Share on other sites

Link to post
Share on other sites

11 hours ago, fizzlesticks said:

You convert argv[2] to an int using atoi then compare them.

This is my new(ish) code:

#include <stdio.h>
#include <stdlib.h>
//hi babe

#pragma warning(disable: 4996)

int main(int argc, char *argv[])
{
	float numberOne = 0, numberTwo = 0, answer = 0;
	
	if (argc == 3)
	{
		numberOne = atof(argv[1]);

		numberTwo = atof(argv[2]);

		if ((numberOne == 0) && (argv[1] != 0))
		{
			printf("invalid input");
		}
		else if ((numberTwo == 0) && (argv[2] != 0))
		{
			printf("invalid input");
		}
		else if ((numberOne == 0) && (argv[1] == 0))
		{
			answer = numberOne + numberTwo;
			printf("%f", answer);
		}
		else if ((numberTwo == 0) && (argv[2] == 0))
		{
			answer = numberOne + numberTwo;
			printf("%f", answer);
		}
		else if ((numberOne != 0) && (numberTwo != 0))
		{
			answer = numberOne + numberTwo;
			printf("%f", answer);
		}
	}
}

 

There are 10 types of people in the world. Those that understand binary and those that don't.

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, FaiL___ said:

This is my new(ish) code:

<snip>

"argv[1]", "argv[2]" and so on are pointers that point to the beginning of a string containing the command line parameter in question. That is, they contain a memory address, pointing to the memory location where the string is located, they do not contain the actual string.

 

Thus, if this condition is true:

argc == 3

then "argv[1]" and "argv[2]" can never equal zero. 0 (NULL) is a reserved pointer value meaning the pointer is not pointing a valid object. But since "argc" is equalling 3 they must be pointing to a string and thus can never be NULL.

 

As such, your latest code is incorrect, it will detect "0" as "invalid input".

For example, if the user passes 0 as the first command line parameter then:

		numberOne = atof(argv[1]);		//numberOne will become equal to 0, because the user entered a actual 0.

		if ((numberOne == 0) && (argv[1] != 0)) //But the pointer value in argv[1] is not NULL, because it points to a valid string containing "0".
		{
			printf("invalid input");
		}

A solution would be to use "strtod" in stead of "atof". "strtod" also converts a string to double, but it takes the address of a char pointer as second argument.

If the conversion succeeds this pointer points to the character after the last character used in the conversion. If the conversion fails this pointer points to the beginning of the string to convert. So we can test for failure by comparing the pointers. If they match, no conversion was made thus indicating invalid input:

#include <stdio.h>
#include <stdlib.h>
//hi babe

#pragma warning(disable: 4996)

int main(int argc, char *argv[])
{
	float numberOne = 0, numberTwo = 0, answer = 0;
	
	if (argc == 3)
	{
		char *endptr;
		numberOne = strtod(argv[1], &endptr);
		if (endptr == argv[1])
		{
			printf("invalid input argument 1.\n");
			return 1;
		}

		numberTwo = strtod(argv[2], &endptr);
		if (endptr == argv[2])
		{
			printf("invalid input argument 2.\n");
			return 2;
		}

		answer = numberOne + numberTwo;
		printf("Answer: %f\n", answer);
	}
	else
	{
		printf("This program wants 2 arguments!\n");
	}
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

Not a solution, just a diagnostic step. 

use printf("%s", argv[2]) to make sure you're looking at the right string.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×