r/C_Homework • • Mar 20 '20
Writing a switch code
So my homework tells me to do the same output using if and else and then switch. So basically the program is adding one day to the input for example 20122020 will become 21122020. I've done the if and else code:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int fecha, anio, mes, dia;
cout << "Introduce una fecha de formato ddmmaaaa:" << endl;
cin >> fecha;
//Anio
anio = fecha%10000;
//Dia
dia = fecha/1000000;
//Numero de dias del mes
mes = (fecha%1000000)/10000;
if (mes==4 || mes==6 || mes==9 || mes==11) //30 dias
{
if (dia==30)
cout << 1 << "/" << mes+1 << "/" << anio <<endl;
else
cout << dia+1 << "/" << mes << "/" << anio <<endl;
}
else if (mes==2){
//Anio bisiesto
if (((anio % 4 == 0) && (anio % 100!= 0)) || (anio%400 == 0)) {
//29 dias
if (dia==29)
cout << 1 << "/" << mes+1 << "/" << anio <<endl;
else
cout << dia+1 << "/" << mes << "/" << anio <<endl;
}
//Anio estandar
else {
//28 dias
if (dia==28)
cout << 1 << "/" << mes+1 << "/" << anio <<endl;
else
cout << dia+1 << "/" << mes << "/" << anio <<endl;
}
}
else {
//31 dias
if (mes==12 && dia==31)
cout << 1 << "/" << 1 << "/" << anio +1 <<endl;
else if (dia==31 && mes!=12)
cout << 1 << "/" << mes + 1 << "/" << anio <<endl;
else
cout << dia+1 << "/" << mes << "/" << anio <<endl;
}
return 0;
}
Dia means day, mes= month, anio= year fecha=date
The thing is how do i do the switch version of the exact same output?
View all comments
Show parent comment
1
u/nattack Mar 21 '20
I'm afraid that if you know how to write a switch statement, the rest should be obvious to you. I can't give you the answer. anything further is just cheating, and not conducive to you learning how to change an if statement into a switch statement.