labs-edaf30/lab4/string_cast_test.cc

30 lines
849 B
C++
Raw Permalink Normal View History

2024-12-11 15:54:56 +01:00
#include <iostream>
#include <string>
#include "date.h" // För Date-klassen
#include "string_cast.h" // Inkludera string_cast
int main() {
try {
// Testa string_cast med int
int i = string_cast<int>("123");
std::cout << "Integer: " << i << std::endl;
// Testa string_cast med double
double d = string_cast<double>("12.34");
std::cout << "Double: " << d << std::endl;
// Testa string_cast med Date
Date date = string_cast<Date>("2015-01-10");
std::cout << "Date: " << date << std::endl;
// Testa ogiltig konvertering
int invalid = string_cast<int>("abc");
std::cout << "Invalid conversion: " << invalid << std::endl;
} catch (const std::invalid_argument& e) {
std::cout << "Error: " << e.what() << std::endl;
}
return 0;
}