calculation of month as per Julian Calendar - c++

I'm a beginner in QT C++, and I'm trying to print Julian day on a label_j with respect the value mentioned in array list. I'm unable to do that. Please could you see and correct it. Thanks a lot in advance.
Current month is been fetch from the UTC,
if month equal jan then print 0,
month equal then print 31,
month equal March then print 59 and so on until it reach December.
void MainWindow::getJulianDay()
{
int month_arr[]={0,31,59,90,120,151,181,212,243,273,304,334};
QString January, February, March, April, May, June, July, August, September, October, November, December;
QDateTime local(QDateTime::currentDateTimeUtc());
QDateTime UTC(local.toUTC());
QString month=UTC.toString("MMMM");
switch (month)
{
case January:
month=month_arr[0];
break;
case February:
month=month_arr[1];
break;
case March:
month=month_arr[2];
break;
case April:
month=month_arr[3];
break;
case May:
month=month_arr[4];
break;
case June:
month=month_arr[5];
break;
case July:
month=month_arr[6];
break;
case August:
month=month_arr[7];
break;
case September:
month=month_arr[8];
break;
case October:
month=month_arr[9];
break;
case November:
month=month_arr[10];
break;
case December:
month=month_arr[11];
break;
default: month=invalid;
break;
ui->label_j->setText(month);
}

I see two main mistakes :
First of all, your case statement conditions are empty. You should do :
switch(variable) {
case condition1:
...
break;
case condition2:
...
break;
...
default:
...
break;
}
So, your code becomes :
switch (month);
{
case 1:
month="January";
answer=month_arr[0];
break;
case 2:
month="February";
answer=month_arr[1];
break;
...
default:
month="invalid";
break;
}
The second main problem is that ui->label_j->setText(answer); should be out of the switch loop. As it is currently, this statement will never be executed.
Edit : As noticed by m7913d, you have a lot of type errors. Review the type of your variables !

Related

VSCode compiling/running C code but not C++ [duplicate]

This question already has answers here:
How do I set up Visual Studio Code to compile C++ code?
(14 answers)
C++ Setup For VS Code [duplicate]
(1 answer)
Closed 4 months ago.
I installed, according to a few YT tutorials, the VSCode editor. I also installed the C/C++ extensions, theme and the Code runner extension, and I downloaded from sourceforge the Mingw64 folder, moved it to C:\ and then added the Path to the system variables. Despite this, the compiler can compile c but not c++ (gives me an error like "exited with code -1" or something). I searched up on google and found nothing about this. Does anybody have a solution?
I also tried changing the compiler from gcc.exe to g++.exe in the C/C++ configuration. Nothing changed.
I'm going to put the code I wrote under here. Please ignore the language, I'm not English :)
C code:
#include<stdio.h>
int main(){
int eta;
printf("Ciao, inserisci eta: ");
scanf("%d", &eta);
printf("L'eta e' %d", eta );
return 0;
}
C++ code:
#include<iostream>
using namespace std;
int main(){
float importo, rendcat, incA=0, incB=0, incD=0, mediaAg=0, mediaEd=0, sommaAg=0, sommaEd=0;
char cat, tipo, ered, nome;
int percrendcat, aliq, coeffimm, erF=0, erAg=0, erEd=0;
do{
importo=0;
rendcat=0;
percrendcat=0;
aliq=0;
coeffimm=0;
cout<<"\nInserire il nome (iniziale): ";
cin>>nome;
if(nome!='*'){
do{
cout<<"\nInserire la rendita catastale: ";
cin>>rendcat;
if(rendcat<=0){
cout<<"\nInserire una rendita maggiore di 0.";
}
}while(rendcat<=0);
do{
cout<<"\nInserire la categoria di immobile: "
<<"\n[a/A] Categoria A"
<<"\n[b/B] Categoria B"
<<"\n[d/D] Categoria D\n";
cin>>cat;
switch(cat){
case 'a':
case 'A':{
coeffimm=120;
break;
}
case 'b':
case 'B':{
coeffimm=140;
break;
}
case 'd':
case 'D':{
coeffimm=60;
break;
}
default:{
cout<<"\nOpzione non valida. ";
break;
}
}
}while(cat!=65&&cat!=66&&cat!=68&&cat!=97&&cat!=98&&cat!=100);
do{
cout<<"\nInserire il tipo di immobile: "
<<"\n[a/A] Casa"
<<"\n[b/B] Terreno agricolo"
<<"\n[c/C] Terreno edificabile\n";
cin>>tipo;
switch(tipo){
case 'a':
case 'A':{
percrendcat=5;
break;
}
case 'b':
case 'B':{
percrendcat=25;
coeffimm=90;
erAg++;
break;
}
case 'c':
case 'C':{
percrendcat=30;
coeffimm=95;
erEd++;
break;
}
default:{
cout<<"\nOpzione non valida. ";
break;
}
}
}while(tipo!='a'&&tipo!='A'&&tipo!='b'&&tipo!='B'&&tipo!='c'&&tipo!='C');
do{
cout<<"\nL'eredit"<<char(133)<<" proviene da: "
<<"\n[a/A] Genitore"
<<"\n[b/B] Fratello"
<<"\n[c/C] Altro parente\n";
cin>>ered;
switch(ered){
case 'a':
case 'A':{
aliq=5;
break;
}
case 'b':
case 'B':{
aliq=7;
erF++;
break;
}
case 'c':
case 'C':{
aliq=9;
break;
}
default:{
cout<<"\nOpzione non valida. ";
break;
}
}
}while(ered<65||(ered>67&&ered<97)||ered>99);
importo=(((rendcat+(rendcat*percrendcat/100))*coeffimm)*aliq)/100;
cout<<"\nNome del cittadino (iniziale): "<<nome;
cout<<"\nImporto da pagare: "<<importo<<" euro.";
cout<<"\nTipologia di bene selezionata: "<<tipo;
cout<<"\nValore dell'aliquota applicata: "<<aliq<<"\n";
switch(cat){
case 'a':
case 'A':{
incA=incA+importo;
break;
}
case 'b':
case 'B':{
incB=incB+importo;
break;
}
case 'd':
case 'D':{
incD=incD+importo;
break;
}
}
switch(tipo){
case 'b':
case 'B':{
sommaAg=sommaAg+importo;
mediaAg=sommaAg/erAg;
break;
}
case 'c':
case 'C':{
sommaEd=sommaEd+importo;
mediaEd=sommaEd/erEd;
break;
}
}
cout<<"\nNumero di cittadini che hanno ricevuto eredit"<<char(133)<<" da fratello: "<<erF;
cout<<"\nIncasso dello stato per immobili di categoria A: "<<incA<<" euro.";
cout<<"\nIncasso dello stato per immobili di categoria B: "<<incB<<" euro.";
cout<<"\nIncasso dello stato per immobili di categoria D: "<<incD<<" euro.";
cout<<"\nIl valore medio delle tasse pagate sui terreni agricoli ammonta a "<<mediaAg<<" euro.";
cout<<"\nIl valore medio delle tasse pagate sui terreni edificabili ammonta a "<<mediaEd<<" euro.";
}
}while(nome!='*');
cout<<"\n3B software, grazie per aver scelto i nostri programmi.";
return 0;
}

