find the errors in c++

Write your question here. Find the errors and display what is the output??

The goal is to take a string that contains currency like the one below and to step through the string and find any currency. This function is from our system and works fine you just need to get it running and understand how it works.

1. Take any string like this one and read it into memory:
"I want to by a shirt but it is too expensive. My favorite one is $20.15 and my second favorite one is $15 so I do not know which one to buy"
2. Step through the string using the function here to try to find the currency and replace the currency with the currency tag which is #c. The following code does all this for you.

3. Display the output string that should look like this:
"I want to by a shirt but it is too expensive. My favorite one is #c and my second favorite one is #c so I do not know which one to buy"

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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
 #include "stdafx.h"
#include <iostream>
#include<stdio.h>
#include <string>
#include<cctype>
#include<stdlib.h>
#include<fstream>
#include<iterator>
#include<list>
#include <functional>
#include <algorithm>
#include<vector>
 
using namespace std;

class sample
{
public:
  static auto checkForCurrency = [](char *&prevPtr, char *&p)->bool
  {
    char *origPrevPtr = prevPtr;
    char *origPtr = p;

    static const unsigned char CentSymbol = 155;
   
    static const unsigned char PoundSymbol = 156;
    static const unsigned char YenYuanSymbol = 157;

    bool retVal = false;

    if (*p == '$' || *p == '€' || *p == '£' || *p == '¥' || *p == '¢')
	{
      advance(prevPtr, p, 1);
      if (isNumber(prevPtr, p))
	  {
        retVal = true;
      } 
	  else if (*p == TheSpace) 
	  {
        char *tmpPtr = p;
        advance(prevPtr, p, 1);
        if (isNumber(prevPtr, p))
		{
          retVal = true;
        } 
		else
		{
          p = tmpPtr;
        }
      }
    } 
	else if(isNumber(prevPtr, p)) {
      if (*p == CentSymbol) {
        advance(prevPtr, p, 1);
        retVal = true;
      } else if (isCurrency(prevPtr, p)) {
        retVal = true;
      } else if (*p == TheSpace) {
        char *tmpPtr = p;
        advance(prevPtr, p, 1);
        if (*p == CentSymbol) {
          advance(prevPtr, p, 1);
          retVal = true;
        } else if(isCurrency(prevPtr, p)) { 
          retVal = true;
        } else {
          p = tmpPtr;
        }
      }
    }

    if (retVal) {
      memset(origPtr, TheSpace, p - origPtr);
      *(origPtr + 0) = '#';
      *(origPtr + 1) = 'c';
    } else {
      prevPtr = origPrevPtr;
      p = origPtr;
    }

    return retVal;
  };

 static auto isCurrency = [](char *&prevPtr, char *&p)->bool
  {
    // Modified by Kewei, added several new currency units
    static const string currencies[] = {  // make sure the longer version is first
      "dollars", "dollar", "cents", "cent",
      "euros", "euro", "yen", "yuan", "pounds", "pound", "pence",
      "usd", "eur", "aud", "cad", "cny",
    };

    for (const string &currency : currencies) {
      const string::size_type lenCurrency = currency.length();
      if (strncmp(p, currency.c_str(), lenCurrency) == 0) {
        if (*prevPtr == TheSpace) {
          char *const pEndOfCurrency = p + lenCurrency;
          if (*pEndOfCurrency == TheSpace) {
            advance(prevPtr, p, lenCurrency);
            return true;
          }
        }
      }
    }

    return false;
  };


static auto isNumber = [](char *&prevPtr, char *&p)->bool
  {
    bool retVal = false;

    if (*p == '-' || *p == '+') {
      advance(prevPtr, p, 1);
      if (isDigit(prevPtr, p)) {
        retVal = true;
        for (;;) {
          while (isDigit(prevPtr, p)) {}
          if (*p == ',') {
            advance(prevPtr, p, 1);
            continue;
          }
          break;
        }
        if (*p == '.') {
          advance(prevPtr, p, 1);
          while (isDigit(prevPtr, p)) {}
          if (*p == 'e') {
            advance(prevPtr, p, 1);
            if (*p == '-' || *p == '+') {
              advance(prevPtr, p, 1);
            }
            while (isDigit(prevPtr, p)) {}
          }
        } else if (*p == 'e') {
          advance(prevPtr, p, 1);
          if (*p == '-' || *p == '+') {
            advance(prevPtr, p, 1);
          }
          while (isDigit(prevPtr, p)) {}
        } else if (*p == '/') {		
          advance(prevPtr, p, 1);
          for (;;) {
            while (isDigit(prevPtr, p)) {}
            if (*p == ',') {
              advance(prevPtr, p, 1);
              continue;
            }
            break;
          }
        }
      }
    } else if (isDigit(prevPtr, p)) {
      retVal = true;
      for (;;) {
        while (isDigit(prevPtr, p)) {}
        if (*p == ',') {
          advance(prevPtr, p, 1);
          continue;
        }
        break;
      }
      if (*p == '.') {
        advance(prevPtr, p, 1);
        while (isDigit(prevPtr, p)) {}
        if (*p == 'e') {
          advance(prevPtr, p, 1);
          if (*p == '-' || *p == '+') {
            advance(prevPtr, p, 1);
          }
          while (isDigit(prevPtr, p)) {}
        }
      } else if (*p == 'e') {
        advance(prevPtr, p, 1);
        if (*p == '-' || *p == '+') {
          advance(prevPtr, p, 1);
        }
        while (isDigit(prevPtr, p)) {}
      } else if (*p == '/') {		
        advance(prevPtr, p, 1);
        for (;;) {
          while (isDigit(prevPtr, p)) {}
          if (*p == ',') {
            advance(prevPtr, p, 1);
            continue;
          }
          break;
        }
      }
    }

    return retVal;
  };


  static auto advance = [](char *&prevPtr, char *&p, const size_t offset)->void
  {
    if (offset == 0) return;  // safety measure
    prevPtr = p + (offset - 1);
    p = prevPtr + 1;
	for (prevPtr = const_cast<char *>(text.c_str()), p = const_cast<char *>(text.c_str() + 1);
    *p != '\0';
    advance(prevPtr, p, 1)) {
      if ((flags & TagPercent) == TagPercent) {
        if (checkForPercent(prevPtr, p)) continue;
      }
  }

  };

  static auto isDigit = [](char *&prevPtr, char *&p)->bool
  {
    if (*p >= '0' && *p <= '9') {  
      advance(prevPtr, p, 1);
      return true;
    }
    return false;
  };

};

int main()
{
	sample smpl;
    smpl.checkForCurrency = []('1', '1')->bool;
	smpl.isCurrency = [](char *&prevPtr, char *&p)->bool;
	smpl.isNumber = [](char *&prevPtr, char *&p)->bool;
	smpl.advance = [](char *&prevPtr, char *&p, const size_t offset)->void;
    smpl. isDigit = [](char *&prevPtr, char *&p)->bool;
	cin.ignore();

	system("pause");
	return 0;
}
This is clearly some sort of homework or exercise. We're not going to provide solutions, I'm afraid. You need to do that and, providing you get stuck, post what in particular you're stuck with.

http://www.cplusplus.com/forum/beginner/1/
Topic archived. No new replies allowed.