GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 18-Apr-2004, 14:21
crystalattice's Avatar
crystalattice crystalattice is offline
Flame War Instigator
 
Join Date: Apr 2004
Location: San Diego
Posts: 1,543
crystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nice
Angry

Strings and arrays in C


Task: write an interactive program the reads in 7 strings using <i>scanf()</i>, print them as a list, then sort them alphabetically and print a new list.

I am thinking of using an array to store the strings but it doesn't seem to be working. I think it requires a 2d array because I want to store multiple words, but even when I test it w/ just a single array, I don't get an output.

Here's my test code:
CPP / C++ / C Code:
#include <stdio.h>
#include <string.h>

#define N_STRINGS 7

int main()
{
	
	const char array[N_STRINGS] = {0};
	int i;
	
	printf("Please enter 7 strings.\n");
	for (i=0; array[i]<N_STRINGS; ++i)
		scanf("%s", array);
	for (i=0; array[i]<N_STRINGS; ++i)
		printf("%s", array);
	return 0;
}

When run, this code will accept an input, but doesn't print an output. In my debugger, when I step through I can see a single string being written to the array but all array indexes after the word are nulled out, e.g. type in "one" and "two" and the array is written as :

array[0]='o'
array[1]='n'
array[2]='e'
array[3] through array[6]='\0'

Now, when the <i>printf()</i> is run, nothing is printed, even though there is something in the array.

I've tried moving the <i>i</i> counter from the for loop to the body, as in scanf("%s", array[i]) and the for loop to just "i < N_STRINGS" but that doesn't help.

Unfortunantly, my textbooks don't give similar examples to this and I can't find anything specific on the 'net. About the only thing I've found is that <i>scanf()</i> can be used to input a string to an 1d array and <i>printf()</i> will print this array. I've tested this seperately so I know this works, but I can't seem to get it to work w/ this problem.

Hope I don't sound like a clueless noob.

Any thoughts?
__________________
Common Sense v2.0-Striving to make the world a little bit smarter.
  #2  
Old 18-Apr-2004, 14:48
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hi Crystalattice. You do need a 2d array. When you define:
CPP / C++ / C Code:
	
	const char array[N_STRINGS] = {0};

You are simply getting an array of characters which is one string. In this case, it is being initialized with Nulls, so every position will be filled with 0 (Null).

I ran your code and got exactly what I would expect. A single input prompt and the printout of what I input. Perhaps, adding a newline will help you see your output, such as:
CPP / C++ / C Code:
		printf("\n%s\n", array);

Beyond that your code needs to be changed to use either a ragged array or a 2d char array.

HTH,
d
  #3  
Old 27-Apr-2004, 17:30
crystalattice's Avatar
crystalattice crystalattice is offline
Flame War Instigator
 
Join Date: Apr 2004
Location: San Diego
Posts: 1,543
crystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nice

Still have problems


So, I realize that I need a 2d array, but how do I fill it using scanf()? My books don't cover it very well; they mostly talk about getchar(). Can I use a loop to fill it like if I used getchar()? I tried it both w/ a for loop and while loop but it didn't work. I would think you could use '\0' for the loop-stop condition, since it's the last entry in an array, but it doesn't work.

How else can you fill an array w/ scanf()?

Thanks for the help.
__________________
Common Sense v2.0-Striving to make the world a little bit smarter.
  #4  
Old 27-Apr-2004, 19:53
Max Payne's Avatar
Max Payne Max Payne is offline
Regular Member
 
Join Date: Apr 2004
Location: 3° 08 North 101° 42 East
Posts: 332
Max Payne is a jewel in the roughMax Payne is a jewel in the roughMax Payne is a jewel in the rough
First,Your for loop doesn't seem quite right.

for (i=0; array[i]<N_STRINGS; ++i)

the condition array[i]<N_STRINGS should be change to i<N_STRINGS,

You need to clear terminating newline ('\n' or ENTER ) from the input stream before calling scanf() again.
So to clear the buffer use the fflush(stdin); // in c
or
cin.clear(); //clears the buffer
cin.sync(); // syncronize it

so, with all the changes, here is the new code.

CPP / C++ / C Code:
#include <stdio.h>
#include <string.h>

#define N_STRINGS 7

int main()
{
  char array[N_STRINGS][10];

  int i;
  printf("Please enter 7 strings.\n");
  for (i=0; i<N_STRINGS; ++i)
  {
    scanf("%s",array[i]);
	fflush(stdin);
  }
  for (i=0; i<N_STRINGS; ++i)
    printf("%s\n",array[i]);
  return 0;
}
__________________
When you say "I wrote a program that crashed Windows," people just stare at you blankly and say "Hey, I got those with the system, for free." Linus Torvalds
  #5  
Old 28-Apr-2004, 00:28
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by Max Payne
So to clear the buffer use the fflush(stdin); // in c
No you don't. The standard states that fflush() is not defined for input streams. It may or may not work -- different compilers do different things. Stay away from fflush(stdin);

CPP / C++ / C Code:
scanf("%s",array[i]);
is also dangerous. Execute this program:
CPP / C++ / C Code:
#include <stdio.h>
int main()
{
    char tmp[8];
    scanf("%s", tmp);
    printf("%s\n", tmp);
    return 0;
}
and enter 20 characters for tmp. You are able to easily overwrite memory. My test crashed! So since you are simpy entering a character string, a much safer way is to use fgets:
CPP / C++ / C Code:
rc = fgets(array[i], 10, stdin);
__________________

Age is unimportant -- except in cheese
  #6  
Old 11-May-2004, 19:01
crystalattice's Avatar
crystalattice crystalattice is offline
Flame War Instigator
 
Join Date: Apr 2004
Location: San Diego
Posts: 1,543
crystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nice
Talking

Sorry 'bout the wait


Just wanted to say thanks for the help. My program works now, thanks to everyone. As a matter of fact, I'm still waiting for my school to get back to me about how to work this problem, so this forum is way better when I need help.

Thanks again.
__________________
Common Sense v2.0-Striving to make the world a little bit smarter.
 
 

Recent GIDBlogToyota - 2008 September Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Extra null element in an array samtediou MySQL / PHP Forum 2 11-Dec-2003 11:52

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 16:04.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.