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

  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
    28,413
    Instead of having words be an array of pointers to char, have it be an array of arrays of char.
    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

  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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

  5. #5
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    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
    28,413
    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.
    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. 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, 01:18 AM
  5. Saving string to file problem
    By PJYelton in forum C++ Programming
    Replies: 4
    Last Post: 01-15-2003, 09:46 AM