STRUCT – Definition And Access To Data

The rest of the program is not difficult: we know how to read and compare texts and numbers. We will only make two considerations:

It will not behave correctly if the texts (name of the file, for example) contain spaces, because we still do not know how to read texts with spaces.
We have limited the number of chips to 1000, so, if they ask us to add, we should make sure before we still have space available.
With all this, our source would look like this:

// Introduction to C ++, Nacho Cabanes
// Example 07.05:
// Array with many struct and menu to manage it

#include <iostream>
#include <string>
using namespace std;

Type struct
{
string nameFich; // File name
long tamanyo; // The size in bytes
};

int numeroFichas = 0; // Number of chips we already have
int i; // For loops
int option; // The menu option that the user chooses

stringTemporal text; // To request data from the user
IntTemporal number;

int main ()
{
tipoDatos * fichas = new tipoDatos [1000];

do
{
// Main menu
cout << endl;
cout << “Choose an option:” << endl;
cout << “1.- Add data of a new file” << endl;
cout << “2.- Show the names of all the files” << endl;
cout << “3.- Show files that are more than a certain size” << endl;
cout << “4.- See data of a file” << endl;
cout << “5.- Exit” << endl;

cin >> option;

// We do one thing or another according to the chosen option
switch (option)
{
case 1: // Add a new data
if (numberFichas <1000) // If it stays hollow
{
cout << “Enter the name of the file:”;
cin >> tabs [numberTasks] .nameFich;
cout << “Enter the size in KB:”;
cin >> tabs [numberTasks] .tamanyo;
numberFichas ++; // And we have one more tab
}
else // If there is no space for more chips, we warn
cout << “Maximum number of cards reached (1000)!” << endl;
break;

case 2: // Show all
for (i = 0; i <numberFichas; i ++)
cout << “Name:” << index cards [i] .nameFich
<< “; Size:” << tabs [i] .tamanyo
<< “Kb” << endl;
break;

case 3: // Show according to size
cout << “From what size do you want me to show you?”;
cin >> Temporary number;
for (i = 0; i <numberFichas; i ++)
if (tabs [i] .tamanyo> = Temporary number)
cout << “Name:” << index cards [i] .nameFich
<< “; Size:” << tabs [i] .tamanyo
<< “Kb” << endl;
break;

case 4: // See all the data (few) of a file
cout << “From which file do you want to see all the data?”;
cin >> Temporal text;
for (i = 0; i <numberFichas; i ++)
if (tabs [i] .nameFich == textTemporal)
cout << “Name:” << index cards [i] .nameFich
<< “; Size:” << tabs [i] .tamanyo
<< “Kb” << endl;
break;

case 5: // Exit: we warn that we left
cout << “End of the program” << endl;
break;

default: // Other option: not valid
cout << “Unknown option!” << endl;
break;
}
} while (option! = 5); // If the option is 5, we end

return 0;
}

You may also like...

1 Response

  1. 2017

    […] Previous story STRUCT – Definition And Access To Data […]

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

error: Content is protected !!