Reputation: 5
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;
}
Upvotes: 0
Views: 30
Reputation: 225757
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);
Upvotes: 0
Reputation: 49
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.
Upvotes: 1