Not sure why my comparisons aren't working between my input (text) and my several char arrays. I'm trying to get the user's input to terminate certain functions as you'll see below.

I realise it's not the most efficient code but if there's anything obvious I'm missing please let me know.

-- Not very experienced in C programming as you can probably see.

#include <stdlib.h>

#include <stdio.h>

#include <string.h>

#include <unistd.h>

char homedir[] = "home";

char getp[] = "getpath";

char pwd[] = "pwd";
char end[] = "exit";
char seten[] = "setpath";

char text[20];

char notext[] = "";





void begin()

{    



fputs("Enter either: \ncd \npwd \ngetpath \nsetpath \nhome\n", stdout);

printf("Or enter: \nexit\nto terminate program\n");

fflush(stdout); 

fgets(text, sizeof text, stdin);

}



void entryerror()

{



printf("Please enter a valid choice.\n");



}



void home()

{

	char *hme = getenv("HOME"); 	

	
if (hme) 
	{
	printf("Value of HOME is: \n%s\n", hme);
	fflush(stdout);
	}



}



void path()

{

/*fgets(text, sizeof text, stdin);

if (strcmp(text, getp) == 0)

{*/

	char *env = getenv("PATH");            

	
if (env)
	{
	printf("Value of PATH is: \n%s\n", env);

	fflush(stdout);
	}

}	


	

	/*entryerror();

	begin();*/



void printwd()

{   	  



	char *pwd = getenv("PWD");

if(pwd)

	{

	printf("Value of PWD is: \n%s\n", pwd);

	fflush(stdout); 

	}

	

}



void printcheck()

{

if (strcmp(text, pwd) > 0)

{

printwd();

}

else

{

entryerror();

main();

}

}

void setenviro()
{
int set = setenv("PATH", "PC", 1);

char *env1 = getenv("PATH");

      if (env1)

            printf("Value of PATH after 'setenv' is: \n%s\n", env1);
}













int main()



{

begin();
if (strcmp(text, pwd) >0)
{

printwd();
}
else if (strcmp(text, getp) >0)
{
path();
}
else if (strcmp(text, homedir) >0)
{
home();
}
else if (strcmp(text, end) >0)
{
exit(1);
}
else if (strcmp(text, seten) >0)
{
setenviro();
}
else
{
entryerror();
main();
}
main();



	



  return 0;

Forgot to mention that this code is incomplete (Which is probably quite clear!)
Thanks,

Dec.

fgets includes the trailing newline, so when the user types "pwd", the resulting string is actually "pwd\n". Try removing the newline after your fgets:

if (fgets(text, sizeof text, stdin) != NULL)
    text[strcspn(text, "\n")] = '\0';

Never call main().
Use return.

Thanks for your reply Narue:

I cant seem to find my \n after my fgets? Im confused.
When I run the program, when I type: "pwd" the correct thing shows. "getpath" the correct thing shows. However, when I type "home" it prints the statement in the getpath() method. This is why I think it's my char[] compares using strcmp's?

Thanks Caligulaminus:

What do you mean? Just use "return" instead of main()? Does it do the same thing?
It it dangerous or just bad practice calling main()?

Thanks Caligulaminus:

What do you mean? Just use "return" instead of main()? Does it do the same thing?
It it dangerous or just bad practice calling main()?

If you're done in a Function you want to end (and return from) it.
If you call main() it would be (unwanted) recursion eventually overflowing your stack.

main()
     main()
          main()
               main()
                    main()
                         ...

Besides many other Problems...

However, when I type "home" it prints the statement in the getpath() method.

If the strings are equal, strcmp will return 0. You're always checking if the result is greater than 0 rather than equal to 0.

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.