简体   繁体   中英

While loop syntax error will not compile even when trying to break out

I keep getting a syntax error and cannot find where i am not completing the loop. my program is to query for what to convert to then after it converts asks the user if they want to do it again and if yes, present option menu again.

Can someone tell me where i am going wrong?

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void){ 
    char answer;
    int selection;
    int again = 0;
    printf("Select Conversion:\n");
    printf("(1.)Hex to Binary\n(2.)Binary to Hex\n");
    scanf("%d", &selection);
    while (again != 1) 
    {
        if (selection ==1)
        {
            char bin2[1000], hexa2[1000];
            long int a = 0;
            printf("Enter a hex or binary value: "); 
            scanf("%s", &hexa2);
            printf("\nBinary value: ");
            while (hexa2[a])
            {
                switch (hexa2[a])
                {
                case '0':
                    printf("0000"); 
                    break;
                case '1':
                    printf("0001"); 
                    break;
                case '2':
                    printf("0010"); 
                    break;
                case '3':
                    printf("0011"); 
                    break;
                case '4':
                    printf("0100"); 
                    break;
                case '5':
                    printf("0101"); 
                    break;
                case '6':
                    printf("0110"); 
                    break;
                case '7':
                    printf("0111"); 
                    break;
                case '8':
                    printf("1000"); 
                    break;
                case '9':
                    printf("1001"); 
                    break;
                case 'A':
                    printf("1010"); 
                    break;
                case 'B':
                    printf("1011"); 
                    break;
                case 'C':
                    printf("1100"); 
                    break;
                case 'D':
                    printf("1101"); 
                    break;
                case 'E':
                    printf("1110"); 
                    break;
                case 'F':
                    printf("1111"); 
                    break;
                case 'a':
                    printf("1010"); 
                    break;
                case 'b':
                    printf("1011"); 
                    break;
                case 'c':
                    printf("1100"); 
                    break;
                case 'd':
                    printf("1101"); 
                    break;
                case 'e':
                    printf("1110"); 
                    break;
                case 'f':
                    printf("1111"); 
                    break;
                default:
                    printf("\n Invalid hexa digit %c ", hexa2[a]);
                    return 0;
                }
                a++;
            break;
            }
        }   
            
        if (selection == 2)
        {
            long int bin, hex = 0, i = 1, left;
            printf("Enter Binary Number: \n");
            scanf("%ld", &bin);
            while (bin != 0)
            {
                left = bin % 10;
                hex = hex + left * i;
                i = i * 2;
                bin = bin / 10;
            }
            printf("Hexadecimal value: %lX", hex);
        }
        printf("\nContinue(Y/N)?\n");
        scanf("%s", &answer);
        if (answer=='Y'||answer=='y')
            {
                again = again +2;
            }
            else
            {
                again=1;
            }
        }
    }
    printf("Exit");
    return 0;
    
}

I keep getting a syntax error and cannot find where i am not completing the loop. my program is to query for what to convert to then after it converts asks the user if they want to do it again and if yes, present option menu again.

Can someone tell me where i am going wrong?

The code you posted has more } than { . Remove the extra } here:

        if (answer=='Y'||answer=='y')
            {
                again = again +2;
            }
            else
            {
                again=1;
            }
        }

Also, change scanf("%s", &hexa2); to scanf("%s", hexa2); . For %s , a char * should be passed. hexa2 is an array of 1000 char ( char [1000] ), so &hexa2 is a pointer to an array of 1000 char ( char (*)[1000] ). When you use hexa2 as an argument, it will be automatically converted to a pointer to its first argument, so that will be a char * .

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void) {
    char answer;
    int selection;
    do
    {
        printf("Select Conversion:\n");
        printf("(1.)Hex to Binary\n(2.)Binary to Hex\n");
        scanf("%d", &selection);
        if (selection == 1)
        {
            char bin2[1000], hexa2[1000];
            long int a = 0;
            printf("Enter a hex value: ");
            scanf("%s", hexa2);
            printf("\nBinary value: ");
            while (hexa2[a])
            {
                switch (hexa2[a])
                {
                case '0':
                    printf("0000");
                    break;
                case '1':
                    printf("0001");
                    break;
                case '2':
                    printf("0010");
                    break;
                case '3':
                    printf("0011");
                    break;
                case '4':
                    printf("0100");
                    break;
                case '5':
                    printf("0101");
                    break;
                case '6':
                    printf("0110");
                    break;
                case '7':
                    printf("0111");
                    break;
                case '8':
                    printf("1000");
                    break;
                case '9':
                    printf("1001");
                    break;
                case 'A':
                    printf("1010");
                    break;
                case 'B':
                    printf("1011");
                    break;
                case 'C':
                    printf("1100");
                    break;
                case 'D':
                    printf("1101");
                    break;
                case 'E':
                    printf("1110");
                    break;
                case 'F':
                    printf("1111");
                    break;
                case 'a':
                    printf("1010");
                    break;
                case 'b':
                    printf("1011");
                    break;
                case 'c':
                    printf("1100");
                    break;
                case 'd':
                    printf("1101");
                    break;
                case 'e':
                    printf("1110");
                    break;
                case 'f':
                    printf("1111");
                    break;
                default:
                    printf("\n Invalid hexa digit %c ", hexa2[a]);
                    return 0;
                }
                a++;
            }
        }

        else if (selection == 2)
        {
            long int bin, hex = 0, i = 1, left;
            printf("Enter Binary Number: \n");
            scanf("%ld", &bin);
            while (bin != 0)
            {
                left = bin % 10;
                hex = hex + left * i;
                i = i * 2;
                bin = bin / 10;
            }
            printf("Hexadecimal value: %lX", hex);
        }
        printf("\nContinue(Y/N)?\n");
        scanf("%s", &answer);
    } while (answer == 'Y' || answer == 'y');
    printf("Exit");
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM