Reading strings input by the user...

This is a discussion on Reading strings input by the user... within the C Programming forums, part of the General Programming Boards category; I've done a search and came up with nothing helpful.... I have been stuck at this for a few day's ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    46

    Reading strings input by the user...

    I've done a search and came up with nothing helpful....

    I have been stuck at this for a few day's now , I need too write a program that will use scanf() to read in seven strings input by the user. The program should print the seven strings as a list , then sort them alphabettically and print a new list.

    I need to use the function strcmp() , & the preprocessing directive #define N_STRINGS 7 to assist in the sorting of the strings

    I have tried using for loops & while loops with scanf() in reading in the strings , & then printing it out but im all ready stuck


    At this point im just trying to get my code to read 7 strings input by the user & just print them out again.
    This is what i've come up with sofar but it's not working at all & very long .

    Code:
    #include <stdio.h>
    #include <string.h>
    #define N_STRINGS 7
    #define SIZE 100
    #define SIZED 700
    
    void main(int argc)
    {
      char c;
      int i;
      int count = 0;
      char array1[SIZE];
      char array2[SIZE];
      char array3[SIZE];
      char array4[SIZE];
      char array5[SIZE];
      char array6[SIZE];
      char array7[SIZE];
      
      printf("Type in 7 strings...\n\n");
      if (argc < SIZED){
    	  while ((c = getchar()) != '\n'){
    	      scanf("%s", array1);
    	      count++;
    	  }
    	   array1[count - 1] = '\0';
    	   count = 0;
    	   while ((c = getchar()) != '\n'){
    		   scanf("%s", array2);
    		   count++;
    	   }
    	   array2[count - 1] = '\0';
    	   count = 0;
    	   while ((c = getchar()) != '\n'){
    		   scanf("%s", array3);
    		   count++;
    	   }
    	   array3[count - 1] = '\0';
    	   count = 0;
    	   while ((c = getchar()) != '\n'){
    		   scanf("%s", array4);
               count++;
    	   }	    
    	   array4[count - 1] = '\0';
    	   count = 0;
    	   while ((c = getchar()) != '\n'){
    		   scanf("%s", array5);
    		   count++;
    	   }
    	   array5[count - 1] = '\0';
    	   count = 0;
    	   while ((c = getchar()) != '\n'){
    		   scanf("%s", array6);
    		   count++;
    	   }
    	   array6[count - 1] = '\0';
    	   count = 0;
    	   while ((c = getchar()) != '\n'){
    		   scanf("%s", array7);
    		   count++;
    	   }
    	   array7[count - 1] = '\0';
      }
      else {
    	  printf("Type in 7 strings...\n");
      }
      printf("\n");
      printf("Printing the strings again ...\n");
      for (i = 0;(c = getchar()) != '\0';++i){
    	  printf("%s",array1[i]);
      }
      for (i = 0;(c = getchar()) != '\0';++i){
    	  printf("%s",array2[i]);
      }
      for (i = 0;(c = getchar()) != '\0';++i){
    	  printf("%s",array3[i]);
      }
      for (i = 0;(c = getchar()) != '\0';++i){
    	  printf("%s",array4[i]);
      }
      for (i = 0;(c = getchar()) != '\0';++i){
    	  printf("%s",array5[i]);
      }
      for (i = 0;(c = getchar()) != '\0';++i){
    	  printf("%s",array6[i]);
      }
      for (i = 0;(c = getchar()) != '\0';++i){
    	  printf("%s",array7[i]);
      }
      
    
    }

    Thanks a lot for any help
    Btw I must use scanf() in reading in the strings it says so in my textbook .

  2. #2
    Compulsive Liar Robc's Avatar
    Join Date
    Jul 2004
    Posts
    149
    >void main(int argc)
    This is way wrong. First, main returns int. Second, the only two portable definitions take 0 and 2 arguments. Third, you aren't using argc correctly.

    It's better to leave the implementation to the nitty gritty details at this stage. Study the following and incorporate what you learn into your own code. You'll probably need to replace qsort with your own custom sorting routine though. Most teachers don't like you to use it.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define N_STRINGS 7
    
    int cmp(const void *a, const void *b);
    
    int main(void)
    {
      /* Declare an array of strings */
      char strings[N_STRINGS][BUFSIZ];
      int  i;
    
      /* Read N_STRINGS strings */
      for (i = 0; i < N_STRINGS; i++) {
        /* A prompt is always helpful to users */
        printf("Enter string %%%d: ", i + 1);
        /* Unless you print a '\n' in the prompt, you need this */
        fflush(stdout);
        /* ALWAYS check scanf for success */
        if (scanf("%s", strings[i]) != 1) {
          fprintf(stderr, "Error reading input\n");
          return EXIT_FAILURE;
        }
      }
      /* Sort the array using cmp as the comparison routine */
      qsort(strings, N_STRINGS, sizeof strings[0], cmp);
      /* Print out the sorted result */
      for (i = 0; i < N_STRINGS; i++) {
        printf("%s\n", strings[i]);
      }
    
      return EXIT_SUCCESS;
    }
    
    int cmp(const void *a, const void *b)
    {
      return strcmp((const char *)a, (const char *)b);
    }

  3. #3
    Helper
    Join Date
    Jun 2004
    Posts
    255
    Quote Originally Posted by Cmuppet
    This is what i've come up with sofar but it's not working at all & very long .
    You should learn how to configure your compiler to say more about your coding :
    Code:
    Compilateur: Default compiler
    Building Makefile: "D:\Dev-Cpp\Makefile.win"
    Exécution de  make...
    make.exe -f "D:\Dev-Cpp\Makefile.win" all
    gcc.exe -D__DEBUG__ -c main.c -o main.o -I"C:/DEV-CPP/include" -W -Wall -O2   -g3
    
    main.c:8: warning: return type of `main' is not `int'
    main.c:8: warning: `main' takes only zero or two arguments
    
    main.c: In function `main':
    main.c:70: warning: format argument is not a pointer (arg 2)
    main.c:73: warning: format argument is not a pointer (arg 2)
    main.c:76: warning: format argument is not a pointer (arg 2)
    main.c:79: warning: format argument is not a pointer (arg 2)
    main.c:82: warning: format argument is not a pointer (arg 2)
    main.c:85: warning: format argument is not a pointer (arg 2)
    main.c:88: warning: format argument is not a pointer (arg 2)
    
    gcc.exe -D__DEBUG__ main.o  -o "Projet1.exe" -L"C:/DEV-CPP/lib" C:/Dev-Cpp/lib/libws2_32.a -g3 
    
    Exécution terminée
    Compilation OK
    Emmanuel Delahaye

    "C is a sharp tool"

  4. #4
    Registered User
    Join Date
    Jul 2004
    Posts
    46
    Thanks for the replies guys. I was assuming the user entering a string though could enter more than one word at a time & the string would end only when the user hits enter ??

  5. #5
    Compulsive Liar Robc's Avatar
    Join Date
    Jul 2004
    Posts
    149
    >I was assuming the user entering a string though could enter more than one word at a
    >time & the string would end only when the user hits enter ??
    That's not how scanf works. Parsing only goes to the next whitespace with the %s specifier. That means that words will be split up. Fortunately, this is easy to fix, but unfortunately, it's difficult to get right.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define N_STRINGS 7
    
    int cmp(const void *a, const void *b);
    
    int main(void)
    {
      /* Declare an array of strings */
      char strings[N_STRINGS][1024];
      int  i;
    
      /* Read N_STRINGS strings */
      for (i = 0; i < N_STRINGS; i++) {
        /* A prompt is always helpful to users */
        printf("Enter string %%%d: ", i + 1);
        /* Unless you print a '\n' in the prompt, you need this */
        fflush(stdout);
        /* ALWAYS check scanf for success */
        /* The [^\n] specifier says to read until '\n' is found */
        if (scanf("%1024[^\n]", strings[i]) != 1) {
          fprintf(stderr, "Error reading input\n");
          return EXIT_FAILURE;
        }
        /* scanf leaves '\n' in the stream, so we have to remove it */
        getchar();
      }
      /* Sort the array using cmp as the comparison routine */
      qsort(strings, N_STRINGS, sizeof strings[0], cmp);
      /* Print out the sorted result */
      for (i = 0; i < N_STRINGS; i++) {
        printf("%s\n", strings[i]);
      }
    
      return EXIT_SUCCESS;
    }
    
    int cmp(const void *a, const void *b)
    {
      return strcmp((const char *)a, (const char *)b);
    }
    Even that's not perfect, but it'll be more than enough for your purposes. In general, it's a bad idea to use scanf for string data. fgets is better suited to that task.

  6. #6
    Registered User
    Join Date
    Jul 2004
    Posts
    46

    Thumbs up

    Thanks again for your help Robc

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User
    Join Date
    Jul 2004
    Posts
    46
    Oke i've come up with this code , & i've been paining my brain trying to figure out why it's not working. Im guessing it's an array overflow somewhere somehow ???

    Thanks again for helping !!

    Code:
    #include <stdio.h>
    #include <string.h>
    #define N_STRINGS 7
    #define SIZE 15
    
    void main(void)
    {
      int i,j,compare;
      char arrays[N_STRINGS][SIZE];
      char sortedarray[N_STRINGS][SIZE];
    
      for (i = 0;i < N_STRINGS;++i){
    	  printf("Enter string %d : ",i);
          scanf("%s",arrays[i]);
      }
      printf("Printing the strings again ...\n");
      for (i = 0;i < N_STRINGS;++i){
    	  printf("\n%s\n",arrays[i]);
      }
      printf("Sorting the strings...\n");
    
      for (i = 0;i < N_STRINGS;++i){
    	  for (j = 0;j < SIZE;++j){
    	    compare = strcmp(&arrays[i][j],&arrays[i + 1][j]);
    	    if ((compare == -1)||(compare == 0)){
    		  sortedarray[i][j] == arrays[i][j];
    		  sortedarray[i + 1][j] == arrays[i + 1][j];
    	    }
    	    else{
                      sortedarray[i][j] == arrays[i + 1][j];
                      sortedarray[i + 1][j] == arrays[i][j];
    	    }
    	  }
    	 
      }
      printf("Printing the strings again sorted this time...\n");
      for (i = 0;i < N_STRINGS;++i){
    	  for (j = 0;j < SIZE;++j)
    		  printf("%s",sortedarray[i][j]);
    	  
      }
    }
    
    int strcmp(const char *s1,const char *s2){
         return (*s1,*s2);
    }
    Last edited by Cmuppet; 07-21-2004 at 04:51 AM.

  9. #9
    Helper
    Join Date
    Jun 2004
    Posts
    255
    Quote Originally Posted by Cmuppet
    Oke i've come up with this code , & i've been paining my brain trying to figure out why it's not working. Im guessing it's an array overflow somewhere somehow ???

    Thanks again for helping !!
    Starts weirdly...
    Code:
    Compiling MAIN.C:
    Error MAIN.C 7: main must have a return type of int
    Warning MAIN.C 31: Code has no effect
    Warning MAIN.C 32: Code has no effect
    Warning MAIN.C 36: Code has no effect
    Warning MAIN.C 37: Code has no effect
    Warning MAIN.C 54: Parameter 's1' is never used
    Code:
    	    if ((compare == -1)||(compare == 0)){
    		  sortedarray[i][j] == arrays[i][j];
    		  sortedarray[i + 1][j] == arrays[i + 1][j];
    	    }
    	    else{
                      sortedarray[i][j] == arrays[i + 1][j];
                      sortedarray[i + 1][j] == arrays[i][j];
    	    }
    My compiler is correct. These four '==' are weird ! You meant '=', of course...
    Code:
    int strcmp(const char *s1,const char *s2){
         return (*s1,*s2);
    }
    Huh! Why in the world are you attempting to redefine strcmp() ? Just drop these three lines.

    I don't pretend that this will fix all your bugs, but it's a start.
    Emmanuel Delahaye

    "C is a sharp tool"

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Emmanuel Delahaye
    Huh! Why in the world are you attempting to redefine strcmp() ? Just drop these three lines.
    I'm not saying I'd recommend it, but you're allowed to do so if you so desire...

    [edit]
    Although, their implementation ... leaves much to be desired.
    After a second, it's probably a bad idea to include "string.h" in attempting to do so...
    [/edit]

    Quzah.
    Last edited by quzah; 07-21-2004 at 05:34 AM.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Jul 2004
    Posts
    46
    oh dear my brain must have been fried lol , using == instead of =
    thanks for pointing that out , i overlooked it completly

  12. #12
    Registered User
    Join Date
    Jul 2004
    Posts
    46
    Im still getting an error message

  13. #13
    Registered User
    Join Date
    Jul 2004
    Posts
    46
    Thanks btw for pointing these things out to me

  14. #14
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    Well post your latest code and actual error messages then
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading console input (character mode applications)
    By maxhavoc in forum Windows Programming
    Replies: 12
    Last Post: 11-27-2005, 04:13 AM
  2. Replies: 4
    Last Post: 04-21-2004, 05:18 PM
  3. comparing user input
    By lambs4 in forum C Programming
    Replies: 5
    Last Post: 12-15-2002, 10:28 AM
  4. Am I going insane? How do you input strings?
    By GrNxxDaY in forum C++ Programming
    Replies: 16
    Last Post: 07-27-2002, 10:53 PM
  5. Format User Input When using strings
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 03-08-2002, 03:59 AM

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