Note that there are some explanatory texts on larger screens.

plurals
  1. PORemove an element from a vector in C
    primarykey
    data
    text
    <p>How can I remove an element from a vector in C without changing my print_vector function?</p> <p>1) Here's the code that I made for removing an element on a position given from the keybord:</p> <pre><code>void remove_a_cost(int a) { int nr, c; printf("Give the number of cost for remove: "); scanf("%d", &amp;nr); if(nr&gt;a) { printf("The remove is impossible!\n"); } else { for(c=nr;c&lt;=a;c++) { chelt[c]=chelt[c+1]; } } } </code></pre> <p>2)This is the print function</p> <pre><code>void print_costs(int a) { int i; if(a&gt;0 &amp;&amp; a&lt;=n) { for(i=1;i&lt;=a;i++) { printf("\nCost %d\n\n",i); printf("Day: %s\n", chelt[i].day); printf("Sum: %d\n", chelt[i].sum); printf("Type: %s\n", chelt[i].type); } } } </code></pre> <p>3) Here's the add_new_cost() function</p> <pre><code>int add_new_cost() { int a,i; printf("Nr of costs = "); scanf("%d", &amp;a); if(a&gt;0 &amp;&amp; a&lt;=n) { for(i=1;i&lt;=a;i++) { printf("\nType the attributes for cost %d",i); printf("\nDay = "); scanf("%s",chelt[i].day); printf("Sum = "); scanf("%d", &amp;chelt[i].sum); printf("Type = "); scanf("%s",chelt[i].type); } } return a; } </code></pre> <p>4) This is the main function</p> <pre><code>int main() { setbuf(stdout,NULL); int b,choice; do { printf("\nMenu\n\n"); printf("1 - Add a cost\n"); printf("2 - Print a cost\n"); printf("3 - Update a cost\n"); printf("4 - Delete a cost\n"); printf("5 - Exit\n\n"); printf("Command = "); scanf("%d",&amp;choice); switch (choice) { case 1: b=add_new_cost(); break; case 2: print_costs(b); break; case 3: update_cost(b); break; case 4: remove_a_cost(b); break; case 0: printf("Goodbye\n"); break; default: printf("Wrong Choice. Enter again\n"); break; } } while (choice != 0); return 0; } </code></pre> <p>Example: If I have 4 elements on the vector:</p> <pre><code>1)Type the attributes for cost Day = luni Sum = 2 Type = dsasa Type the attributes for cost 2 Day = marti Sum = 23 Type = adsds Type the attributes for cost 3 Day = miercuri Sum = 23 Type = asd Type the attributes for cost 4 Day = joi Sum = 232 Type = asdas </code></pre> <p>and I try to delete, let's say the 3rd element, this is what I receive when I print:</p> <pre><code>Cost 1 Day: luni Sum: 20 Type: maradf Cost 2 Day: marti Sum: 23 Type: afas Cost 3 Day: joi Sum: 45 Type: sdfadsf Cost 4 Day: Sum: 0 Type: </code></pre> <p>The element(COST 4) appears when it should've been deleted. Is there a solution for deleting the element WITHOUT changing the print function?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload