lundi 28 octobre 2019

I am facing problem while iterating over a string vector

I was trying to make a program to convert numbers into words using the range-based loop for string vector.

// conversion of numbers into word 
#include<bits/stdc++.h>
using namespace std;
int main(){
vector<string> words = {"one", "two", "three","four","five","six", 
"seven", "eight", "nine"};

for(int x:words){
  cin>>num; 
  if(num==words[x]) cout<<words[x]; 
} 
return 0;

}

// this program is showing error: { "message": "no suitable conversion function from\"std::__cxx11::string\" to \"int\" exists",

}

// If inside the 'for statement' I change int x to auto x this is working fine. I don't understand what difference does this makeS.

Aucun commentaire:

Enregistrer un commentaire