Converting normal numbers into roman numerals using switch statement in C - c

so i did dis little activity from school works fine with only if else's but then my professor told me to convert it to switch which is giving me a hard time coz when i run some numbers like 11 are out putted as only I instead of XI pls halp me :((((
#include<stdio.h>
#include<conio.h>
int main()
{
int R,M,D,C,L,X,V,I,H=0;
char roman_string= ' ';
printf("\n\nEnter number here: ");
scanf("%d",&R);
M=R/1000;
L=R%1000%500%100/50;
X=R%1000%500%100%50/10;
V=R%1000%500%100%50%10/5;
I=R%1000%500%100%50%10%5/1;
if(R>3000){
roman_string= 'A';
}
if(M==3){
roman_string= 'B';
}
if(M==2){
roman_string= 'C';
}
if(M==1){
roman_string= 'D';
}
D=R%1000/500;
if((D==1) && (C==4)){
roman_string= 'E';
}
if ((D==1) && (C!=4)){
roman_string='F';
}
if((D==0) && (C==4)){
roman_string= 'G';
}
C=R%1000%500/100;
if(C==3){
roman_string= 'H';
}
if(C==2){
roman_string= 'I';
}
if(C==1){
roman_string= 'J';
}
if((L==1) && (X==4)){
roman_string= 'K';
}
if((L==1) && (X!=4)){
roman_string='L';
}
if((L==0) && (X==4)){
roman_string='M';
}
if(X==1){
roman_string='N';
}
if(X==2){
roman_string='O';
}
if(X==3){
roman_string='P';
}
if((V==1) &&(I!=4)){
roman_string='Q';
}
if(I==1){
roman_string='R';
}
if(I==2) {
roman_string='S';
}
if(I==3) {
roman_string='T';
}
if((I==4) && (V%2==1)) {
roman_string='U';
}
if((I==4) && (V%2==0)){
roman_string='V';
}
switch (roman_string)
{
case 'A' :
printf("Invalid Input");
break;
case 'B':
printf("MMM");
break;
case 'C':
printf("MM");
break;
case 'D':
printf("M");
break;
case 'E':
printf("CM");
break;
case 'F':
printf("D");
break;
case 'G':
printf("CD");
break;
case 'H':
printf("CCC");
break;
case 'I':
printf("CC");
break;
case 'J':
printf("C");
break;
case 'K':
printf("XC");
break;
case 'L':
printf("L");
break;
case 'M':
printf("XL");
break;
case 'N':
printf("X");
break;
case'O':
printf("XX");
break;
case 'P':
printf("XXX");
break;
case 'Q':
printf("V");
break;
case 'R':
printf("I");
break;
case 'S':
printf("II");
break;
case 'T':
printf("III");
break;
case 'U':
printf("IX");
break;
case 'V':
printf("IV" );
break;
}
getch();
return 0;
}

Any time you see a bunch of if statements piled up that just compare the same variable to different integer constants like so:
if(x == 1) {
do_1();
}
else if(x == 2) {
do_2();
}
else if(x == 3) {
do_3();
}
/// etc.
else {
do_default();
}
You can change them into a switch statement:
switch(x) {
case 1:
do_1();
break;
case 2:
do_2();
break;
case 3:
do_3();
break;
/// etc.
default:
do_default();
}

Related

C - "if" statement will never be executed [-Wswitch-unreachable] in a switch statement

