![]() |
|
#1
|
||||
|
||||
Strings and arrays in CTask: 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:
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
|
||||
|
||||
|
Hi Crystalattice. You do need a 2d array. When you define:
CPP / C++ / C Code:
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:
Beyond that your code needs to be changed to use either a ragged array or a 2d char array. HTH, d |
|
#3
|
||||
|
||||
Still have problemsSo, 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
|
||||
|
||||
|
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:
__________________
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
|
||||
|
||||
|
Quote:
CPP / C++ / C Code:
CPP / C++ / C Code:
CPP / C++ / C Code:
__________________
Age is unimportant -- except in cheese |
|
#6
|
||||
|
||||
Sorry 'bout the waitJust 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 GIDBlog
Toyota - 2008 September Promotion by Nihal
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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