Digit to numbers using vectors and strings
Apr 26, 2015 at 10:08am UTC
I like to convert digit to number and number to digit only using vectors and strings. And when I run this code on Dev c++, an error message "Vector was not declared in this scope...suggested alternative" like this appears. Is that supposed to be? I checked again and again my code(actually copied from a website), I don't find any errors. Can anyone explain this?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
#include "iostream"
#include "vector"
#include "string"
#include "algorithm"
int main ()
{
vector<string>names;
vector<string>digits;
{names.push_back("zero" ); digits.push_back("0" );
names.push_back("one" ); digits.push_back("1" );
names.push_back("two" ); digits.push_back("2" );
names.push_back("three" ); digits.push_back("3" );
names.push_back("four" ); digits.push_back("4" );
names.push_back("five" ); digits.push_back("5" );
names.push_back("six" ); digits.push_back("6" );
names.push_back("seven" ); digits.push_back("7" );
names.push_back("eight" ); digits.push_back("8" );
names.push_back("nine" ); digits.push_back("9" );
}
string input;
while (cin >> input) {
bool ok = false ;
for (int i = 0; i < 10; ++i) {
if (input == names[i]) {
cout << digits[i] << endl;
ok = true ;
} else if (input == digits[i]) {
cout << names[i] << endl;
ok = true ;
}
}
if (! ok) cout << "Unknown number" << endl;
}
// keep_window_open();
return 0;
}
Last edited on Apr 26, 2015 at 10:09am UTC
Apr 26, 2015 at 10:23am UTC
Names in the standard library are located in the std namespace. If you put std:: in front of
vector ,
string ,
cin ,
cout and
endl it should work.
1 2 3 4
int main ()
{
std:: vector<std:: string>names;
...
Apr 26, 2015 at 10:34am UTC
Thanks a million :3 :3 #Peter87
Topic archived. No new replies allowed.