So i am trying to make an switch statement that checks if the user has not already entered that switch statement before and I am trying to do this with an if statement inside the switch, but it always ignores the if statement and always just completes the case which is inside it, disregarding the other arguments, but what is also interesting is that it changes a to bijis, but doesnt do the actual condition check:
char bijis = '*';
char izv;
printf("\nChoose the next station ");
scanf("%d",&izv);
switch(izv) {
if (a != bijis) {
case 'a': case 'A':
a = bijis;
jaut_a();
break;
}
if (b != bijis) {
case 'b': case 'B':
b = bijis;
jaut_b();
break;
}
if (c != bijis) {
case 'c': case 'C':
c = bijis;
jaut_c();
break;
}
if (d != bijis) {
case 'd': case 'D':
d = bijis;
jaut_d();
break;
}
if (e != bijis) {
case 'e': case 'E':
e = bijis;
jaut_e();
break;
}
if (k1 != bijis) {
case '1':
k1 = bijis;
jaut_k1();
break;
}
if (k2 != bijis) {
case '2':
k2 = bijis;
jaut_k2();
break;
}
if (k3 != bijis) {
case '3':
k3 = bijis;
jaut_k3();
break;
}
if (k4 != bijis) {
case '4':
k4 = bijis;
jaut_k4();
break;
}
if (k5 != bijis) {
case '5':
k5 = bijis;
jaut_k5();
break;
}
if (k6 != bijis) {
case '6':
k6 = bijis;
jaut_k6();
break;
}
if (k7 != bijis) {
case '7':
k7 = bijis;
jaut_k7();
break;
}
if (k8 != bijis) {
case '8' :
k8 = bijis;
jaut_k8();
break;
}
if (k9 != bijis) {
case '9' :
k9 = bijis;
jaut_k9();
break;
}
default:
clearscr();
printf("Kļūdaina ievade, šajā lauciņā jau esi bijis! Ieraksti jebkuru burtu vai skaitli,kas rādīts tabulā.\n (Zvaigznītes parāda vietas kur jau esi bijis.");
sleep(3.5);
karte_plans();
break;
} ;
Your case labels are inside of the if blocks. The switch jumps directly to the relevant label, so the condition is jumped over.
Put the condition after the label.
switch(izv) {
case 'a': case 'A':
if (a != bijis) {
a = bijis;
jaut_a();
}
break;
case 'b': case 'B':
if (b != bijis) {
b = bijis;
jaut_b();
}
break;
...

Declaring a variable gives an error, makes me think there's something else I'm missing

I have a function in C that compiles fine until I add another case to my switch statement and declare a variable.
Here's the function
void evaluate(int num_inputs) {
struct gate *ptr = gatehead;
while (ptr->next != NULL) {
switch (ptr->kind) {
case 0:
if (get_input_value(ptr->params[0]) && get_input_value(ptr->params[1])) {
write_to_output(true, ptr->output);
} else {
write_to_output(false, ptr->output);
}
break;
case 1:
if (get_input_value(ptr->params[0]) || get_input_value(ptr->params[1])) {
write_to_output(true, ptr->output);
} else {
write_to_output(false, ptr->output);
}
break;
case 2:
if (!(get_input_value(ptr->params[0]) && get_input_value(ptr->params[1]))) {
write_to_output(true, ptr->output);
} else {
write_to_output(false, ptr->output);
}
break;
case 3:
if (!(get_input_value(ptr->params[0])) && !(get_input_value(ptr->params[1]))) {
write_to_output(true, ptr->output);
} else {
write_to_output(false, ptr->output);
}
break;
case 4:
if ((get_input_value(ptr->params[0]) || get_input_value(ptr->params[1])) && !(get_input_value(ptr->params[0]) && get_input_value(ptr->params[1]))) {
write_to_output(true, ptr->output);
} else {
write_to_output(false, ptr->output);
}
break;
case 5:
if (get_input_value(ptr->params[0])) {
write_to_output(false, ptr->output);
} else {
write_to_output(true, ptr->output);
}
break;
case 6:
if (get_input_value(ptr->params[0])) {
write_to_output(true, ptr->output);
} else {
write_to_output(false, ptr->output);
}
break;
case 7:
char* d = "DECODER";
printf("%s", d);
break;
}
ptr = ptr->next;
}
}
If I comment out case 7, everything works fine. Even keeping the print and break compiles, but declaring the char* causes the error "expected expression"
The problem is that the case 7: label is immediately followed by a variable declaration.
case 7:
char* d = "DECODER";
A label can only be applied to a statement, and a declaration is not considered a statement. You can get around this by adding an empty statement before the case label:
case 7:
;
char* d = "DECODER";

Convert a string from argv to unsigned byte array

For example i need an
unsigned code[] {
0x0001,
0x0002,
0x0003,
.. and so on
}
from argv. I would like to input it as a stream like this:
000100020003 and so on
unsigned hextodigit(int hex)
{
unsigned result = 0;
switch(toupper(hex))
{
case '0':
return 0;
break;
case '1':
return 1;
break;
case '2':
return 2;
break;
case '3':
return 3;
break;
case '4':
return 4;
break;
case '5':
return 5;
break;
case '6':
return 6;
break;
case '7':
return 7;
break;
case '8':
return 8;
break;
case '9':
return 9;
break;
case 'A':
return 10;
break;
case 'B':
return 11;
break;
case 'C':
return 12;
break;
case 'D':
return 13;
break;
case 'E':
return 14;
break;
case 'F':
return 15;
break;
default:
break;
}
}
unsigned fromhex4chars(const char *str)
{
return hextodigit(*(str + 3)) + (hextodigit(*(str + 2)) << 4) + (hextodigit(*(str + 1)) << 8) + (hextodigit(*str) << 12);
}
void convert(const char *str, unsigned *buff)
{
size_t len = strlen(str) / 4;
while(len--)
{
*buff++ = fromhex4chars(str);
str += 4;
}
}
int main(int argc, char **argv)
{
unsigned x[100];
convert(argv[1], x);
return 0;
}

Comments(/*...*/) longer than one line

I am supposed to count the /*...*/ comments in a file and I was wondering if this code is right or am I missing something out?
My Code
void commentsLongerThanOneLine(FILE* inputStream, FILE* outputStream) {
int count = 0, c, state = 1;
while ((c = fgetc(inputStream) != EOF)) {
switch (state) {
case 1: switch (c) {
case '/': state = 2; break;
}
break;
case 2: switch (c) {
case '/': state = 3; break;
case '*': state = 4; break;
default: state = 1;
}
break;
case 3: switch (c) {
case '\n': state = 1; break;
default: state = 4; count++;
}
break;
case 4:switch (c) {
case'*': if (c == '\n') count++; break;
default: state = 4; if (c == '\n') count++;
}
}
}
fprintf(outputStream, "Comments longer than one line: %d\n", count);
}

Variable is printed properly right before an if else loop, but does not retain that value one in the if else loop

I've written a program that reads an account value from a text file and then assigns these values to a variable for each account. Once this is done, reading from the same text file, an action to be done to each account value is identified in the format "Account# Actiontype ActionModifier". All the correct values are being scanned and the proper values are being stored when I insert printfs to check, but immediately after the correct value is printed, the value is put through an else if loop. When doing this for some reason the value is being read as 1 no matter what and is executing the actions in the else if loop as if it were equal to 1.
loopState = 1;
while(loopState != 0)
{
for(i = 1;i < 100 ;i++)
{
if(i < 6)// This executes fine
{
fscanf(bankfile,"%f",&accValue);
switch(i)
{
case 1:
acc1 = accValue;
break;
case 2:
acc2 = accValue;
break;
case 3:
acc3 = accValue;
break;
case 4:
acc4 = accValue;
break;
case 5:
acc5 = accValue;
printf("Done module one\n");
break;
}
}
else // scan for the the first value, than the second, then the third
{
fscanf(bankfile,"%d",&accNum);
if(accNum != 0)
{
fscanf(bankfile,"%c",&accAction);
if(accAction == 'W' || accAction == 'D')
{
fscanf(bankfile,"%f",&actValue);
printf("%d %c %.2f\n",accNum,accAction,actValue);//this will be correct
printf("%d\n",accNum); //this will print say "2"
if(accNum = 1)//but this if statement will be run
{//thus printing the below printf
printf("If accNum = 1 this will print\n");
switch(accAction)
{
case 'W':
final1 = withdrawl(acc1,actValue);
break;
case 'D':
final1 = deposit(acc1,actValue);
break;
}
}
else if(accNum = 2)
{
switch(accAction)
{
case 'W':
final2 = withdrawl(acc2,actValue);
break;
case 'D':
final2 = deposit(acc2,actValue);
break;
}
}
else if(accNum = 3)
{
switch(accAction)
{
case 'W':
final3 = withdrawl(acc3,actValue);
break;
case 'D':
final3 = deposit(acc3,actValue);
break;
}
}
else if(accNum = 4)
{
switch(accAction)
{
case 'W':
final4 = withdrawl(acc4,actValue);
break;
case 'D':
final4 = deposit(acc4,actValue);
break;
}
}
else if(accNum = 5)
{
switch(accAction)
{
case 'W':
final5 = withdrawl(acc5,actValue);
break;
case 'D':
final5 = deposit(acc5,actValue);
break;
}
}
}
else if(accAction == 'B' || accAction == 'U')
{
printf("%d %c\n",accNum,accAction);//this will be correct
printf("%d\n",accNum);//this will print say "4"
if(accNum = 1)//but this if statement will be run
{//thus printing this printf below
printf("If accNum = 1 this will print\n");
switch(accAction)
{
case 'B':
final1 = balance(acc1);
break;
case 'U':
final1 = update(acc1);
break;
}
}
else if(accNum = 2)
{
switch(accAction)
{
case 'B':
final2 = balance(acc2);
break;
case 'U':
final2 = update(acc2);
break;
}
}
else if(accNum = 3)
{
switch(accAction)
{
case 'B':
final3 = balance(acc3);
break;
case 'U':
final3 = update(acc3);
break;
}
}
else if(accNum = 4)
{
switch(accAction)
{
case 'B':
final4 = balance(acc4);
break;
case 'U':
final4 = update(acc4);
break;
}
}
else if(accNum = 5)
{
switch(accAction)
{
case 'B':
final5 = withdrawl(acc5,actValue);
break;
case 'U':
final5 = deposit(acc5,actValue);
break;
}
}
}
}
else
loopState = 0;
}
}
}
printf("Exited Loop");
}
From my understanding I can't see where the value is being altered if just the line before it is being seen as the correct value
Your conditional statements are doing assignment.
else if (accNum = 3)
should be
else if (accNum == 3)

Resources