Skip to content
This repository was archived by the owner on Jul 29, 2025. It is now read-only.

Commit f837d1d

Browse files
committed
Step 5 - Refactor
1 parent 30dfec5 commit f837d1d

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

add.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,16 @@ int add(string numbers) {
1111
numbers = numbers.substr(4);
1212
}
1313
if (numbers.find("-") != string::npos) {
14-
throw runtime_error("negatives not allowed: -" + numbers.substr(numbers.find("-") + 1, 1));
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+
}
1524
}
1625
int sum = 0;
1726
stringstream ss(numbers);

0 commit comments

Comments
 (0)