search through check

So I am trying to make a program to search a string and put into a number sequence and find i am having some mistakes. The program is suppose to go like this:

Enter the string you wish to show:
one seven eight nine

The number you entered was 1789


I know it is going through an infinte loop but dont know how to change the bool to check the string once and not sure if any other prob, please help and thanks in advanced.


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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82


#include <iostream>
#include <string>

using namespace std;

int main()
{

string number = " ";
bool search = true;


string zero = "zero";
string one = "one";
string two = "two";
string three = "three";
string four = "four";
string five = "five";
string six = "six";
string seven = "seven";
string eight = "eight";
string nine = "nine";



cout << "enter the number you wish to change" << endl;
getline(cin,number);


// search through the string and seach for every word in sentence

// need to change search into false but dont know where
while(search == true){
    //if zero is found 
    if(number.find(zero)){
        number = number + "0";
    }
    // if one is found
    else if(number.find(one)){
        number = number + "1";
    }
    // if two is found....etc
    else if(number.find(two)){
        number = number + "2";
    }
    else if(number.find(three)){
        number = number + "3";
    }
    else if(number.find(four)){
        number = number + "4";
    }
    else if(number.find(five)){
        number = number + "5";
    }
    else if(number.find(six)){
        number = number + "6";
    }
    else if(number.find(seven)){
        number = number + "7";
    }
    else if(number.find(eight)){
        number = number + "8";
    }
    else if(number.find(nine)){
        number = number + "9";
    }
    else{
        cout << "error occured in the searched " << endl;
    }
}

cout << "the number you  entered was " << number << endl;

    return 0;
}




Last edited on
It's difficult to know where/how to set "search" to false in your algorithm.
Your algorithm is basically backwards and wouldn't work properly even with a terminating condition for the loop.
If I entered "four three two one" it will compute the value "1234" because it's just searching the whole string for all the different number names in order over and over again.

What you need to do is process the input string directly. Then the end condition is obvious: you reached the end of the string. You need to determine where the word boundaries are, extract the words and look each up in the order they appear.

EDIT: I just noticed that, strangely, you seem to be trying to build up your output in the input string. You should use a separate output string. And you're not using "find" right. It doesn't return a bool (or anything useful as a bool). It returns std::string::npos if it doesn't find the substring.
Last edited on
Actually it would find "one" from "four three two one" on every iteration (forever). The joys of if..else if..else

http://www.cplusplus.com/reference/string/string/find/
Since the return value is position, "four" has position 0 and 0 converts to false.
Therefore, there is no "four" in "four three two one"??


Formatted input. Read one word at a time.
If it equals a known word, then add corresponding value.
Such loop ends when stream ends or fails, which could be inconvenient.

Plan B: Read the line. Then create std::istringstream
Now you have a finite stream and you can read one word at a time.
See: http://www.cplusplus.com/reference/sstream/istringstream/istringstream/
Last edited on
Actually it would find "one" from "four three two one" on every iteration (forever).

Good point. I actually had in mind a scheme to delete the words when they've been found as a way to detect the end condition. That would stop the infinite loop but not print the words out in order.
its easier to process in reverse.
then you can do powers of 10 from 0

//again, in reverse:
for(int i = 0; have more input to process ; i++)
intval += pow(10, i++)* lookupdigit(textdigit); //unordered map, string array, whatever this is

one seven eight nine
that says 1 * 9
+ 10* 8
+ 100 * 7
+ 1000 * 1
etc

whole program is only like 5 lines with that idea, including a string table:
string lookupdigit{ {"zero"}, {"one"}, ... {"nine"} };
Last edited on
Looks like one for std::map and std::stringstream.
As a starter, possibly:

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
#include <map>
#include <string>
#include <iostream>
#include <sstream>

int main()
{
	const std::map<std::string, unsigned> digits {{"zero", 0}, {"one", 1}, {"two", 2}, {"three", 3}, {"four", 4}, {"five", 5}, {"six", 6}, {"seven", 7}, {"eight", 8}, {"nine", 9}};

	std::string num;

	std::cout << "Enter number in words: ";
	std::getline(std::cin, num);

	std::istringstream iss(num);
	std::ostringstream oss;

	for (std::string n; iss >> n; )
		if (const auto f {digits.find(n)}; f != digits.end())
			oss << f->second;
		else
			oss << n << " - unknown digit word\n";

	std::cout << "The number you entered was " << oss.str() << '\n';
}


Then need to deal with non-lowercase case input and what happens if not a digit word entered. This code just uses a message?
Registered users can post here. Sign in or register to post.