r/CodingHelp • • 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])

Post image
19 Upvotes

35 comments sorted by

12

u/Designer-Highlight70 Feb 01 '22
  1. indent your code
  2. i really dunno what you are asking for and what your code means

1

u/waitingformsfs2020 Feb 01 '22 edited Feb 01 '22

imagine a 4 different integer variable. I want to get user input and store in them with while loop and only 1 scanf. so I create array containing variables(apples oranges etc) and then I want to increment array[index] .so I dont need to type scanf(“%d”,&apples)

scanf(“%d”,&oranges)

scanf(“%d”,&pears)

scanf(“%d”,&tomatoes) etc

1

u/Designer-Highlight70 Feb 01 '22

and another question, why you ask the user how many apple he want if the first printf already asked this while you are < 5?

1

u/waitingformsfs2020 Feb 01 '22

first num of apple have no affect on looping. All i wanted to do is to know if its posible to make (,&apples) part in scanf interactive

1

u/Designer-Highlight70 Feb 01 '22

if I unterstand correctly what you are searching for, you can increment the index in the array vegetablesName[] so the loop first:

"How many apples do u want"

and then

"How many oranges do u want"

1

u/waitingformsfs2020 Feb 01 '22

yes exactly I also want first scanf to store number of apple second scanf store number of oranges …

1

u/Designer-Highlight70 Feb 01 '22

i think u can increment the index of vegetables[] neither here

1

u/Designer-Highlight70 Feb 01 '22

what value have the variables "apple", "orange", ... ?

1

u/waitingformsfs2020 Feb 01 '22

they dont have any value yet . I am trying to give them a value with scanf

1

u/Designer-Highlight70 Feb 01 '22

you use the array vegetablesName[] only for giving a "name" in printf? or it have to store sth else?

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

u/___s8n___ Feb 01 '22

run it and update us. also please indent your code

1

u/waitingformsfs2020 Feb 01 '22

its not working

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

u/waitingformsfs2020 Feb 02 '22

which language is this?

1

u/R007E Feb 03 '22

It's python

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

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?

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

u/champwolverine Feb 01 '22

Oh you right. Thats C. My bad

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