I am currently doing a supermarket management project for my uni, and one of the criteria is to have the sales timestamped with the system time when they are made. I am using the ctime library with the tm struct, and declaring it with
time_t now = time(0);
tm *ltm = localtime(&now);
at the beggining of the main(), to read the system time from that moment when the variable is declared.
Problem is, if I try to put it inside the switch I'm using it gives me cross initialization error (example and error below). And as I have it on the very beginning of the program, if I let it idle for a few minutes and do a sale, the saved hour will be outdated, which I don't want. Does anyone know how to put this working inside a switch, or a workaround for it? Searched all over google but all I could find was how to define it. Thank you!
Example of intended behaviour:
#include <ctime>
int main(){
*variables*
//----------------------
switch(menu1){
case 1:
*case code*
break;
case 2:
time_t now = time(0);
tm *ltm = localtime(&now);
*case code*
break;
case 3:
*case code*
break;
default:
*case code*
break
}
return;
}
Error im having with this implementation (it also seems to give me an error to every case below where it is implemented): Error messages