c - Cannot get input from user to work in place of manual strings entered in code -
I'm trying to run a program that basically takes an input string and it's through some code Sends and lists it according to the linked spectrum I thought how to do this by using manual (actual code) text input, but when I'm trying to take input from the user, I do not work it can.
#include & lt; Stdlib.h & gt; # Include & lt; Stdio.h & gt; #include & lt; String.h & gt; Structure list {char * value; Structure list * next; }; Compare Int (structure list * one, structure list * 2) {return SRCMP (one-> values, two-> value); } Add zero (structure list ** pp, four * value, int (* CMP) (structure listlist * l, structure listlist * r)) {struct list * new; New = maulok (size (* new)); New-> value = value; (; * PP! = Faucet; pp = & amp; nbsp; - PP) - & gt; Next) {if (CPP (* PP, new)> break;); } New-> gt; Next = * pp; * PP = new; } Zero performance (structure list * ptr) {for (; ptr! = NULL; ptr = ptr- & gt; next) {printf ("% s \ n", ptr-> values); }} Int main (zero) {struct list = root = null; Four strings; Printf ("Alphabetically enter a string to sort and display with a character count:"); String = meet (); While (string! = 10) {add (& root, & string, compare); String = meet (); } Display (route); Return 0; }
Seeing in the main function, I'm pretty sure that it has something to do with getchar and the fact that it reads the characters as intus, but I do not understand How to fix this, output is just a bunch of empty lines, but when the loop is removed and the string is primarily recorded as below, then it works fine? Why is it like this?
int main (zero) {struct list = root = zero; Four strings; Printf ("Alphabetically enter a string to sort and display with a character count:"); Addition (and root, "T", compare); Add (and root, "h", compare); Plus (and root, "i", compare); Addition (and root, "s", compare); Plus (and root, "m", compare); Addition (and root, "y", compare); Addition (and root, "T", compare); Addition (and root, "e", compare); Addition (and root, "X", compare); Addition (and root, "T", compare); Display (route); Return 0; }
Now the output is I h m m t t t y y
Which is correct, can someone help me?
When you type "t"
in the example, you get the address This address of an array is different for each value that you send to add
. However, the address of the string does not change, and whenever you make a call, you can enter new-> Value is set to
add
instead of new-> value = value
, new-> gt; Try the value = * value
(and all necessary changes needed).
There are not many changes needed to be done, note that ending after a string of input is not very good behavior. A good practice would be to write a destructor who tear down the string after the first line and then if there is no argument given in the next type, then another good practice will be to sort the arguments if no one has passed, then any There is no argument also.
#include & lt; Ctype.h & gt; # Include & lt; Stdlib.h & gt; # Include & lt; Stdio.h & gt; #include & lt; String.h & gt; Structure list {char value; / * Change * / structure list * next; }; TypeFIFF (* comparison) (structure list * one, structure list * 2); Compare Int (structure list * one, structure list * 2) {return tolower (one-> value) & gt; Tiller (two->; value); / * Change * zeros * xmalloc (size_t s) {zero * v = malloc (s); If (v == faucet) {mirror ("molok"); Exit (EXIT_FAILURE); } Return V; } Zero Joint (structure list ** pp, four values, comparison CMP) / * change * / {structure list * new; New = xmalloc (sizeof * new); / * Check for errors * / new- & gt; Value = value; Change the type of value for * * * *; (; * Pp! = NULL; pp = & amp; nbsp; (* pp) - & gt; Next) {if (cmp (* pp, new)> 0) break; } New-> gt; Next = * pp; * PP = new; } Zero performance (structure list * ptr) {for (; ptr! = NULL; ptr = ptr- & gt; next) {putchar (ptr-> value); / * Change * / fever ('\ n'); } Int main (zero) {struct list = root = zero; Four strings; While ((string = getchar ()) = '\ n') {/ * Alternate changes (1) * / add (and root, string, comparison); } Display (route); Return 0; } / * * (1) Readability of future maintenance, * Portability and discretion are required to use "\ n" instead of 10. * * Writing () is cleaner only once * /
Comments
Post a Comment