problem with struct/array

Write your question here.
hello! i'm having troubbles with a uni project: need to creat a program that
inserts and removes elents from a list struct/array. my problem is that it inserts but does not move the other elments along.




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
struct Hab {
	int id,sexo;
	float sal;
}; 


int main(int argc, char** argv) {
	Hab pes[1000];
	char r;
    float somasal=0,media;
	int i=0,total,total2,maior,contm=0,nid,nsexo,nsal,p;
	do {
	    cout<< "digite a idade:";
		cin>>pes[i].id;
		cout<<"digite o sexo:";
		cin>>pes[i].sexo;
		cout<<"digite o salario:";
		cin>>pes[i].sal;
		i++;
		cout<<"deseja continuar? s/n :";
		cin>>r;
		
			}
	while(r=='s');
	total=i;
	total2=i;
	for (int j=0;j<total;j++)
	somasal+=pes[j].sal;
	media=somasal/total;
	cout<<"a media de salario e:"<<media<<endl;
	maior=pes[0].id;
	
	for(int k=1;k<total;k++)
	if(pes[k].id>maior)
	maior=pes[k].id;
	cout<<"a maior idade e:"<<maior<<endl;
	
	for(int m=0;m<total;m++)
	if(pes[m].sexo==2 && pes[m].sal<=200)
	contm++;
	cout<<"mulheres com salario ate 200: "<<contm<<endl;
	for(int n=0;n<total;n++)
		cout<<pes[n].id<< pes[n].sexo<< pes[n].sal <<endl;
	
	cout<<" qual a posicao de insercao? ";
	cin>>p;	

	for(int i=total2+1;i<p;i--){
	
	    pes[i+1].id =pes[i].id;
	    pes[i+1].sal =pes[i].sal;
	    pes[i+1].sexo =pes[i].sexo;
	   	}

	    cout<< "digite a nova idade:";
		cin>>pes[p-1].id;
		cout<<"digite o novo sexo:";
		cin>>pes[p-1].sexo;
		cout<<"digite o novo salario:";
		cin>>pes[p-1].sal;
    
	
	for( i=0;i<total2+1;i++)
		cout<<pes[i].id<< pes[i].sexo<< pes[i].sal <<endl;
	
	
	
	return 0;
}
Last edited on
Artur Gouveia,

Three things while I work on your code.


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



It also helps to post a complete program that can be compiled and run. Someone may see something that you missed.

Since some of your strings are in a language other than English it would help to know what language it is in to make sure the translation is correct.

Andy
Hello Artur Gouveia,

While working on the program I have some questions.

When it comes to "sexo" what are you using for its value?

When it comes to "sal" what is the range and value that you want to work with?

I have also considered the "sexo" would work better as a "char" and not an "int". Just a thought.

Andy
Hello Artur Gouveia,

Working on the program these are some of the changes I made:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main()
{
    constexpr int MAXSIZE{ 15 };  // <--- Reduced for testing. Change later.

    //Hab pes[MAXSIZE];  // <--- Original.
    Hab pes[MAXSIZE]     // <--- Used for testing. Comment or remove when finished.
    {
        { 60, 'M', 100 },
        { 20, 'F', 200 },
        { 21, 'M', 150},
        { 25, 'F', 250},
        { 25, 'M', 175},
        { 42, 'F', 180}
    };
    char answer{};
    double somasal{}, media{};
    int idxCount{ 6 };  // <--- Changed for testing. Remove the " 5 " for normal run.
    //int total{}, total2{}, nid{}, nsexo{}, nsal{};   // <--- These variables unused or not needed.
    int maior{}, contm{}, insertPoint{};

Following this part I commented out the entire do/while loop for testing. One addition I did make is after entering "sexo" I used this to make sure the letter is in upper case:
 
pes[idxCount].sexo = std::toupper(pes[idxCount].sexo);  // <--- Added. Requires header file "<cctype>". 


The first for loop to sum the array is this now:
1
2
for (int j = 0; j < idxCount; j++)
    somasal += pes[j].sal;

There is no need for the extra "total" variables. Just use what you have. "idxCount", idx short for index, is a better name than "i", but you may choose a different name that works better for you in your language.

The program is good down to where you move the elements in the array before the insert.

I did make this change:
1
2
3
4
std::cout << "  #  Idade Sexo Sal\n " << std::string(18, '-') << '\n';

for (int idx = 0; idx < idxCount; idx++)
    std::cout << " [" << idx << "]   " << pes[idx].idade << "   " << pes[idx].sexo << "   " << pes[idx].sal << '\n';


Which gives the output of:

  #  Idade Sexo Sal
 ------------------
 [0]   60   M   100
 [1]   20   F   200
 [2]   21   M   150
 [3]   25   F   250
 [4]   25   M   175


But in order to use this the variable "insertPoint" would need to increase 1 before the for loop.

The other option:

  #  Idade Sexo Sal
 ------------------
 [1]   60   M   100
 [2]   20   F   200
 [3]   21   M   150
 [4]   25   F   250
 [5]   25   M   175


Then in line 4 the "idx" between the []s would need to be "idx + 1" then "insertPoint" would not have to change.

The problem with your move for loop, other than being overworked, is that it does not work.

You are thinking about it in the right direction just not implementing it correctly.

In the first part of the for loop the loop counter needs to start at the end of the used part of the array. The variable "idxCount" not only tells you how many elements of the array are used, but it also marks the next element in the array. So the middle part of the for loop needs to check if the loop counter is greater than the "insertPoint". The last part of the for loop is correct.

Inside the for loop all I have is pes[idx] = pes[idx - 1];. Notice the minus here because you are starting at the end and working backwards.

Following the for loop you can use this if you want: pes[insertPoint] = {};, but it is not necessary since you will be entering new information.

You do have to add 1 to "idxCount" to reflect the new size of the part of the array used.

What I have not done yet is before the shift of the array you should check that "idxCount" + 1 is less than "MAXSIZE" so that you do not go past the end of the array.

Almost forgot I changed the struct to:
1
2
3
4
5
6
struct Hab
{
    int idade{};
    char sexo{};  // <--- Changed type.
    double sal{};
};


Then the prompt for "sexo" is: std::cout << "digite o sexo (M / F): ";. What is in the () can be what you need and if you keep the variable as an "int" then you need to say what numbers to use.

Also notice there is a space after the (:). Without a newline (\n) or "endl" this puts the "cin" on the same line as the prompt with a space between the prompt and what you enter.

Andy
Andy

thank you very much for your help. As a beguinner in programming i am going to study and then implement your recommendations, the language is portuguese.

Artur Gouveia
Topic archived. No new replies allowed.