LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-14-2012, 06:55 AM   #1
marbangens
Member
 
Registered: Aug 2012
Location: sweden
Distribution: Slackware, Fedora
Posts: 118

Rep: Reputation: Disabled
c program and bash arguments


I in doing the gnu c tutorial and stuck on the cow program

#include <stdio.h>

int
main()
{
int cows = 6;

if (cows > 1)
printf("We have cows\n");

if (cows > 10)
printf("loads of them!\n");

return 0;
}

This is no problem as it is here. But I want to make it take arguments
about how many cows I have. The code look like this now.


#include <stdio.h>

int main(int argc, char **argv)
{
int cows;

if (argc != 2)
{
printf("error - you must give an int argument\n");
return 1;
}

if (argc >= 3)
{
printf("error - only one argument is taken");
return 1;
}

cows = *argv;

if (cows > 1 || cows < 9)
printf("We have cows\n");

if (cows > 10)
printf("loads of them!\n");

return 0;
}


The problem is that I always get different output no mater what argument
I give. Like

./a.out 0
you have cows

./a.out 3
you have cows
loads of them!

and sometimes no output...
 
Old 10-14-2012, 07:06 AM   #2
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
When you post code, use [CODE][/CODE] tags around it to make it more legible...

Your problem is that '*argv' is a 'char*' (i.e. argv is a pointer to a pointer to a char, as 'char**' suggests). So you need to convert the numbers written as text in '*argv' into an int. To do this, just use the 'atoi' function:

Code:
cows = atoi(argv[1]);
Also, your second test on argc will never be triggered - you test 'argc != 2' and then 'argc >= 3'. If you wanted to detect the two conditions separately, you should use 'argc < 2' and then 'argc >= 3'.

Hope this helps,

Last edited by Snark1994; 10-14-2012 at 07:09 AM.
 
Old 10-14-2012, 07:08 AM   #3
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
Make sure to put the code in code tags.

Code:
#include <stdio.h>

int main(int argc, char **argv)
{
int cows;

if (argc != 2)
{
printf("error - you must give an int argument\n");
return 1;
}

// this statement is redundant
if (argc >= 3)
{
printf("error - only one argument is taken");
return 1;
}

cows = *argv;

if (cows > 1 || cows < 9)
printf("We have cows\n");

if (cows > 10)
printf("loads of them!\n");

return 0;
}
You can't do 'cows = *argv;' it doesn't make sense, I'm sure the compiler complained with 'assignment makes integer from pointer without a cast'. Instead use sscanf to scan argv for an integer like:

Code:
#include <stdio.h>

int main(int argc, char * argv[])
{
	int cows;

	if (2 != argc)
	{
		fprintf(stderr, "ERROR: Need exactly one argument\n");
		return 1;
	}

	// get cows
	sscanf(argv[1], "%d", &cows);

	if (cows > 1)
	{
		printf("We have cows\n");
	}

	if (cows > 10)
	{
		printf("loads of them!\n");
	}

	return 0;
}
EDIT: or you can use atoi like Snark1994 suggests.

Last edited by H_TeXMeX_H; 10-14-2012 at 07:09 AM.
 
Old 10-15-2012, 02:42 AM   #4
marbangens
Member
 
Registered: Aug 2012
Location: sweden
Distribution: Slackware, Fedora
Posts: 118

Original Poster
Rep: Reputation: Disabled
Thanks for the help.
and I will use "["CODE]"["/CODE] next time.
 
Old 10-16-2012, 03:43 AM   #5
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Did that solve your problem? If so, can you click the "Mark this thread as SOLVED" link at the top of the page, please? That way people know the thread contains a solution.

Thanks,
 
Old 10-24-2012, 12:24 PM   #6
marbangens
Member
 
Registered: Aug 2012
Location: sweden
Distribution: Slackware, Fedora
Posts: 118

Original Poster
Rep: Reputation: Disabled
sorry snark hop you can forgive me for the delay.
I have been busy.
 
Old 10-25-2012, 03:42 AM   #7
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Aha, no problem at all - it's just a bit of etiquette really.

Have a nice day,
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] arguments in bash congos Linux - Newbie 5 12-19-2011 03:54 AM
[SOLVED] Generating two bash arguments from 'ls' mzh Linux - General 4 07-06-2011 09:24 AM
modifying sub program arguments RudraB Programming 5 04-20-2010 09:06 AM
bash script arguments R3N3G4D3 Programming 5 11-01-2007 10:16 AM
bash arguments stuckinhell Programming 6 08-13-2004 05:10 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:45 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration