This repository was archived by the owner on Mar 1, 2026. It is now read-only.
forked from ParosSrl/string-calculator-kata
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathC-String-Calculator-Kata.cpp
More file actions
57 lines (48 loc) · 1.37 KB
/
C-String-Calculator-Kata.cpp
File metadata and controls
57 lines (48 loc) · 1.37 KB
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
#include <iostream>
#include <fstream>
#include <string>
#include "./add.h"
using namespace std;
int main() {
// Display menu options
cout << "Choose an option:" << endl;
cout << "1. Enter the string manually" << endl;
cout << "2. Load the string from the input.txt file" << endl;
// Get user choice
int choice;
cin >> choice;
// Variable to store input string
string inputString;
// Handle user choice
if (choice == 1) {
// Manually enter the string
cout << "Enter the string: ";
cin >> inputString;
}
else if (choice == 2) {
// Load the string from the input.txt file
ifstream inputFile("input.txt");
// Check if the file is open
if (!inputFile.is_open()) {
cerr << "Error opening the input.txt file." << endl;
return 1;
}
// Read the string from the file
getline(inputFile, inputString);
}
else {
// Invalid choice, exit with an error message
cerr << "Invalid choice. Exiting." << endl;
return 1;
}
try {
// Call the add function and display the result
cout << "Result: " << add(inputString) << endl;
}
catch (const exception& e) {
// Handle exceptions and display error message
cerr << "Error: " << e.what() << endl;
return 1;
}
return 0;
}