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

Commit 6a9b83b

Browse files
committed
Finally an actual Main
I wanted to give the calculator a way to be usable
1 parent dbf8f83 commit 6a9b83b

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

C-String-Calculator-Kata.cpp

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,57 @@
11
#include <iostream>
2+
#include <fstream>
23
#include <string>
34
#include "./add.h"
45

56
using namespace std;
67

7-
int main(string numbers)
8-
{
9-
return add("0");
8+
int main() {
9+
// Display menu options
10+
cout << "Choose an option:" << endl;
11+
cout << "1. Enter the string manually" << endl;
12+
cout << "2. Load the string from the input.txt file" << endl;
13+
14+
// Get user choice
15+
int choice;
16+
cin >> choice;
17+
18+
// Variable to store input string
19+
string inputString;
20+
21+
// Handle user choice
22+
if (choice == 1) {
23+
// Manually enter the string
24+
cout << "Enter the string: ";
25+
cin >> inputString;
26+
}
27+
else if (choice == 2) {
28+
// Load the string from the input.txt file
29+
ifstream inputFile("input.txt");
30+
31+
// Check if the file is open
32+
if (!inputFile.is_open()) {
33+
cerr << "Error opening the input.txt file." << endl;
34+
return 1;
35+
}
36+
37+
// Read the string from the file
38+
getline(inputFile, inputString);
39+
}
40+
else {
41+
// Invalid choice, exit with an error message
42+
cerr << "Invalid choice. Exiting." << endl;
43+
return 1;
44+
}
45+
46+
try {
47+
// Call the add function and display the result
48+
cout << "Result: " << add(inputString) << endl;
49+
}
50+
catch (const exception& e) {
51+
// Handle exceptions and display error message
52+
cerr << "Error: " << e.what() << endl;
53+
return 1;
54+
}
55+
56+
return 0;
1057
}

input.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)