Reading CAN Bus bitmask

I have a netgain hyper9 motor and controller. Controller is hooked up to CAN bus. I am using an esp32 with mcp2515 to read the data. The format is as in the image here.
I can read a byte ok and get a number but can not correctly read the word (2bytes) nor can I get the correct bitmap.
I am using esphome to read this through a lambda function.
Are there any c++ / esphome lambda people that could help?
WORD FORMAT: All words are sent in Little-endian format which reverses the order and stores the least significant byte at the lower memory address with the most significant byte being stored at the highest memory address.
byte word format:
reading a bitmask:
reading rpm through 2 byte word:
canbus:
- platform: mcp2515
id: my_mcp2515
spi_id: McpSpi
cs_pin: GPIO14
can_id: 2
use_extended_id: false
bit_rate: 250KBPS
clock: 8MHZ
mode: NORMAL
on_frame:
- can_id: 0x301 #[canbus:066]: received can message std can_id=0x301 size=8
use_extended_id: false
then:
- lambda: |-
//State of Charge - works ok
int soc = x[2];
id(motor_battery_soc).publish_state(soc);
//RPM - word format
std::string r1 = to_string(x[0]);
std::string r2 = to_string(x[1]);
std::string rpmString = r2 + r1;
id(motor_rpm).publish_state(rpmString);
//System flags
ESP_LOGD("system_flags x3: ", "%d", x[3] );
ESP_LOGD("system_flags x4: ", "%d", x[4] );
std::string sf1 = to_string(x[3]);
std::string sf2 = to_string(x[4]);
std::string sfString = sf2 + sf1;
std::string sflag = " ";
ESP_LOGD("main", "sfString: %s", sfString);
// *** I know this is not correct because should search for a true/false flag on the bitmask ***
int sfInt = atoi(sfString.c_str());
ESP_LOGD("sfInt: ", "%d", sfInt);
switch(sfInt) {
case 0:
sflag = "SoC is Low For Traction";
id(motor_system_flags).publish_state("SoC is Low For Traction");
break;
case 1:
sflag = "SoC is Low For Hydraulic";
id(motor_system_flags).publish_state("SoC is Low For Hydraulic");
break;
case 2:
sflag = "Reverse Direction Active";
id(motor_system_flags).publish_state("Reverse Direction Active");
break;
case 3:
sflag = "Forward Direction Active";
id(motor_system_flags).publish_state("Forward Direction Active");
break;
case 4:
sflag = "Park Brake Active";
id(motor_system_flags).publish_state("Park Brake Active");
break;
case 5:
sflag = "Pedal Brake Active";
id(motor_system_flags).publish_state("Pedal Brake Active");
break;
case 6:
sflag = "Controller is in Overtemperature";
id(motor_system_flags).publish_state("Controller is in Overtemperature");
break;
case 7:
sflag = "Key Switch Overvoltage";
id(motor_system_flags).publish_state("Key Switch Overvoltage");
break;
case 8:
sflag = "Key Switch Undervoltage";
id(motor_system_flags).publish_state("Key Switch Undervoltage");
break;
case 9:
sflag = "Vehicle is Running";
id(motor_system_flags).publish_state("Vehicle is Running");
break;
case 10:
sflag = "Traction is Enabled";
id(motor_system_flags).publish_state("Traction is Enabled");
break;
case 11:
sflag = "Hydraulic is Enabled";
id(motor_system_flags).publish_state("Hydraulic is Enabled");
break;
case 12:
sflag = "Powering is Enabled";
id(motor_system_flags).publish_state("Powering is Enabled");
break;
case 13:
sflag = "Powering is Ready";
id(motor_system_flags).publish_state("Powering is Ready");
break;
case 14:
sflag = "Powering is Precharging";
id(motor_system_flags).publish_state("Powering is Precharging");
break;
case 15:
sflag = "Main Contactor Closing";
id(motor_system_flags).publish_state("Main Contactor Closing");
break;
default:
sflag = "No System Flag";
break;
}
id(motor_system_flags).publish_state(sflag);
//Fault code
int fault_code = x[5];
id(motor_fault_code).publish_state(fault_code);
//Motor Temp
int temp = x[6] - 40;
id(motor_temp).publish_state(temp);
text_sensor:
- platform: template
id: motor_system_flags
name: "Motor System Flags"
- platform: template
id: motor_rpm
name: "Motor RPM"
sensor:
- platform: template
id: motor_battery_soc
name: "Motor Battery SoC"
accuracy_decimals: 0
- platform: template
id: motor_fault_code
name: "Motor Fault Code"
- platform: template
id: motor_temp
name: "Motor Temp"
accuracy_decimals: 0

