Jayne West Jayne West - 1 year ago 89
C Question

How to keep track of the amount a certain loop has run and storing values into arrays

I am a newbie in coding and preparing an assignment in which I am encountering certain difficulties. I just want you guys to help me identify my mistake for one issue I am facing. Before pasting my code I am giving an overview of what had to be done.
The user have to create drones and after creating one drone the program should return back to the main menu, store the details of the 1st drone and if request to enter another drone he/she should be allowed to do so until the number of drones have reached 10. Only 10 drones are allowed and we have to use arrays for storing the values of each drone. I am unable to make the program count 10 and using arrays to store them. In the code I have pasted here, I am trying for 2 but I am unable to do that as well. Please help me...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct drone_t{
char name[30];
float top_s;
float acc;
};

void printing (struct drone_t dron[2]);

int main()
{
int a, i;
char nam;
struct drone_t drone[2];
printf("Welcome to the drone travel time calculator\n");
printf("1. Create Drone\n2. Calculate Time\n3. Exit\n");
scanf("%d", &i);
if (i == 1)
{
for (a=1; a < 3; a++)
{
printf("What is the name of the drone?\n");
scanf("%s", drone[a].name);
printf("What is the top speed of the drone? (kmph)\n");
scanf("%f", &drone[a].top_s);
printf("What is the acceleration of the drone? (mpsps)\n");
scanf("%f", &drone[a].acc);
printing(drone);
}
}
else if (i == 2)
{
printing(drone);
}
}

void printing (struct drone_t dron[2])
{
int a;
for (a=1; a < 3; a++)
{
printf("Name is: %s\n", dron[a].name);
}
//return 0;
}


Anticipated Output is:

Welcome to the drone travel time calculator
1. Create Drone
2. Calculate Time
3. Exit
1
What is the name of the drone?
Jayne
What is the top speed of the drone? (kmph)
12
what is the acceleration of the drone? (mpsps)
12
1. Create Drone
2. Calculate Time
3. Exit
1
What is the name of the drone?
JayneW
What is the top speed of the drone? (kmph)
12
what is the acceleration of the drone? (mpsps)
12
1. Create Drone
2. Calculate Time
3. Exit
2
Select a drone:
1. Jayne
2. JayneW


EDIT:

@coderredoc

I just succeeded in building a basic code as per the pseudocode you gave me. It is going back to the main menu after each input. Don't know about the 10 drones yet. Will check it. Please check my code.

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

struct drone_t{
char name[30];
float top_s;
float acc;
};

void show_menu();
void do_create();

#define MAXDRONES 10

int main()
{
int dronesCreated = 0;
int i;
while(1)
{
show_menu();
if (i == 1)
{
if(dronesCreated<=MAXDRONES-1)
{
do_create();
dronesCreated++;
}
else
{
printf("You cannot add more than 10 drones!");
}
}
else
{
break;
}
}
}

void show_menu()
{
int i;
printf("1. Create Drone\n2. Calculate Time\n3. Exit\n");
scanf("%d", &i);
}

void do_create()
{
char name[10];
int b, c;
printf("Name");
scanf("%s", name);
printf("Speed");
scanf("%d", &b);
printf("%s and %d", name, b);
}

Answer Source

As far as I can understand you are stuck in this part.

How to continue getting input?

For that you need to write a while(1){..} with a propr exit condition like break etc.

Rest of the job boils down to how many drones are there? and accessing and storing them.

For storing an array of structure will be sufficient. For count of drone you can use a seperate variable. Accessing them is nothing but array access.

A pseudocode to your guide:

#define MAXDRONES 10
int dronesCreated =0;
while(1){
  show _menu();
  if( option is DRONE_CREATE){
     if(dronesCreated<=MAXDRONES-1){         
         do_create();//increment NUM_OF_DRONES here.
         dronesCreated++;
     }
     else 
       print appropriate message.
  else if(option is PRINT)
     print()
  else
     break;
}

Look it is simple to understand the design of the program from intuitive viewpoint.

You need a loop because you need to continuouskly ask for option from user. That's why loop.

How to stop at 10 drones?

You keep a variable and check when DRONE_CREATE option is given whether 10 drones are created. If yes then don't call the function else do call the function.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download