Like Tree2Likes
  • 1 Post By theju112
  • 1 Post By whiteflags

Counting the number of inputs

This is a discussion on Counting the number of inputs within the C Programming forums, part of the General Programming Boards category; Hi, I am trying to get the input from the user as strings, the end of which is characterized by ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    2

    Counting the number of inputs

    Hi,
    I am trying to get the input from the user as strings, the end of which is characterized by the string "end". So, the desired output will be the number of strings entered till the string "end". Say, the user inputs :
    hello
    world
    1234
    end
    the output should be :
    you entered 3 inputs
    The code with which I was trying to achieve the above is :
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    char *str[30][100]={'\0'};
    int n=0,i=0,counti=0,flag=0;
    char *fun_str_ip(char *str_ip[30][100],int num);
    int main()
    {
     printf("Start Entering Inputs :");
     while(!flag)
     {
      if(strcmp(*str,"end")==0)
      {
       flag=1;
       break;
      }
      else
      {
       for(i=0;i<counti;i++)
       {
        fun_str_ip(*str_ip,i);
       }
       counti++;
      }
     }
     printf("You have entered %d inputs\n",counti);
    }
    char *fun_str_ip(char *str_ip[30][100],int num)
    {
     printf("Enter Input Number %d :",num);
     scanf("%s",&str_ip[30][num]);
    }
    but this was my second attempt.
    The following was my first :
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    int main()
    {
    *char inpc[30][100];
     int i,counti=0,counta=0;
     do
     {
      printf("Enter The Inputs (Enter END when done) :\n");
      for(i=0;i<100;i++)
      {
       scanf("%s",&inpc[30][i]);
       counta++;
       if(inpc[30][i]=="END\0"||"end\0")
       {
        break;
       }
       counti++;
      }
      break;
     }while(inpc[30][100]!="END\0")
     printf("You have entered %d inputs\n",counti);
     return(0);
    }
    The moment i finished typing both the snippets i had this feeling of deja vu. It so occured that both didn't run the way they were intended to. I know I'm missing something really important. Can someone please point out what is missing and help me? Thanks in advance.

  2. #2
    Registered User
    Join Date
    Aug 2011
    Posts
    91
    there are many issues to be addressed here..but i think that you have to learn a bit more on 2d arrays. and also on pointers..

    so ill paste your code and give you some hints as comments next to them so that you can try to understand mistakes..

    i have a ready version working, but i dont wont to give it away freely..but ill try to get you there...

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    
    char *str[30][100]={'\0'}; 
    /* what are you tring to declare above? i guess that you want to declare a 2d array which
    can store strings(one word in each row) but why have you used a pointer??
    all you need is char str[30][100] which means you have now declared an array which can store 30 
    words, each of which can have 100 characters*/
    int n=0,i=0,counti=0,flag=0;
    
    char *fun_str_ip(char *str_ip[30][100],int num);
    int main()
    {
     printf("Start Entering Inputs :");
     while(!flag)
     {
     /*in the if statemen below,what are you actually trying to compare? */
     /*i mean you have not taken any input string yet so you cant compare yet*/
     /*also *str? what does that mean?*/	
      if(strcmp(*str,"end")==0) 
      {
       flag=1;
       break; /*if your above condiiton is met, then the loop will bw exited so what is the need of
       the condition check inside while??*/
      }
      else
      {
       for(i=0;i<counti;i++)
       {
        fun_str_ip(*str_ip,i); /*you have not declared anything called str_ip did you type it instead of str?*/
       }
       counti++;
      }
     }
     printf("You have entered %d inputs\n",counti);
    }
    char *fun_str_ip(char *str_ip[30][100],int num)
    {
     printf("Enter Input Number %d :",num);
     scanf("%s",&str_ip[30][num]);
    }

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,673
    I suppose the ultimate lazy way is to count newlines in your input until you finally match the "end" string. This way can actually be extended to go on counting for quite a long time. I'm tempted to show you what I did after you get it to work.

    There is no need to actually compare a zero character. The user won't be entering one - that is an implementation detail.

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    2
    Quote Originally Posted by theju112 View Post
    there are many issues to be addressed here..but i think that you have to learn a bit more on 2d arrays. and also on pointers..

    so ill paste your code and give you some hints as comments next to them so that you can try to understand mistakes..

    i have a ready version working, but i dont wont to give it away freely..but ill try to get you there...

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    
    char *str[30][100]={'\0'}; 
    /* what are you tring to declare above? i guess that you want to declare a 2d array which
    can store strings(one word in each row) but why have you used a pointer??
    all you need is char str[30][100] which means you have now declared an array which can store 30 
    words, each of which can have 100 characters*/
    int n=0,i=0,counti=0,flag=0;
    
    char *fun_str_ip(char *str_ip[30][100],int num);
    int main()
    {
     printf("Start Entering Inputs :");
     while(!flag)
     {
     /*in the if statemen below,what are you actually trying to compare? */
     /*i mean you have not taken any input string yet so you cant compare yet*/
     /*also *str? what does that mean?*/    
      if(strcmp(*str,"end")==0) 
      {
       flag=1;
       break; /*if your above condiiton is met, then the loop will bw exited so what is the need of
       the condition check inside while??*/
      }
      else
      {
       for(i=0;i<counti;i++)
       {
        fun_str_ip(*str_ip,i); /*you have not declared anything called str_ip did you type it instead of str?*/
       }
       counti++;
      }
     }
     printf("You have entered %d inputs\n",counti);
    }
    char *fun_str_ip(char *str_ip[30][100],int num)
    {
     printf("Enter Input Number %d :",num);
     scanf("%s",&str_ip[30][num]);
    }
    Thanks for pointing out the errors theju112....I think I'll try 2 work this snippet out by myself....unless I throw my guard away....but I'm glad you already hav a working code ready...!!!
    I'll try to clear certain doubts of mine....In the order of your comments....
    1. was a desperate move....i'm neither a beginner nor a moderate programmer in C....i think i'm something in between and a bit confused one too....referring to string functions and operations online made me think that str[30][100] and *str[30][100] work in a similar way....i'd be really happy if you share some links of specific programs in this context (strings,arrays and pointers) here....also if you can recommend any good books to which i can refer to....thanks in advance.....
    2. and 3. the intend was to see if the 2D array held any string "end" by comparing str and the string "end"....if true, then exit the loop coz we already hav the exit condition....the prime motive was to establish "end" as the exit condition....does checking in the earlier part of while loop affect the motive...???i mean before taking inputs....did that to play safe....
    the next for loop was implemented for the 2D array concept....but i still have the feeling that the way i'm playing with counti is wrong....i need this variable to count the number of inputs....i thought i could use the same for getting the inputs by placing in the loop condition....please help me figure this out....
    4. oops....it was just a typo....i meant fun_str_ip(str,i)....sorry for the confusion....i actually kept on changing the same code in the same file over and over again....this is a result of that.....
    is it okay if i pass the string variable str[30][100] as str...???will that make a difference...???
    thanks in advance....
    Last edited by skrowten_hermit; 09-26-2012 at 11:24 AM. Reason: invalid text

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    91
    this site has one of the best tutorials you can get online..reading the tutorials on arrays strings and pointers will help..
    one thing i would recommend is to make sure that you have the exact idea of what you should do to solve the problem..
    then you can write in simple English the steps you will perform and just convert that into code..this might not sound good and you might want to jump into coding straight away..but trust me try to have the exact idea before you start coding..

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,673
    referring to string functions and operations online made me think that str[30][100] and *str[30][100] work in a similar way....i'd be really happy if you share some links of specific programs in this context (strings,arrays and pointers) here....also if you can recommend any good books to which i can refer to....thanks in advance.....
    The syntax for arrays works as follows:
    Code:
    element-type array-name[array-size];
    If the element type is a pointer type. that means that each array cell from [0] to [array-size - 1] can point to an address and be dereferenced with the * operator, and each array cell is managed like a pointer would be. This usually comes up in two places. One is when you have to store hard coded strings. Many times, these strings are different lengths so it is easier to use const char *strings[SIZE];
    The const char * element type here is meant as a convenience, since the array is not going to be edited and the lengths of individual strings can be computed.* The other time this comes up involves dynamic memory, where memory is retrieved from one of the malloc/calloc/realloc functions first and then stored into this array. If you need help with strings in particular, we have a tutorial. C Strings - Cprogramming.com

    Even when you do need the flexibility afforded by arrays of this type, it can be prudent to start off with the non-pointer type, and then modify working code to use pointers, especially since you have only a little experience with C.

    2. and 3. the intend was to see if the 2D array held any string "end" by comparing str and the string "end"....if true, then exit the loop coz we already hav the exit condition....the prime motive was to establish "end" as the exit condition....does checking in the earlier part of while loop affect the motive...???i mean before taking inputs....did that to play safe....
    Comparing uninitialized memory is not playing it safe. The comparison is an example of undefined behavior in your program and can have unpredictable results. You should take care to not do anything with variables that do not contain a value yet.
    the next for loop was implemented for the 2D array concept....but i still have the feeling that the way i'm playing with counti is wrong....i need this variable to count the number of inputs....i thought i could use the same for getting the inputs by placing in the loop condition....please help me figure this out....
    I do not see why not to just use the same 1-D array when you are doing this part with the counting. When I wrote post #3, you can do that with a 1-D array. If a 2-D array (i.e. matrix) is required by the assignment, you can simply copy the 1-D array into one of the rows of the matrix.

    My preference is:
    Code:
    char input[SIZE];
    /* get input */
    matrix[i][0] = '\0';
    strncat( matrix[i], input, SIZE - 2);
    *The length of a string actually always needs to be computed, because a C string is defined as an arbitrary sequence of characters terminated by \0.
    Last edited by whiteflags; 09-26-2012 at 12:09 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading an Unknown Number of Inputs
    By Aeias in forum C++ Programming
    Replies: 13
    Last Post: 02-25-2012, 07:27 AM
  2. Replies: 7
    Last Post: 12-04-2011, 09:43 PM
  3. converting two keypad inputs into one decimal number
    By volkvanmyn25 in forum C Programming
    Replies: 1
    Last Post: 09-16-2010, 09:09 AM
  4. Counting Numbers in Array, not counting last number!
    By metaljester in forum C++ Programming
    Replies: 11
    Last Post: 10-18-2006, 11:25 AM
  5. Question about limiting the number of user inputs in C
    By chobibo in forum C Programming
    Replies: 15
    Last Post: 08-30-2006, 12:37 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21