const string exit="exit";
string wrd="";
while (wrd!=exit) //CHECK IF THE STRING IS "exit" (ignored!!!)
{
int a=0;
vector<string>nmbs(10); //just the vector
nmbs[0]="zero";
nmbs[1]="one";
nmbs[2]="two";
nmbs[3]="three";
nmbs[4]="four";
nmbs[5]="five";
nmbs[6]="six";
nmbs[7]="seven";
nmbs[8]="eight";
nmbs[9]="nine";
cout<<"\nenter an integer between 1 and 10\n\n";
while (cin>>wrd) //take the string
{cout<<"\n";
for (int i=0;i<nmbs.size();i++)
{if (wrd==nmbs[i]) {cout<<i; break;} //see if the string is fine
else if (wrd!=nmbs[i]) a++;
if (a!=nmbs.size()-1);
else cout<<"wrong input??";} //if it's not fine then say "wrong.."
a=0;
cout<<"\n\n";}
} |
string wrd="";string wrd; there is no need to re-initialize[read: re-set] the value to "" [or empty]){if (wrd==nmbs[i]) {cout<<i; break;} obscures in which block a certain line of code lies, there is no point in saving space if you can't get your program to run.if (a!=nmbs.size()-1); if a is not equal to the vector size then what? If you run through it with a pen and paper it will help clarify where you messed up. If you are still struggling after you've attempted it feel free to reply and i'll give you the solution.
if (a!=nmbs.size()-1); |
string "1" equal to int 1 |
const string exit="exit";
string wrd="";
int a=0;
vector<string>nmbs(10);
nmbs[0]="zero";
nmbs[1]="one";
nmbs[2]="two";
nmbs[3]="three";
nmbs[4]="four";
nmbs[5]="five";
nmbs[6]="six";
nmbs[7]="seven";
nmbs[8]="eight";
nmbs[9]="nine";
cout<<"\nenter an integer between 1 and 10\n\n";
cout<<"<exit> to leave the program\n\n";
while (cin>>wrd and wrd!=exit)
{cout<<"\n";
for (int i=0;i<nmbs.size();i++)
{if (wrd==nmbs[i])
{cout<<i; break;}
else if (wrd!=nmbs[i])
a++;
if (a!=nmbs.size());
else if (wrd=="0") cout<<"zero";
else if (wrd=="1") cout<<"one";
else if (wrd=="2") cout<<"two";
else if (wrd=="3") cout<<"three";
else if (wrd=="4") cout<<"four";
else if (wrd=="5") cout<<"five";
else if (wrd=="6") cout<<"six";
else if (wrd=="7") cout<<"seven";
else if (wrd=="8") cout<<"eight";
else if (wrd=="9") cout<<"nine";
else cout<<"wrong input??";
}
a=0;
cout<<"\n\n";}
cout<<"\nbye bye!! (any key to exit)\n\n"; |
if(a = nmb.size()-1) cout << "Wrong input";