0

Ok, so i'm having a bit of trouble with managing my arrays / for loops (pretty new to C). I need to ask the user how many types of paint they want to enter, then take data three times for each paint, and output all the data at the end. I seem to be ok with taking all the data from the user, it's mainly outputting all the data at the end that i'm struggling with. I'm not looking for a quick solution to this specific problem, as I want to learn how the arrays / for loops work when outputting data (if that makes any sense).

#include <stdio.h>

int main(void)
{

    int amount, count;
    int result1, result2, result3;
    char paintname;

    printf("Please enter how many paints you want to compare:\n");
    scanf("%d", &amount);

    for (count = 1; count <= amount; count++)
    {
        printf("Please enter the name of paint number %d:\n", count);
        scanf("%s", &paintname);

        printf("Please enter the first result of paint number %d:\n", count);
        scanf("%d", &result1);
        printf("Please enter the second result of paint number %d:\n", count);
        scanf("%d", &result2);
        printf("Please enter the third result of paint number %d:\n", count);
        scanf("%d", &result3);
    }


    return 0;
}
  • First mistake very evident, you don't check the return value of `scanf()`. – Iharob Al Asimi Oct 29 '15 at 00:52
  • What have you tried? What are the problems you are encountering? I think you need to review your understanding of arrays as well, since you aren't actually using any in this code. – ajshort Oct 29 '15 at 01:11
  • What you need is an array of structs. Those are described in section 3 of chapter 6 in K&R 2nd edition. Since K&R is a little out of date, you may wish to consider any one of [these fine books](http://stackoverflow.com/a/562377/3386109). – user3386109 Oct 29 '15 at 01:54

2 Answers2

1

If you're looking for how to store all the results, you should use an array for every result (and name) that is large enough to hold all the user inputs. This array has a size that is dynamic (that is, it is decided at run-time when the user inputs it), so it should be allocated dynamically by the use of malloc()/calloc() and then free()'d later on.

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
  • i've had a look at what malloc, calloc, and free do and i get the general grasp of them, but we've not yet covered them in my course yet so I don't think im expected to use them yet. i've added an array to `paintname[101]` and added the for loop `for (i = 1; i < amount; i++) { printf("%s", paintname[i]); }` to try and print out each paintname separately, but there's definitely something im seriously going wrong with, i feel like only the first paintname is being stored. Thanks for your help! – Charley1601 Oct 29 '15 at 08:18
0

You have paintname declared as a char. That means it only holds one character. You need it hold multiple characters, i.e. a char array:

char paintname[50];
...
scanf("%s", paintname);
dbush
  • 205,898
  • 23
  • 218
  • 273