C++ #defining enum states

I am wondering if I made a good decision when I defined all states of my enum to their shorter counterparts: just to tidy up the code.Code:Enum:
enum class ESelectedCharacterState : uint8
{
SS_WantsWalk,
SS_WantsJog,
SS_WantsCrouch,
SS_WantsProne,
SS_WantsJump
};
Defining:
#define WantsWalk ESelectedCharacterState::SS_WantsWalk
#define WantsJog ESelectedCharacterState::SS_WantsJog
#define WantsCrouch ESelectedCharacterState::SS_WantsCrouch
#define WantsProne ESelectedCharacterState::SS_WantsProne
#define WantsJump ESelectedCharacterState::SS_WantsJump
Case with no #defined enum states:
switch (StateSelected)
{
case ESelectedCharacterState::SS_WantsWalk:
break;
case ESelectedCharacterState::SS_WantsJog:
break;
case ESelectedCharacterState::SS_WantsCrouch:
break;
case ESelectedCharacterState::SS_WantsProne:
break;
case ESelectedCharacterState::SS_WantsJump:
break;
default:
break;
}
Case with #defined enum states:
switch (StateSelected)
{
case WantsWalk:
break;
case WantsJog:
break;
case WantsCrouch:
break;
case WantsProne:
break;
case WantsJump:
break;
default:
break;
}
This is actually a small bit of code but I use this enum very frequently in my project.
Using a typedef would be a cleaner way to achieve this, by making your enum's type shorter.
typedef ESelectedCharacterState ESCS;

