printf - String not printing fully C -


note: homework, want know why messed print, not finished program.

#include    <stdio.h>  char    get_band( int band_number, char band_color[] ); void    get_resistance( int resist, int power );  int main() {     int     resist;     int     power;     get_resistance(resist, power ); }  void get_resistance( int resist, int power ) {     int     band_number;     char    band_color[3];     char    color[3];       get_band( band_number, band_color );     printf("%s", band_color); } char get_band( int band_number, char band_color[] ) {     int     x;     x=0;     while (x < 3)     {         printf("which band select? (1-3)\ndo not select 1 have selected prior!\t");         scanf("%d", &band_number);          if (band_number = 1)         {             printf("what color assign here?\t");             scanf("%s", &band_color[0]);             x++;         }         else if (band_number = 2)         {             printf("what color assign here?\t");             scanf("%s", &band_color[1]);             x++;         }         else if (band_number = 3)         {             printf("what color assign here?\t");             scanf("%s", &band_color[2]);             x++;         }      }     return (*band_color);  } 

so when run it, no errors or warnings, last color enter. example, enter green, blue, yellow, in order. yellow printed back. regardless of order use numbers, last color entered.

you have 2 major problems code:

first:

if (band_number = 1) {   ... } 

= assignment operator == equal operator. since band_number = 1 evaluates 1 true branch taken.

second:

char    band_color[3]; 

and later:

scanf("%s", &band_color[0]); 

you have not declared enough space , stomping on previous values. furthermore want statically allocate array of strings need more following:

void get_resistance( int resist, int power ) {   int     band_number;   char    band_color[3][20];     get_band( band_number, band_color );   printf("%s\n", band_color[0]);   printf("%s\n", band_color[1]);   printf("%s\n", band_color[2]); }  void get_band( int band_number, char band_color[][20] ) {   int     x;   x=0;   while (x < 3)     {       printf("which band select? (1-3)\ndo not select 1 have selected prior!\t");       scanf("%d", &band_number);       printf("what color assign here?\t");        scanf("%s", band_color[band_number - 1]);       x++;     }  } 

i have simplified code logic, had lot duplication. key point though char band_color[3][20] declare enough space 3 strings 20 characters long (including terminating character). function prototype get_band has changed accommodate new type of band_color.

the code simplification removed if statements using observation number inputted user 1 greater array index. can index array number minus 1, i.e. band_color[band_number - 1].

note susceptible buffer overflow if type in string 2 long when prompted. not high quality code, trying demonstrate mistake

if might find this link helpful regards getting user input.

also link why using scanf bad user input.


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -