This repository was archived by the owner on Jul 29, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 1 file changed +9
-17
lines changed Expand file tree Collapse file tree 1 file changed +9
-17
lines changed Original file line number Diff line number Diff line change 55using namespace std ;
66
77int add (string numbers) {
8- char delimeter = ' ,' ;
8+ char delimiter = ' ,' ;
99 if (numbers.find (" //" ) == 0 ) {
10- delimeter = numbers[2 ];
10+ delimiter = numbers[2 ];
1111 numbers = numbers.substr (4 );
1212 }
13- if (numbers.find (" -" ) != string::npos) {
14- int minusPos = numbers.find (" -" );
15- int substringStart = minusPos + 1 ;
16- int newlinePos = numbers.substr (substringStart).find (' \n ' );
17- int delimiterPos = numbers.substr (substringStart).find (delimeter);
18- if (delimiterPos != string::npos && (newlinePos == string::npos || newlinePos < delimiterPos)) {
19- throw std::runtime_error (" negatives not allowed: -" + numbers.substr (substringStart, delimiterPos));
20- }
21- else {
22- throw std::runtime_error (" negatives not allowed: -" + numbers.substr (substringStart, newlinePos));
23- }
24- }
2513 int sum = 0 ;
2614 stringstream ss (numbers);
2715 string num, strg;
2816 while (getline (ss, strg, ' \n ' )) {
2917 stringstream lineStream (strg);
30- while (getline (lineStream, num, delimeter)) {
31- if (stoi (num) <= 1000 ) {
32- sum += stoi (num);
18+ while (getline (lineStream, num, delimiter)) {
19+ int currentNum = stoi (num);
20+ if (currentNum < 0 ) {
21+ throw runtime_error (" negatives not allowed: " + to_string (currentNum));
22+ }
23+ if (currentNum <= 1000 ) {
24+ sum += currentNum;
3325 }
3426 }
3527 }
You can’t perform that action at this time.
0 commit comments