Switch statement doesn't work with enum (C++)

enum Maximum_Value{
MAXIMUM_VALUE_1 = 0,
MAXIMUM_VALUE_7 = 1,
MAXIMUM_VALUE_15 = 2,
MAXIMUM_VALUE_26 = 3,
MAXIMUM_VALUE_34 = 4
};
int value_from_function = functionetc();
switch(value_from_function){
MAXIMUM_VALUE_1: printf("MAXIMUM_VALUE_1 :%x\n",value_from_function); break;
MAXIMUM_VALUE_7: printf("MAXIMUM_VALUE_7 :%x\n",value_from_function); break;
MAXIMUM_VALUE_15: printf("MAXIMUM_VALUE_15 %x\n",value_from_function); break;
MAXIMUM_VALUE_26: printf("MAXIMUM_VALUE_26 %x\n",value_from_function); break;
MAXIMUM_VALUE_34: printf("MAXIMUM_VALUE_34 %x\n",value_from_function); break;
default: printf("default :%x\n",value_from_function);
}
The code above always seems to hit the default statement, printing "default :0" even though that should hit MAXIMUM_VALUE_1.
I've tried casting the variable in switch to no effect
I guess I should save the return value into a variable of type "Maximum_Value", but why doesn't the code work anyway?
Edit: Thanks for pointing out the awfully stupid mistake everyone :P. The root of the problem was copying coding from systemverilog, which uses 'case' as a keyword instead of 'switch', and doesn't require 'case' at the start of each case
Enumerators aren't labels but switch statements jump to labels. You use case to create a label switch statements can jump to:
case MAXIMUM_VALUE_1: ...; break;
Add case keyword then it will work.
case MAXIMUM_VALUE_1: printf("MAXIMUM_VALUE_1 :%x\n",value_from_function); break;
case MAXIMUM_VALUE_7: printf("MAXIMUM_VALUE_7 :%x\n",value_from_function); break;
case MAXIMUM_VALUE_15: printf("MAXIMUM_VALUE_15 %x\n",value_from_function); break;
case MAXIMUM_VALUE_26: printf("MAXIMUM_VALUE_26 %x\n",value_from_function); break;
case MAXIMUM_VALUE_34: printf("MAXIMUM_VALUE_34 %x\n",value_from_function); break;
default: printf("default :%x\n",value_from_function);
you are missing the case keyword before every label!
syntax of switch case is-
switch(type){
case type1: ....; break;
case type2: ....; break;
......
default: .....;
}

QmodelIndex and header data

I have a 4 tableviews in my ui screen .In my header data function for 4 views , I just need one 1 row name- temperature for view 1 and 3 and four rows with name field 1x,field 4x, field 10x, field 40x respectively.
My function is
virtual QVariant headerData(int section,Qt::Orientation orientation,
int role = Qt::DisplayRole) const
{
switch(role)
{
case Qt::DisplayRole:
switch (orientation)
{
case Qt::Vertical:
switch (m_channel)
{
case 0:
switch (section) // Range
{
case 0:
return "Temperature1";
}
case 1:
switch (section) // Range
{
case 0:
return "Field 1x range";
case 1:
return "Field 4x range";
case 2:
return "Field 10x range";
case 3:
return "Field 40x range";
}
case 2:
switch (section) // Range
{
case 0:
return "Temperature2";
}
case 3:
switch (section) // Range
{
case 0:
return "Field 1x range";
case 1:
return "Field 4x range";
case 2:
return "Field 10x range";
case 3:
return "Field 40x range";
}
But, the screen when compiled shows temperature,field 4x, field 10x, field 40x for views 1 and view 3, which I don't wont
Please help
You are missing breaks in your switch statement. For example:
switch (m_channel)
{
case 0:
switch (section) // Range
{
case 0:
return "Temperature1";
}
break; // <-- You need this.
case 1:
...
It's also generally a good idea to provide a default label for switch statements.