I Cannot GET THIS TO WORK.. (Saving A STRING Into An Array...)

This is a discussion on I Cannot GET THIS TO WORK.. (Saving A STRING Into An Array...) within the C Programming forums, part of the General Programming Boards category; All I want to do is ask the user to enter words, then I bubble sort them alphabetically, then print ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    9

    I Cannot GET THIS TO WORK.. (Saving A STRING Into An Array...)

    All I want to do is ask the user to enter words, then I bubble sort them alphabetically, then print them back out. THE PROBLEM IS.. When I try to store "str" into the words array, idk what's happening... When I print them out, it's only saving the LAST word I typed into every array location..


    Code:
    
    
    
    #include "stdafx.h"
    #include <stdio.h>
    #include <string.h>
    
    
    #define MAXLENGTH 15
    #define MAXWORDS 3
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    
    
    char str[80];
    char *words[25];
    char *hold;
    int j;
    int i;
    int wordcount=0;
    
    
    while (wordcount<MAXWORDS){
    
    
    	  printf("Enter a word: ");
    		 fgets(str, MAXLENGTH, stdin);
    
    
      /* remove newline, if present */
      i = strlen(str)-1;
      if( str[i] == '\n') 
          str[i] = '\0';
    
    
      printf("%s\n",str);
    
    
      words[wordcount]=str;
    
    
    	wordcount++;
    
    
    }
    
    
    printf("%d words.\n",wordcount);
    
    
    for(i=1;i<wordcount;++i)
    	for(j=wordcount-1;j>=i;--j)
    		if (strcmp(words[j-1],words[j])>0){
    		hold=words[j-1];
    		words[j-1]=words[j];
    		words[j]=hold;
    		}
    
    
    
    
    for(i=0;i<wordcount;i++)
    	printf("%s \n",words[i]);
    
    
    	return 0;
    }
    I realize "char*s are not strings. They are pointers"

    But how do I fix this?

    All I want to do is ask the user to enter strings 1 line at a time, and have them saved in an array.

    Then sort the strings alphabetically in the array.

    Then print it.

    That's all!!.. I have the sorting working, I just don't have the storing the strings from the user into the array!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Instead of having words be an array of pointers to char, have it be an array of arrays of char.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    9
    Can you explain a little more? Do you mean have a multi-dimensional array? Could you please write a small example on it.. I'm new to C.

    EDIT: Is it possible you'd like to join me on a TeamViewer session and help me fix this? I'm sure it will only take about 5 minutes.. :S I'm getting desperate at this point..
    Last edited by Lej1; 03-27-2012 at 09:10 PM.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    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.

  5. #5
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    612
    I always thought char array[10][10]; was the same as char* array[10]; Are not the brackets just a dereference such as the '*'?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Quote Originally Posted by camel-man
    I always thought char array[10][10]; was the same as char* array[10];
    Not at all: an array of 10 arrays of 10 chars is not the same as an array of 10 pointers to char. They are not even implicitly convertible since the former is converted a pointer to an array of 10 chars, whereas the latter is converted to a pointer to a pointer to char.

    Quote Originally Posted by camel-man
    Are not the brackets just a dereference such as the '*'?
    We're examining declarations, not expressions.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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. saving entire input into 1 string?
    By rodrigorules in forum C++ Programming
    Replies: 3
    Last Post: 04-09-2010, 02:44 PM
  2. Tutorial/Guide on saving www source to a string
    By jimzy in forum C++ Programming
    Replies: 3
    Last Post: 06-24-2007, 06:41 PM
  3. How do i get array work for string?
    By skydancer in forum C Programming
    Replies: 10
    Last Post: 07-31-2006, 11:18 AM
  4. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 12:18 AM
  5. Saving string to file problem
    By PJYelton in forum C++ Programming
    Replies: 4
    Last Post: 01-15-2003, 08:46 AM

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