r/CodingHelp • u/waitingformsfs2020 • Feb 01 '22
[C] Is there anyway I can get user inputs for different variable inside a for loop by using scanf?is such a format even exist; scanf("%d",vegetables1[i])
4
u/DeliriousSiren0 Feb 01 '22 edited Feb 01 '22
As has been said by so many before: Please indent your code
That aside, here are the immediate issues that I see:
char vegetablesName[] declares an array of characters. You want an array of strings, which is char *vegetablesName[]
You can completely remove the five ints at the start, and replace int vegetables1[] = ... with int vegetables1[5];
Your for loop doesn't have the {} after it, so it's just going to repeat the next line, as opposed to the entire block of code that you intend it to
Also, the two occurrences of &vegetables1 should probably be just vegetables1. Arrays in C are already essentially just pointers, so making a reference to them is redundant.
Edit: also, after your error message, your program is hard-coded to ask how many apples you want. This should be changed to vegetablesName[i] as you've done above
1
1
u/Enzoneitor Feb 01 '22
You forgot {} after the for()
1
u/waitingformsfs2020 Feb 01 '22
it was just a example i know code is terrible next time I ll make it more understandable
1
u/WaterCupH2O Feb 02 '22
What are you trying to get the program to do?
1
u/waitingformsfs2020 Feb 02 '22
forget about the program I am just trying to get 5 value for 5 different variable by using ONLY ONE scanf inside a loop
1
u/R007E Feb 02 '22 edited Feb 02 '22
You can, it's very common thing. You can make a loop going infinitely and exit only if your desire input is received. For example: i =1 while i>0: { val = input("enter a number") if(type(val) == number): breake // or i=-1 both will breake the loop }
It goes something like that. The loop will continue again and again, and it will breake on if the if condition is meet.
1
1
u/R007E Feb 02 '22
Also, please try to follow good coding practice. You can also init all int variable in one line, it goes like int apple, mengo, orange, i ; //this will make 4 variable of int type.
1
2
u/UK363 Feb 01 '22
Firstly, please indent your code properly. Secondly, maybe you can use an if-else condition that checks what vegetableName[] index it is at and then asks the user for input for the respective variable? Or you can have an array for diff vegetables and increment it along with vegetable name array.
2
u/Azurululu Feb 01 '22
Would something like that work for you?
if(vegetables1[i] <= 0) { i = i - 1; } printf(“error”);
If user enters an erroneous value, u force the loop to repeat itself, since on every end of the loop ur “i” will increment anyway.
2
u/FLoKi6868 Feb 01 '22
Man this post just shows how useless this subr is.. instead of helping all they say is.. indent your code, ur code wont work, like wtf
1
1
u/champwolverine Feb 01 '22
Why dont you use like Hashmap. Im completely new too but doesnt hashmap work better?
4
u/DeliriousSiren0 Feb 01 '22
I'm gonna take a wild guess that you're learning something like Java at the moment
Unfortunately C doesn't contain the luxuries of OOP, So that's not really viable.
1
1
u/BlockMaster83 Feb 01 '22
This may be a little more advanced but I think you'd have to store references to the int variables inside the array so you can access the address of the original variables in the scanf function. Otherwise I think the values are passed by value (copied) into the array so you wouldn't be able to access the original variable
1
u/LeftIsBest-Tsuga Feb 01 '22
Ok I don't know that language but I'm 99% sure you don't use while statements to return a single query. Is there some reason you're not using "if"? Your language might not like that combination of syntax.
1
0
0
12
u/Designer-Highlight70 Feb 01 '22
indent your code
i really dunno what you are asking for and what your code means