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;
}
Related
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
i am trying to figure out a way in esp32 to have (a list) for lack of better terms, that i could call upon as a variable input. I have a void playwav(); function. I want to use a global variable to define the file that should be played. ie playwav('alarm'). I had a case switch setup but was using the a b c method. As my code is growing (next step is going to add 71 new wav files), i am quickly realizing that method will no longer work. I have been told to do an array but for the life of me no matter what ive read i just cant grasp how to do it nor call upon the files by name. Does anyone have advice on what i should do? Any help is much appreciated.
This is what i currently have.
void playWAV (char effect, int bg1248_general_volume)
{
Serial.println("Entered playWAV");
switch (effect)
{
case 'a':
file = new AudioFileSourceSD("/wav/alarm.wav");
break;
case 'b':
file = new AudioFileSourceSD("/wav/alarmhigh.wav");
break;
case 'c':
file = new AudioFileSourceSD("/wav/alarmlow.wav");
break;
case 'd':
file = new AudioFileSourceSD("/wav/error.wav");
break;
case 'e':
file = new AudioFileSourceSD("/wav/error1.wav");
break;
case 'f':
file = new AudioFileSourceSD("/wav/noread.wav");
break;
case 'g':
file = new AudioFileSourceSD("/wav/noreadings.wav");
break;
case 'h':
file = new AudioFileSourceSD("/wav/normalrange.wav");
break;
case 'i':
file = new AudioFileSourceSD("/wav/warning.wav");
break;
case 'j':
file = new AudioFileSourceSD("/wav/warninghigh.wav");
break;
case 'k':
file = new AudioFileSourceSD("/wav/warninglow.wav");
break;
case 'l':
file = new AudioFileSourceSD("/wav/startup.wav");
break;
case 'm':
file = new AudioFileSourceSD("/wav/startup.wav");
break;
case 'n':
file = new AudioFileSourceSD("/wav/update.wav");
break;
case 'o':
file = new AudioFileSourceSD("/wav/startup_dev.wav");
}
float volumeGain = ((float)bg1248_general_volume / 100.0) * 39.0;
Serial.print("volumeGain:");
Serial.println(volumeGain);
id3 = new AudioFileSourceID3(file);
out = new AudioOutputI2S(0, 0); // Output to builtInDAC
out->SetPinout(12, 0, 2);
out->SetOutputModeMono(true);
out->SetGain(volumeGain);
wav = new AudioGeneratorWAV();
wav->begin(id3, out);
However because I used the alphabet im going to run out of cases very very soon. I want to be able to call for a specific wav file anywhere in the project preferably like playWAV(filename) or something similar.
I call the function(file) like this (example condition)
if((ns->sensSgv<=cfg.snd_alarm) && (ns->sensSgv>=0.1)) {
// red alarm state
// M5.Lcd.fillRect(110, 220, 100, 20, TFT_RED);
Serial.println("ALARM LOW");
M5.Lcd.fillRect(0, 220, 320, 20, TFT_RED);
M5.Lcd.setTextColor(TFT_BLACK, TFT_RED);
int stw=M5.Lcd.textWidth(tmpStr);
M5.Lcd.drawString(tmpStr, 159-stw/2, 220, GFXFF);
if( (alarmDifSec>cfg.alarm_repeat*60) && (snoozeRemaining<=0) ) {
playWAV('a', 100);
'a'=case 100 = volume.
Alan, this is what I currently had in my enum, i was following a tutorial but doubt i did it right. even if i did i was getting errors saying i had duplicate cases when i don't.
EDIT:
The condition is set using this:
if((ns->sensSgv<=cfg.snd_alarm) && (ns->sensSgv>=0.1))
and if you notice at the bottom there is:
if( (alarmDifSec>cfg.alarm_repeat*60) && (snoozeRemaining<=0) ) {
playWAV('a', 100);
The a corelates to the case A in the switch case. shown above. currently all the places that use the playWAV function are manually specified.
My understanding was using the alphabets in switch case will ran out soon because of large no. of files (>26). So, I implemented the logic using enum first and below is the complete code.
#include<stdio.h>
enum soundEffects {
alarm,
alarm_high,
alarm_low,
Dev_startup,
error,
error1,
noread,
noreadings,
normalrange,
startup,
startup_dev,
update,
warning,
warning_high,
warning_low,
};
void playWav (enum soundEffects effect, int paci_volume)
{
//Serial.println("Entered paciplay_day");
//printf("Entered paciplay_day\n");
switch (effect)
{
case alarm:
//file = new AudioFileSourceSD("/wav/alarm.wav");
printf("/wav/alarm.wav\n");
break;
case alarm_high:
//file = new AudioFileSourceSD("/wav/alarmhigh.wav");
printf("/wav/alarmhigh.wav\n");
break;
case alarm_low:
//file = new AudioFileSourceSD("/wav/alarmlow.wav");
printf("/wav/alarmlow.wav\n");
break;
case error:
//file = new AudioFileSourceSD("/wav/error.wav");
printf("/wav/error.wav\n");
break;
case error1:
//file = new AudioFileSourceSD("/wav/error1.wav");
printf("/wav/error1.wav\n");
break;
case noread:
//file = new AudioFileSourceSD("/wav/noread.wav");
printf("/wav/noread.wav\n");
break;
case noreadings:
//file = new AudioFileSourceSD("/wav/noreadings.wav");
printf("/wav/noreadings.wav\n");
break;
case normalrange:
//file = new AudioFileSourceSD("/wav/normalrange.wav");
printf("/wav/normalrange.wav\n");
break;
case warning:
//file = new AudioFileSourceSD("/wav/warning.wav");
printf("/wav/warning.wav\n");
break;
case warning_high:
//file = new AudioFileSourceSD("/wav/warninghigh.wav");
printf("/wav/warninghigh.wav\n");
break;
case warning_low:
//file = new AudioFileSourceSD("/wav/warninglow.wav");
printf("/wav/warninglow.wav\n");
break;
case startup:
//file = new AudioFileSourceSD("/wav/startup.wav");
printf("/wav/startup.wav\n");
break;
case update:
//file = new AudioFileSourceSD("/wav/update.wav");
printf("/wav/update.wav\n");
break;
}
}
int main()
{
//below are the 3 ways of calling your playWav() function
//1 - using direct numbers
//this will throw compilation error when compiled using cpp compilers
for (int i = 0; i<14; i++)
{
printf("enum value: %d\n", i);
playWav(i, 50);
}
//2 - directly calling using the enum
playWav(startup, 100);
//3 - Just another way of calling
enum soundEffects sound;
sound = error;
playWav(sound, 100);
return 0;
}
Also, I implemented this same logic using double array's. Please do find it below. This uses the name of the file as a parameter so altogether we can eliminate the use of switch statement.
#include<stdio.h>
void playWav (const char *filename, int paci_volume)
{
//Serial.println("Entered paciplay_day");
printf("Entered paciplay_day\n");
//file = new AudioFileSourceSD("/wav/update.wav");
printf("/wav/%s\n", filename);
}
int main()
{
const char list_of_files[][25] = { "startup.wav", "update.wav", "warning.wav", "alarm.wav" };
for (int i = 0; i<4; i++)
{
printf("name: %s\n", list_of_files[i]);
playWav(list_of_files[i], 50);
}
return 0;
}
Please omit my printf statements which I used for debugging. I hope this is useful.
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 !
I have the following enum (which later will grow larger!):
enum TrainingFilters {
NONE = 0,
GAUSS = 1,
SOBEL = 2,
FEATURE = 4
};
I have to print out string representation of the all possible combination. For now, a not-leangthy switch statement works fine, but if I add more items it will be disaster!
void Manager::setFilters(int filters)
{
QString what("Selected filters:");
switch (filters) {
case 0:
what.append(" NONE ");
break;
case 1:
what.append(" GAUSS ");
break;
case 1 | 2:
what.append(" GAUSS SOBEL ");
break;
case 2:
what.append(" SOBEL ");
break;
case 2 | 4:
what.append(" SOBEL FEATURE ");
break;
case 4:
what.append(" FEATURE ");
break;
case 1 | 4:
what.append(" GAUSS FEATURE ");
break;
case 1 | 2 | 4:
what.append(" GAUSS SOBEL FEATURE ");
break;
default:
qDebug() << "Invalid FILTERS enum received!";
return;
}
qDebug() << what;
mFilters = static_cast<TrainingFilters>(filters);
}
P.S: I have a few checkbox items in the user interface, and I should do some stuff according to the checked checkboxes. I use it like this:
var a, b,c;
cbGauss.checked ? a = 1 : a = 0;
cbSobel.checked ? b = 2 : b = 0;
cbFeat.checked ? c = 4 : c = 0;
cpManager.setFilters(a | b | c);
So my qustion is what is the best/easiest/smartest way to achieve this?
You could simply do
if (filters & 1)
what.append("GAUSS ");
if (filters & 2)
what.append("SOBEL ");
if (filters & 4)
what.append("FILTER ");
And so on. This way you can easily add new ones. Of course you have to check for zero and then add NONE.
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: .....;
}