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:
<pre>#include <iostream>
using namespace std;
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_