how to empty an array position and go backward

I need to clear/empty/dunno how to say an specific array position.

I input the info, which is showed like this at output:

0 100.00 xxxxxxxxxxxx xxxxxxxxxxx
1 200.00 xxxxxxxxxxxx xxxxxxxxxxx
2 300.00 xxxxxxxxxxxx xxxxxxxxxxx
3 400.00 xxxxxxxxxxxx xxxxxxxxxxx

Im trying to clear all arrays position let's say 2, doing this:

1
2
3
ingreso[i].monto=NULL;  //float
ingreso[i].rfc.clear(); //string
ingreso[i].concepto.clear();  //string  


but when i showed again, i get this:

0 100.00 xxxxxxxxxxxx xxxxxxxxxxx
1 200.00 xxxxxxxxxxxx xxxxxxxxxxx
2 0.00
3 300.00 xxxxxxxxxxxx xxxxxxxxxxx

after cleared, i need to go backward one position if possible, to get something like this:

0 100.00 xxxxxxxxxxxx xxxxxxxxxxx
1 200.00 xxxxxxxxxxxx xxxxxxxxxxx
2 300.00 xxxxxxxxxxxx xxxxxxxxxxx


thanks in advance.
I am not sure how much this will help but a simpler way is to reference each element individually.
1
2
3
4
ingreso[0] = // value;
ingreso[1] = // value;
ingreso[2] = // value;
ingreso[3] = // value; 

This is what is known as hardcoding. Some people prefer a more dynamic approach. But if you are set on using member functions you may want to use a vector.
Just use a vector instead of an array.
well, that isnt actually what i need. I could have up to 20 positions, the user says which line wanna delete... the full rutine is this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
cout << "Input position you wanna delete. ";
cin >> indice;
for(i=0;i<=indice;i++){
        if(i==indice){
            cout <<setw(3)<<i <<setw(14)<<ingreso[i].mes <<setw(11)<<ingreso[i].monto <<setw(23)<<ingreso[i].concepto<<endl<<endl;
            cout <<"Sure wanna delete this? (y/n) ";
            cin >> resp;
            if(resp=='y' || 'Y'){

                ingreso[i].mes.clear();
                ingreso[i].monto)=0;
                ingreso[i].concepto.clear();
                cout <<"Deleted.";
            }
            else{
                cout << "Nothing deleted!";
            }
        }
    }

well, that isnt actually what i need. I could have up to 20 positions

How does that matter? A vector is dynamic and resized according to what is needed.
http://www.cplusplus.com/reference/vector/vector/

http://www.cplusplus.com/reference/vector/vector/erase/

If that isn't what you want to do then I'm misunderstanding.

Can you give a longer and clearer explanation of what you are trying to do exactly.
Last edited on
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
struct ingresos{
	float monto={ };
	string rfc, concepto, mes;
}ingreso[20];

void expenses(int pos){

        for(i=0;i<=mes;i++){
            if(i==mes){
                cout <<endl << "Input for month " <<nombreMes[i-1] <<": ";
                ingreso[pos].mes=nombreMes[i-1];
            }
        }
        cout<<endl;
        cout <<"Monto: ";
        cin >>ingreso[pos].monto;
        cout <<"Concepto: ";
        getline(cin >> ws,ingreso[pos].concepto);
    }
    cout<<endl<<endl;
    pause();
}

void show expenses(int pos){
    system(CLEAR);
    cout <<endl <<endl;
    cout << "\t\t**** Expenses****";
    cout <<endl <<endl;
    cout <<setw(2)<<"Indice" <<setw(7)<<"Mes" <<setw(13)<<"Monto" <<setw(23)<<"Concepto"<<endl;
    cout <<"-------------------------------------------------------------------------"<<endl;
    for(i=0;i<pos;i++){
        cout <<setw(3)<<i <<setw(14)<<ingreso[i].mes <<setw(11 <<ingreso[i].monto <<setw(23)<<ingreso[i].concepto<<endl;
    }
    cout <<endl;
    pause();
}

void deleteExpenses(){
    int indice;
    char resp;
    cout << "Input position you wanna delete. ";
cin >> indice;
for(i=0;i<=indice;i++){
        if(i==indice){
            cout <<setw(3)<<i <<setw(14)<<ingreso[i].mes <<setw(11)<<ingreso[i].monto <<setw(23)<<ingreso[i].concepto<<endl<<endl;
            cout <<"Sure wanna delete this? (y/n) ";
            cin >> resp;
            if(resp=='y' || 'Y'){

                ingreso[i].mes.clear();
                ingreso[i].monto)=0;
                ingreso[i].concepto.clear();
                cout <<"Deleted.";
            }
            else{
                cout << "Nothing deleted!";
            }
        }
    }


If the user said indice = 10 ..... i need to empty the whole line 10.
Last edited on
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
#include <iostream>
#include <iomanip>
#include <vector>
#include <string>

struct element_type
{
    double value;
    std::string strings[2];
};

std::vector<element_type> elements =
{
    { 100.0, { "aaaaaaaaaaaa", "bbbbbbbbbbbb" } },
    { 200.0, { "cccccccccccc", "dddddddddddd" } },
    { 300.0, { "eeeeeeeeeeee", "ffffffffffff" } },
    { 400.0, { "hhhhhhhhhhhh", "gggggggggggg" } }
};

std::ostream& operator<<(std::ostream& os, const element_type& e)
{
    os << std::fixed << std::setprecision(2) << e.value << ' '
        << e.strings[0] << ' ' << e.strings[1];
    return os;
}

template <typename container_type>
void display(std::ostream& os, const container_type& container)
{
    for (unsigned i = 0; i < container.size(); ++i)
        os << i << ' ' << container[i] << '\n';
    os << '\n';
}

int main()
{
    display(std::cout, elements);
    
    unsigned index;
    std::cout << "Enter element you'd like to remove.\n> ";

    if (std::cin >> index && index < elements.size())
    {
        std::cout << "Removing element " << index << ".\n";
        elements.erase(elements.begin() + index);
        display(std::cout, elements);
    }
    else
        std::cout << "Unable to remove element " << index << ".\n";
}


http://ideone.com/JSWoQ3
Sorry but posting a bunch of code isn't making me less confused. I think what you are trying to do is, if the user enters 10 then 10 elements will be deleted right?
1
2
3
4
5
cin >> indice;
for (int i = 0; i <= indice; i++)
{
   array[i] = 0;
}


Sorry if I am misunderstanding you.

EDIT
Never mind cire has got you.
Last edited on
Regardless of anything else, you don't need that for loop unless I'm totally lost, just the ifs.

You are still better off using a vector for your struct than an array. My Spanish sucks that is throwing me off of what you are writing, but you really should take a look at vectors.
1
2
cin >> indice;
ingreso[indice].erase;

should delete everything from that index and move everything above it down one.

Last edited on
thank you guys =)
Topic archived. No new replies allowed.