Click here to Skip to main content
16,000,841 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Problems with C ++ arrays and strings, line 77 gives me more doubts, but I don't know if the error is really there.

What I have tried:

Hi! It is the first time that I have placed in Code Project, I hope you will be of great help to me. Anyway I'm someone who is learning C ++ ... I was doing an exercise in which I was asked to search for keywords within a string, I tried to do it in the best way but finally I got an error: "77 | error: invalid types '__gnu_cxx :: __ alloc_traitsstd :: allocator <char, char> :: value_type {aka char} [int]' for array subscript | ". What is the cause of this error? Here attached the code of the exercise carried out:
C++
<pre>#include <iostream>
using namespace std;
//Realizzare un programma strutturato con un menu per memorizzare dei promemoria testuali. Ad ogni promemoria è associata una priorità indicata con un numero da 1 (bassa priorità) a 5 (alta priorità). Il menu dovrà avere le seguenti funzionalità: 1. Aggiunta di un promemoria con la relativa priorità; 2. Visualizzazione di tutti i promemoria dall’ultimo al primo inserito; 3. Visualizzazione del numero di promemoria per ciascun livello di priorità; 4. (Solo per chi non soffre di problemi cardiaci) Visualizzare i promemoria che contengono una parola inserita dall’utente. 5. Uscita Effettua i controlli necessari.
const int DIM_MAX=100;

int main()
{
    string promemoria[DIM_MAX], parC;
    int scelta=0, priority[DIM_MAX], p1=0, p2=0, p3=0, p4=0, p5=0, promIns=0;
    do{
        system("cls");
        cout << "***** MENU' *****" << endl;
        cout << "0. Exit" << endl;
        cout << "1. Aggiunta promemoria" << endl;
        cout << "2. Visualizzare i promemoria dall'ultimo al primo inserito" << endl;
        cout << "3. Visualizzare i numeri di promemoria per ciascun livello di priority" << endl;
        cout << "4. Visualizzare i promemoria che si desidera con delle parole chiave" << endl << endl;
        cout << "Scegli l'opzione che ti piace di piu'! ";
        cin >> scelta;
        switch(scelta){
            case 0:
                cout << "Grazie per aver usato il mio servizio, arrivederci!" << endl << endl;
                break;
            case 1:
                cout << "Promemoria: ";
                getline(cin, promemoria[promIns]);
                cin.clear();
                fflush(stdin);
                cout << "Priority: ";
                cin >> priority[promIns];
                while(priority[promIns]<0 || priority[promIns]>5){
                    cout << "Priority errata, prego reinserire una priority compresa tra 1 e 5: ";
                    cin >> priority[promIns];
                }
                switch(priority[promIns]){
                    case 1:
                        p1++;
                        break;
                    case 2:
                        p2++;
                        break;
                    case 3:
                        p3++;
                        break;
                    case 4:
                        p4++;
                        break;
                    case 5:
                        p5++;
                        break;
                    default:
                        break;
                }
                promIns++;
                break;
            case 2:
                for(int i=promIns-1; i>=0; i--){
                    cout << "Promemoria: " << promemoria[i] << endl;
                    cout << "Priority: " << priority[i] << endl << endl;
                }
                break;
            case 3:
                cout << "Hai " << p1 << " promemoria di bassa priority" << endl;
                cout << "Hai " << p2 << " promemoria di medio-bassa priority" << endl;
                cout << "Hai " << p3 << " promemoria di media priority" << endl;
                cout << "Hai " << p4 << " promemoria di medio-alta priority" << endl;
                cout << "Hai " << p5 << " promemoria di alta priority" << endl;
                break;
            case 4:
                cout << "Prego inserire la parola chiave: ";
                getline(cin, parC);
                cin.clear();
                fflush(stdin);
                cout << "Forse stai cercando..." << endl << endl;
                for(int i=0, cont=0, lettG=0, contL=0; cont<promIns; cont++){
                    while(promemoria[cont][i]!='\0'){
                        if(promemoria[cont][i] == parC[cont][i]){
                            lettG++;
                        }
                        else{
                            lettG=0;
                        }
                        i++;
                    }
                    while(parC[contL]!='\0'){
                        contL++;
                    }
                    if(lettG==contL){
                        cout << promemoria[cont] << endl;
                    }
                    i=0;
                }
                break;
            default:
                cout << "INPUT ERRATO! Reinserire correttamente l'opzione.";
                break;
        }
        system("pause");
    }
    while(scelta!=0);
    return 0;
}


I am not one who can speak English well because I am Italian, so I hope to have explained to you in the best way how much. If you need the C ++ code in English, please let me know.

Thanks in advance for the answer;)

NewTechSlyDev_
Posted
Updated 12-Dec-21 21:11pm
v2

1 solution

ParC is not an array:
string promemoria[DIM_MAX], parC

So line 77 rightly complains that you can't apply a second indexer to it:
if(promemoria[cont][i] == parC[cont][i]){

It may be that you want this:
if(promemoria[cont][i] == parC[i]){
But that's up to you - I have no idea what that code is intended to do.
 
Share this answer
 
Comments
Member 15464998 12-Dec-21 17:02pm    
Thanks Bro, the code has started
OriginalGriff 13-Dec-21 2:08am    
You're welcome, but don't call me "bro".

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900