One habit
Treat input as text first. Then convert it only after checking that the pieces exist.
This keeps your program safer when a user types extra spaces, leaves a field empty, or pastes data from a file.
Why parsing matters
Most hackathon C++ projects need some input. It may be a CSV of expenses, a timetable, a list of tasks, or commands typed in a terminal.
Bad parsing makes good logic look broken. Your repo should include sample input so reviewers can run the same case you tested.
Common input trap
Do not mix cin >> with getline without handling the leftover newline. cin >> reads one token. getline reads the rest of the current line.
If you read an int with cin, then want a full name or sentence, clear the newline first.
int n;
cin >> n;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
string title;
getline(cin, title);Parse simple CSV
For project data, keep the format simple. This example reads lines like item,category,amount and skips bad rows instead of crashing.
It uses getline with a comma delimiter, then converts the amount with stod inside a try block.
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
struct Expense {
string item;
string category;
double amount;
};
int main() {
vector<Expense> expenses;
string line;
while (getline(cin, line)) {
if (line.empty()) continue;
stringstream ss(line);
string item, category, amountText;
if (!getline(ss, item, ',')) continue;
if (!getline(ss, category, ',')) continue;
if (!getline(ss, amountText, ',')) continue;
try {
double amount = stod(amountText);
expenses.push_back({item, category, amount});
} catch (const invalid_argument&) {
cerr << "Skipping bad amount: " << line << '\n';
}
}
double total = 0;
for (const auto& e : expenses) total += e.amount;
cout << "Total: " << total << '\n';
}Ask AI carefully
You can ask ChatGPT or Gemini to generate parsing code, but give the exact input format and bad cases.
Check what it returns. Look for unchecked indexing, missing failed-read checks, and conversions that can throw without being handled.
Prompt:
Write C++17 code to read lines in this format:
item,category,amount
Skip blank lines. If amount is invalid, print an error to cerr and continue.
Do not use non-standard libraries.Sample input file
Add a small sample file to your repo. Name it something obvious, like sample_expenses.txt.
This helps another builder or internship reviewer run your project without guessing the input shape.
chai,canteen,15
bus pass,travel,450
notebook,stationery,60
wrong row,canteen,abcParsing checklist
Do each one yourself, then tap it to tick it off. The ticks are only a checklist for you: they are not marked or scored.
0 of 5 done
Do this now
Do each one yourself, then tap it to tick it off. The ticks are only a checklist for you: they are not marked or scored.
0 of 5 done