Kent Spence · August 7, 2015 Heya, so I've been learning to code in 'C' for three days and I decided to write a very basic calculator program (It's more of an abacus really), its completely functional but it has a few bugs one of them is in the loop function that decides what mathematical process will be applied, basically if in an invalid character is input by the user it should loop back to the start of that section but it doesn't ill post the code below so someone can check it out and feel free to tear it apart and tell me what I could do better or did wrong#include #include int main(){//Variables    float num, sum;    char func, select;    int mathFunc;do{//Select a number to process    do{        printf("\nType in a number : ");        scanf(" %f", &num);        printf("Is %.0f the correct number? (y/n): ", num);        scanf(" %c", &select);        if (select == 'y'){            break;        } if (select == 'n') {            select = 'n';        }else{            select = 'n';        }        }while(select == 'n');//Select Basic Math Function         do{                printf("\nWould you like to add , subtract, multiply, or divide? (a/s/m/d): ");                scanf(" %c", &func);                printf("Is %c correct? (y/n):  ", func);                scanf(" %c", &select);                if (select == 'y'){                    break;               }else {                    select = 'n';                }         }while (select == 'n');/*Selects from avaliable math operations based on user inputand verifys the integer that will be added or subtracted etc*/        do{        if (func=='a'){            printf("\nhow much would you like to add to %.0f?\n", num);           scanf(" %d", &mathFunc);           printf("you would like to add %d, is that correct? (y/n): ", mathFunc);           scanf(" %c", &select);        }else if (func=='s'){            printf("\nhow much would you like to subtract off %.0f?\n", num);           scanf(" %d", &mathFunc);           printf("you would like to subtract %d, is that correct? (y/n): ", mathFunc);           scanf(" %c", &select);        }else if (func=='d'){            printf("\nhow much would you like to divide %.0f by?\n", num);           scanf(" %d", &mathFunc);           printf("you would like to divide by %d, is that correct? (y/n): ", mathFunc);           scanf(" %c", &select);        }else if (func=='m'){            printf("\nhow much would you like to multiply %.0f by?\n", num);           scanf(" %d", &mathFunc);           printf("you would like to multiply by %d, is that correct? (y/n): ", mathFunc);           scanf(" %c", &select);        }else {            select = 'n';        }        if (select == 'y'){            break;        }        if (select == 'n'){            select = 'n';        }        else {            select = 'n';        }        }while (select == 'n');//Lets do the maths!!!!if (func == 'a'){    printf("%.0f + %d = %.2f\n\n", num, mathFunc, sum = num + mathFunc);}else if (func == 's'){    printf("%.0f - %d = %.2f\n\n", num, mathFunc, sum = num - mathFunc);}else if (func == 'd'){    printf("%.0f / %d = %.2f\n\n", num, mathFunc, sum = num / mathFunc);}else if (func == 'm'){    printf("%.0f / %d = %.2f\n\n", num, mathFunc, sum = num * mathFunc);}    printf("would you like to calculate again? (y/n): ");    scanf(" %c", &select);    if (select == 'n'){        break;    }else if (select == 'y'){        continue;    }else {    break;}    }while (select == 'y');    return 0;    }

## Replies

 Sha Kal · August 7, 2015 I can't see you code at all, i suggest you yo save the image of your code and upload it here, i have to do it same way as well, i guess there is a problem with website when you try to use " code " option and write something in it. Kent Spence · August 10, 2015 Hahaha hmmmmm yea I ended up writing a new one up which was a lot tidier still has issues though but oh well,#include #include #include #include #include int main(){        //Variables        float num, sum, number;        char sel, mathFunc;       printf("Welcome to Novus Sanctus' Calculator (more of an abacus really)\n");do{       printf("\nPick a number to process: ");       scanf(" %f", &num);      //checks what math function should be used      printf("\nWould you like to add, subtract, multiply, or divide? (a/s/m/d): ");      scanf(" %c", &mathFunc);      //math functionsdo{      if (mathFunc == 'a'){        printf("\nHow much would you like to add to %.2f?: ", num);        scanf(" %f", &number);        printf("%.2f + %.2f = %.2f", num, number, sum = num + number);        }else if (mathFunc == 's'){        printf("\nHow much would you like to subtract from %.2f: ", num);        scanf(" %f", &number);        printf("%.2f - %.2f = %.2f", num, number, sum = num -number);        }else if (mathFunc == 'm'){        printf("\nHow much would you like to multiply %.2f by?: ", num);        scanf(" %f", &number);        printf("%.2f x %.2f = %.2f", num, number, sum = num * number);        }else if (mathFunc == 'd'){        printf("\nHow much would you like to divide %.2f by?: ", num);        scanf(" %f", &number);        printf("%.2f / %.2f = %.2f", num, number, sum = num / number);        }else{        sel = 'x';        }        }while (sel =='x');        printf("\n\nWould you like to calculate again? (y/n): ");        scanf(" %c", &sel);        if (sel == 'y'){            printf("\n===========================================\n");            printf("===========================================\n");        num = 0;        sum = 0;        number = 0;        }}while (sel == 'y');  return 0;} Jon Z. · August 9, 2015 Hi, here is what I think. if (select == 'y'){ break; } //since this would exit the loop, the following lines are not needed. //the following is statement is complete unnecessary, since if select i already 'n', that's what it is, you don't need to assign it to 'n'. if (select == 'n') { select = 'n'; //so before if it was 'n', it makes it 'n'. now if it wasn't 'n', you make it 'n'. so it's gonna be 'n' no matter what. you might as well skip the if statements and say select = 'n'; }else{ select = 'n'; } //if you get to this part, select if for sure 'n'. //Since you run the loop again if select equals 'n', then all the if statements are not necessary, because if the user selected 'y' it's not going to run again. }while(select == 'n');Regarding your last sectionif (func == 'a'){ //printf("%.0f + %d = %.2f\n\n", num, mathFunc, sum = num + mathFunc); //I'd change the line above with sum = num + mathFunc; printf("%.0f + %d = %.2f\n\n", num, mathFunc, sum);}My personal preference when dealing with similar loops is with the following structure.//...while(true){ //this will run until break; or return;    //...    printf("Is %c correct? (y/n): ", func);    scanf(" %c", &select);    if(select == 'y'){break;} //this will exit the loop if select equals 'y'}Another way with more options would be//...while(true){ //this will run until break; or return; //... printf("Is %c correct? (y/n): ", func); scanf(" %c", &select); if(select == 'y'){break;} //this will exit the loop if select equals 'y' else if(select == 'n'){continue;} //this will go to the beginning of the loop else{  printf("Invalid option\n");  //do something about it }}
• 1

## C

129,997 followers