
Ayuda con codigo de estructuras
Publicado por Moises (1 intervención) el 10/02/2022 22:32:39
//La idea del codigo es crear un array de estructuras luego separalas en dos grupos con booleanos
//y guardarlas en dos nuevas arrays de estructuras luego imprimirlas mi problema es que no me lee bien gracias
#include <iostream>
#include <stdio.h>
#include <conio.h>
#include <string.h>
using namespace std;
struct person
{
char name[30];
bool statement;
};
int const MAX=10;
person General[MAX];
person first[MAX];
person second[MAX];
int global_counter_1=0;
int global_counter_2=0;
void load_data(int size);
void fill_persons(int size);
void display_all_data(int size);
void display_first(int counter_1);
void display_second(int counter_2);
int main()
{
int size;
cout<<"How many people you would like to have:\n";
cin>>size;
load_data(size);
display_all_data(size);
fill_persons(size);
cout<<endl;
display_first(global_counter_1);
cout<<endl;
display_second(global_counter_2);
getch();
return 0;
}
void load_data(int size)
{
string ask;
for(int i=0; i<size;i++)
{
fflush(stdin);
cout<<"Enter the name of the person: "; cin.getline(General[i].name,30);
cout<<"Writte 't' if the person have an illnes or 'f' if not:"; cin>>ask;
if(ask == "T" or ask == "t")
{
cout<<"--";
General[i].statement = 1; // Means have an illnes
cout<<General[i].statement;
cout<<endl;
}
if(ask == "F" or ask == "f")
{
cout<<"--";
General[i].statement = 0; // Means dont have";
cout<<General[i].statement;
cout<<endl;
}
if(ask != "T" and ask != "t" and ask != "F" and ask != "f")
{
cout<<"Enter a valid option";
cout<<endl;
}
}
}
void fill_persons(int size)
{
for(int i=0; i<size; i++)
{
if(General[i].statement == 0)
{
cout<<General[i].statement;
strcpy(first[i].name,General[i].name);// Normal
cout<<"-----false "<<first[i].name;
cout<<General[i].name;
cout<<endl;
global_counter_1++;
}
if(General[i].statement == 1)
{
cout<<General[i].statement;
strcpy(second[i].name,General[i].name); // With illnes
cout<<"-----true "<<second[i].name;
cout<<General[i].name;
cout<<endl;
global_counter_2++;
}
}
}
void display_first( int countter_1)
{
cout<<"---------Simply persons---------\n";
for(int i=0; i<countter_1;i++)
{
cout<<"Name: "<<first[i].name<<endl;
}
}
void display_second( int countter_2)
{
cout<<"---------Persons with illnes---------\n";
for(int i=0; i < countter_2;i++)
{
cout<<"Name: "<<second[i].name<<endl;
}
}
void display_all_data( int size)
{
for (int i = 0; i < size; i++)
{
cout << "-----------Persona " << i + 1 << "-----------\n";
cout << General[i].name;
cout << endl;
cout << General[i].statement;
cout << endl;
}
}
//y guardarlas en dos nuevas arrays de estructuras luego imprimirlas mi problema es que no me lee bien gracias
#include <iostream>
#include <stdio.h>
#include <conio.h>
#include <string.h>
using namespace std;
struct person
{
char name[30];
bool statement;
};
int const MAX=10;
person General[MAX];
person first[MAX];
person second[MAX];
int global_counter_1=0;
int global_counter_2=0;
void load_data(int size);
void fill_persons(int size);
void display_all_data(int size);
void display_first(int counter_1);
void display_second(int counter_2);
int main()
{
int size;
cout<<"How many people you would like to have:\n";
cin>>size;
load_data(size);
display_all_data(size);
fill_persons(size);
cout<<endl;
display_first(global_counter_1);
cout<<endl;
display_second(global_counter_2);
getch();
return 0;
}
void load_data(int size)
{
string ask;
for(int i=0; i<size;i++)
{
fflush(stdin);
cout<<"Enter the name of the person: "; cin.getline(General[i].name,30);
cout<<"Writte 't' if the person have an illnes or 'f' if not:"; cin>>ask;
if(ask == "T" or ask == "t")
{
cout<<"--";
General[i].statement = 1; // Means have an illnes
cout<<General[i].statement;
cout<<endl;
}
if(ask == "F" or ask == "f")
{
cout<<"--";
General[i].statement = 0; // Means dont have";
cout<<General[i].statement;
cout<<endl;
}
if(ask != "T" and ask != "t" and ask != "F" and ask != "f")
{
cout<<"Enter a valid option";
cout<<endl;
}
}
}
void fill_persons(int size)
{
for(int i=0; i<size; i++)
{
if(General[i].statement == 0)
{
cout<<General[i].statement;
strcpy(first[i].name,General[i].name);// Normal
cout<<"-----false "<<first[i].name;
cout<<General[i].name;
cout<<endl;
global_counter_1++;
}
if(General[i].statement == 1)
{
cout<<General[i].statement;
strcpy(second[i].name,General[i].name); // With illnes
cout<<"-----true "<<second[i].name;
cout<<General[i].name;
cout<<endl;
global_counter_2++;
}
}
}
void display_first( int countter_1)
{
cout<<"---------Simply persons---------\n";
for(int i=0; i<countter_1;i++)
{
cout<<"Name: "<<first[i].name<<endl;
}
}
void display_second( int countter_2)
{
cout<<"---------Persons with illnes---------\n";
for(int i=0; i < countter_2;i++)
{
cout<<"Name: "<<second[i].name<<endl;
}
}
void display_all_data( int size)
{
for (int i = 0; i < size; i++)
{
cout << "-----------Persona " << i + 1 << "-----------\n";
cout << General[i].name;
cout << endl;
cout << General[i].statement;
cout << endl;
}
}
Valora esta pregunta


0