0

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.

Mikhail Zhuikov
  • 1,213
  • 2
  • 9
  • 19
Coder_H
  • 302
  • 3
  • 9
  • Unrelated to your problem, but please read [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) as well as [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Some programmer dude Oct 28 '19 at 12:56
  • And please take some time to read about [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). And please learn how to create a proper [mcve], one that replicates the problem you ask about and doesn't contain any unrelated problems (which your example does). – Some programmer dude Oct 28 '19 at 12:58
  • 1
    Lastly about the problem you ask about: Please read more about [the range `for` statement](https://en.cppreference.com/w/cpp/language/range-for) and what it really iterates over. [Some good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) might be an important investment if you want to continue to learn C++. – Some programmer dude Oct 28 '19 at 12:59
  • Maybe this could be closed as a duplicate. https://stackoverflow.com/q/15927033/4770166 – RHertel Oct 28 '19 at 15:22

0 Answers0