Thread: C User input

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    51

    C User input

    I'm struggling with the whole pointer concept at the moment. For example I have a piece of code for testing the first char of a command line arguement but I can't get it to work.

    I have tried different ways but at the minute I have

    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
    	char input = argv[1];
    	printf("\n\nInput : %s\n\n", input);
    	if(input[0]=="M")
    	{
    		printf("\nThis is a message\n");
    	}
    	else if(input[0] == "L")
    	{
    		printf("\nThis is a log-in\n");
    	}
    	else
    	{
    		printf("\n\nwe have no match\n\n");
    	}
    }
    Could someone try and explain to me where I'm going wrong? This version says :

    prefix.c: In function ‘main’:
    prefix.c:5: warning: initialisation makes integer from pointer without a cast
    prefix.c:7: error: subscripted value is neither array nor pointer
    prefix.c:11: error: subscripted value is neither array nor pointer
    Thanks!

  2. #2
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    char input = argv[1];

    argv[1] is an array of char (according to the definition above "int main(int argc, char *argv[])"

    So, you've almost got it, now you simply need to subscript that array as well.

    Edit:

    So, after reading your code more, I would make char input as a pointer, eg char *input = ...

    Then, if you know this syntax, use a switch statement in place of the nested if else -- though this is not a requirement (in fact it adds some overhead for the processing and the if-else is better), but it is more readable.

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    51
    Thanks for reply. However,

    I tried doing

    Code:
    char input = argv[1];
    and i got :

    prefix.c: In function ‘main’:
    prefix.c:7: error: subscripted value is neither array nor pointer
    prefix.c:11: error: subscripted value is neither array nor pointer

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    'argv' is an array of pointers to character. That means that 'argv[x]' is a pointer to a character. That means that you need to dereference THAT to get a single character.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by quzah View Post
    'argv' is an array of pointers to character. That means that 'argv[x]' is a pointer to a character. That means that you need to dereference THAT to get a single character.


    Quzah.
    Is there an echo in this thread???

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Maybe. He didn't seem to read it when you wrote it, a half an hour before his last post, so maybe if we keep saying it, he'll eventually figure it out.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Nov 2009
    Posts
    51
    I did read it. However if you read my post you'd see I said I was new to C and struggling with the concept and usage of pointers.

  8. #8
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Quote Originally Posted by Martin_T View Post
    I'm struggling with the whole pointer concept at the moment. For example I have a piece of code for testing the first char of a command line arguement but I can't get it to work.

    I have tried different ways but at the minute I have

    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
    	char input = argv[1];
    	printf("\n\nInput : %s\n\n", input);
    	if(input[0]=="M")
    	{
    		printf("\nThis is a message\n");
    	}
    	else if(input[0] == "L")
    	{
    		printf("\nThis is a log-in\n");
    	}
    	else
    	{
    		printf("\n\nwe have no match\n\n");
    	}
    }
    Could someone try and explain to me where I'm going wrong? This version says :



    Thanks!

    You are having some problems in your code

    first of all argv[0], argv[1] are pointer to char array means you need a char array to store argv[0] or argv[1] and so on....

    like

    Code:
    char input[100];
    strcpy( input, argv[0]); // copy the whole argv[0] string to input string
    and other one is if you are comparing string like input with "M" as i think you know in C/C++ string means double quotes " " and a single char means ' ' single quotes.

    So you should use strcmp to compare the strings

    Code:
    if (strcmp(input, "M") == 0) {
      // true
    } else {
      // false
    }

  9. #9
    Registered User
    Join Date
    Nov 2009
    Posts
    51
    In case anyone has the same issue.

    In c you cannot directly compare the value of 2 strings in a condition like if(string1==string2)

  10. #10
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Are you asking a question or giving suggestion to forum members ??

  11. #11
    Registered User
    Join Date
    Nov 2009
    Posts
    51
    Sorry yes, I was just wanting to post what I found in case anyone was searching and came across the same problem. I have solved the issue.

  12. #12
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Dude in C you cannot do that this is simple string comparison with == operator

  13. #13
    Registered User
    Join Date
    Nov 2009
    Posts
    51
    I'm confused. If your saying you can't use "==" for string comparison in C then we agree. I had to use strcmp to get my code to work.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I think the two of you are actually in agreement
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. handling user input
    By rodrigorules in forum C++ Programming
    Replies: 4
    Last Post: 11-12-2009, 06:21 AM
  2. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  3. SSH Hacker Activity!! AAHHH!!
    By Kleid-0 in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 03-06-2005, 03:53 PM
  4. Nested Structures - User Input
    By shazg2000 in forum C Programming
    Replies: 2
    Last Post: 01-09-2005, 10:53 AM
  5. comparing user input
    By lambs4 in forum C Programming
    Replies: 5
    Last Post: 12-15-2002, 10:28 AM