separate wordzzzz

hi there. im trying to write a code that translates the number in its numerical form (i.e two thousand five hundred == 2500). for now im starting by separating the words and translating them. problem is, when i try to separate them them, all i get is the first word. can someone tell me whats wrong?

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
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cctype>
using namespace std;

main(){
string str[30] = {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten",
                  "eleven", "twelve", "thirteen", "forteen", "fifteen", "sixteen", "seventeen", "eighteen",
                  "nineteen", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety",
                  "hundred", "thousand", "million"}, str1;
int num[30] = {1, 2, 3, 4, 5, 6 , 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 ,19, 20, 30, 40, 50, 60, 70,
              80, 90, 100, 1000, 1000000};
fstream in("english_num.in");
vector<string>vstr;

while(getline(in, str1)){
int ctr = 0, ctr1 = 0;
    for(int i = 0; i != str1.length(); i++) ///checks if its a sentence
        if(isspace(str1[i]))
            ctr1++;

    if(ctr1 == 0){      ///if its not a sentence then it prints the word
        for(int i = 0; i != 29; i++)
            if(str1 == str[i])
                cout<<str[i]<<endl;
    }
    else
    for(int i = 0; i != str1.length(); i++){    ///separating process
        if(isspace(str1[i])){
            vstr.push_back(str1.substr(ctr, i));
            ctr = i;
        }
    }

    if(!vstr.empty()){     ///prints the seperated words
        for(int i = 0; i != vstr.size(); i++){
            if(vstr[i] == "negative")
                cout<<"negative ";
        for(int j = 0; j != 29; j++)
            if(vstr[i] == str[j])
                cout<<str[i]<<" ";
        }
        cout<<endl;
        vstr.clear();
    }
}

}



here's the in file:
six
negative seven hundred twenty nine
one million one hundred one
Last edited on
the problem is that you include space in your separated words.
does that make stop d vector from getting d other words and getting the first word only?
On line 42 you compare the word in the vector with the possible words.
All but the first word contain space. So it can only be true for the first word and hence only output that
uhm, in line 42, it should output what the vector stacked. the separating process is in line 30. ..
Topic archived. No new replies allowed.