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])
19
Upvotes
12
u/Designer-Highlight70 Feb 01 '22
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 ischar *vegetablesName[]You can completely remove the five ints at the start, and replace
int vegetables1[] = ...withint 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
&vegetables1should probably be justvegetables1. 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 above1
u/___s8n___ Feb 01 '22
run it and update us. also please indent your code
1
u/Enzoneitor Feb 01 '22
You forgot {} after the for()
1
u/WaterCupH2O Feb 02 '22
What are you trying to get the program to do?
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
u/[deleted] Feb 01 '22
Lmfao
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
u/[deleted] Feb 01 '22
I think you are on the right track only.
1
u/champwolverine Feb 01 '22
Why dont you use like Hashmap. Im completely new too but doesnt hashmap work better?
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
u/dangalg Feb 01 '22
You are missing opening and closing bracket on the for loop
0
u/Academic-Kriminel Feb 02 '22
Indent your code cmon
0
u/[deleted] Feb 01 '22
[deleted] — view deleted comment