0

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

  • best to post the full code and actual error message. there is no reason not being able to get current time within a switch statement, so likely something specific to your code. – Ronan Hughes May 30 '20 at 13:44
  • Just updated it, was a little confusing to make it clear because the code itself is really big, tried to put the essential, and also put a picture with the error messages it gives me, hope it helps – Luis Gaspar May 30 '20 at 14:18

2 Answers2

1

So, after taking a look at your errors, I found that someone had a similar problem in this question. I'd guess that you have some kind of similar error (I can't tell because you've hidden the content of the cases, sorry). As a result, you could fix your error by scoping your cases inside brackets so you eliminate the chance of using uninitialized variables.

For example:

This snippet won't work due to a redefinition of i:

#include <iostream>

int main()
{
    int foo = 1;
    switch(foo) {
      case 1:
        int i = 42;
        std::cout << i << '\n';
        break;
      case 2:
        int i = 33; // redefinition of i so the snippet doesn't compile
        std::cout << i * 2 << '\n';
    }
}

This can be fixed by simply adding brackets so i goes out of scope before you redefine it:

#include <iostream>

int main()
{
    int foo = 1;
    switch(foo) {
      case 1: {
        int i = 42;
        std::cout << i << '\n';
        break;
      }
      case 2: {
        // i now can be defined since the previous one is out of scope
        int i = 33;
        std::cout << i * 2 << '\n';
      }
    }
}

Hopefully, that makes sense, feel free to correct or clarify with me (I'm pretty new to this)

0

You can also separate such utilities into functions. Below is a basic date to string function which works in switch.

Better still, put in it's own header and you can re-use outside of the main cpp file.

#include <ctime>
#include <iostream>
#include <chrono>

std::string now_string() {
 auto timenow = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());

return ctime(&timenow);
};

int main(){

int menu1;
std::cin >> menu1;

switch(menu1){
    case 1:
        break;
    case 2:
        std::cout << now_string() << std::endl;
        break;
    case 3:
        break;
    default:
        break;
}
return 0;
}
Ronan Hughes
  • 198
  • 1
  • 1
  • 10