-1

NOTE: This is for homework, I just want to know why i get the messed up print, not the 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 would you like to select? (1-3)\nDo not select one you have selected prior!\t");
        scanf("%d", &band_number);

        if (band_number = 1)
        {
            printf("What color would you like to assign here?\t");
            scanf("%s", &band_color[0]);
            x++;
        }
        else if (band_number = 2)
        {
            printf("What color would you like to assign here?\t");
            scanf("%s", &band_color[1]);
            x++;
        }
        else if (band_number = 3)
        {
            printf("What color would you like to assign here?\t");
            scanf("%s", &band_color[2]);
            x++;
        }

    }
    return (*band_color);

}

So when I run it, I get no errors or warnings, but what I do get is the last color I enter. For example, I enter green, blue, yellow, in that order. I will get yellow printed back. Regardless of what order I use for the numbers, I always get back the very last color entered.

WhozCraig
  • 65,258
  • 11
  • 75
  • 141
  • Add `fflush(stdout);` after `printf` calls as you don't have new lines in your `printf`. – ouah Jul 08 '15 at 20:02
  • No warnings? What about the warnings *"uninitialised local variable used"* for `power` and `resist` and `band_number`? – Weather Vane Jul 08 '15 at 20:07
  • 1
    Add to that improper usage of assignment operator vs equivalence comparison (`=` vs. `==`), I pasted this into Xcode and it immediately flagged 7 warnings. – WhozCraig Jul 08 '15 at 20:08
  • Nope, no warnings. Everything is working fine aside from the print statement kicking "the last color entered". I'm using Code Blocks 13.12 if it matters – Tommy Buzaki Jul 08 '15 at 20:12
  • As @WhozCraig said: `if (band_number = 1)` and so on. – Weather Vane Jul 08 '15 at 20:14
  • Even so, its not so much the warnings I'm worried about right now. I care about why band_color prints the last thing I input – Tommy Buzaki Jul 08 '15 at 20:15
  • 1
    If you don't care for the warnings first, when the program behaves unexpectedly, you won't get far. – Weather Vane Jul 08 '15 at 20:16
  • I changed the "=" to "==" and that produced a print of "gbgreen" when entering green, blue, green. – Tommy Buzaki Jul 08 '15 at 20:17
  • 1
    I really wonder where to start with all the faults in these few lines of code. So just a summary: If you a following a C-book or a tutorial: bin it! It is either not good, or not the right one for you. After that, get a new book/tutorial. No offense, but you are obviously missing quite some basics. – too honest for this site Jul 08 '15 at 20:18
  • **The =**? there were at least 3 of them. – Weather Vane Jul 08 '15 at 20:18
  • You cannot scanf a string into a char, (safely), unless it happens to be a string of zero length. – Martin James Jul 08 '15 at 21:41
  • I solved it, esm hit the nail on the head. I did not know you could statically allocate how large each array element could be. I had only used 2d arrays prior and had not even thought about 3d ones. – Tommy Buzaki Jul 09 '15 at 02:05

2 Answers2

4

You have two major problems with your code:

First:

if (band_number = 1) {
  ...
}

= is the assignment operator == is the equal to operator. Since band_number = 1 always evaluates to 1 which is true that branch is always taken.

Second:

char    band_color[3];

and then later:

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

You have not declared enough space and you stomping over your previous values. Furthermore you want to statically allocate an array of strings you need to do something more like the 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 would you like to select? (1-3)\nDo not select one you have selected prior!\t");
      scanf("%d", &band_number);
      printf("What color would you like to assign here?\t");

      scanf("%s", band_color[band_number - 1]);
      x++;
    }

}

I have greatly simplified your code logic, as you had a lot duplication. The key point though is that char band_color[3][20] will declare enough space for 3 strings that are 20 characters long (including the terminating character). The function prototype to get_band has changed to accommodate the new type of band_color.

The code simplification actually removed the if statements by using the observation that the number inputted by the user was always 1 greater than the array index. You can then index the array with that number minus 1, i.e. band_color[band_number - 1].

Note this is susceptible to a buffer overflow if you type in a string that is two long when prompted. It is not high quality code, just trying to demonstrate your mistake

If might find this link helpful with regards to getting user input.

Also a good link to why using scanf is bad for user input.

Community
  • 1
  • 1
missimer
  • 4,022
  • 1
  • 19
  • 33
0

There a few things wrong with your program. There is a lot of bad redundant passing of variables needlessly. You can either declare band_color as a global variable or declare it in get_band function. Same thing with band_number. There is no reason to declare it in get_resistance just to pass it to get_band. Just declare it in get_band as that is the only place it is used. You can then return a pointer to the band_color array to use for the printf. In your if statements in get_band you mis-wrote the evaluators. You used = where they should be ==.

if (band_number == 1)

This is causing your code to allows go into the first if and assign a variable only to the [0] array. Your array only needs to be [2] as that gives you 3 entries. Also you need to declare a length for the strings in your array with a second set of braces.

char band_color[2][10];
bbishopca
  • 296
  • 3
  • 13