How do I incorporate the structure array into the while loop? I need help ASAP! TYIA.

You will write a program to ask the user to enter the data in the table given below (prompt the user using input command), and store it in a structure array. (Hint: You will be using a while loop; at the end of the each loop, ask user if they want to enter more data, if yes, add another entry into the structure, else stop). The name of your structure array should be planet (to store information about the planets), and the field names for the structure array are: name, mass, year, velocity.
This is what I have to far. Surprise surprise its not working. Please help.
% This program will take user inputs of planet data and store it into a
% structure array.
n=1;
while n>1
disp('Do you need to enter data?: ');
if n>1 %yes
plant(1).name = input('name');
plant(1).mass = input('mass');
plant(1).year = input('length of year in earth year');
plant(1).velocity = input('mean orbital velocity');
else n<1; %no
end
disp('Do you need to enter data?: ');
if n>1 %yes
plant(2).name = input('name');
plant(2).mass = input('mass');
plant(2).year = input('length of year in earth year');
plant(2).velocity = input('mean orbital velocity');
else n<1; %no
end
disp('Do you need to enter data?: ');
if n>1 %yes
plant(3).name = input('name');
plant(3).mass = input('mass');
plant(3).year = input('length of year in earth year');
plant(3).velocity = input('mean orbital velocity');
else n<1; %no
end
disp('Do you need to enter data?: ');
if n>1 %yes
plant(4).name = input('name');
plant(4).mass = input('mass');
plant(4).year = input('length of year in earth year');
plant(4).velocity = input('mean orbital velocity');
else n<1; %no
end
end

 Accepted Answer

Look at your first two lines:
n=1;
while n>1
Since n>1 is not true, you will never get into that while loop and nothing will happen.
But even after you fix that up, you have code explicitly for planet(1), planet(2), etc. That is not what your professor wants. He/she wants you to use a variable (i.e., n in your case) for the indexing and to bump that up based on how the user responds to your "more" question. So an outline based on your current code would look something like this:
% This program will take user inputs of planet data and store it into a
% structure array.
n=1;
while true
planet(n).name = input('name');
planet(n).mass = input('mass');
planet(n).year = input('length of year in earth year');
planet(n).velocity = input('mean orbital velocity');
(insert code here to ask the user if they want to input more data) <-- You write the code for this
(if they answer yes) <-- You write the code for this
n = n + 1; % <-- bump up n and go to next loop iteration
else
break; % <-- no more inputs, so break out of loop
end
end
I have left some of the code for you to write. E.g., use another input statement and then test what they input to see if it is yes or no (or 0 or 1 if you use numeric input).

2 Comments

I fixed it and the code got stuck in a loop and it won't display the entire structure array or ask for the inputs.
% This program will take user inputs of planet data and store it into a
% structure array.
n=1;
while true
plant(1).name = input('name');
plant(1).mass = input('mass');
plant(1).year = input('length of year in earth year');
plant(1).velocity = input('mean orbital velocity');
disp('yes to enter more data');
disp('no to not enter more data');
if yes
plant(1).name = input('name: ');
plant(1).mass = input('mass: '):
plant(1).year = input('length of year in earth year; ');
plant(1).velocity = input('mean orbital velocity: ');
n = n + 1;
else
break;
end
end
You need to use my code EXACTLY AS WRITTEN, and just write the two lines that I have indicated. Your latest comment post still has explicit indexing (i.e. plant(1) instead of planet(n)) and that will not work. Also you need to get input from the user after you ask them if they want to input more data ... currently you don't do that.

Sign in to comment.

More Answers (0)

Categories

Find more on Gravitation, Cosmology & Astrophysics in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!