I am having a problem with this project I am working on. The assignment is to create an application that will give a user the choice of displaying the roman numeral equivalence of a number between 1 and 10 or displaying a count of roman numerals from 1 to 10.

program should not end before the user decides to end it
must use the if, while, for and switch structures in code
and output format should resemble:

"This program can display any roman # between 1 and 10.
This program can also count from 1 to 10 in roman #

enter the # 1 to display the roman equivalent of a number
enter #2 to count from 1 to 10 in roman numerals
enter any other # to end application

enter ur choice.... 1

enter any # from 1 to 10: 7

roman numeral VII Decimal 7

and it should loop back to enter 1 or 2 options

than enter ur choice 2

it should list roman numeral I-X and decimal 1-10 but in 2 separate rows.

This is what i have so far but I'm stuck. My first if statement keeps looping and i just cant think of what i should do for the second part. Help would be greatly appreciated.

#include <stdio.h>

int main( void )
{
	int choice;
	int num;
     
	
	printf("This program can display any Roman numeral between 1 and 10.\n");
	printf("This program can also count from 1 to 10 in Roman numeral.\n\n");



	printf("Enter the number 1 to display the Roman equivelent of a number.\n");
	printf("Enter the number 2 to count from 1 to 10 in Roman numerals.\n");
	printf("Enter any other number to end the application.\n\n");

	printf("Enter your choice.\n");
	scanf("%d\n", &choice);

	while ( choice == 1 || choice == 2 )

		if ( choice == 1 ){
			printf( "enter any number from 1 to 10\n" );
			scanf( "%d\n", &num );

			switch ( num ){

				case '1':
					printf( "%s%s\n", "Roman\n Numeral", "Decimal");
					printf( "%s\n" , "I"  );
					break;

				case '2':
					printf( "%s%s\n", "Roman\n Numeral", "Decimal");
					printf( "%s\n" , "II"  );
					break;

				case '3':
					printf( "%s%s\n", "Roman\n Numeral", "Decimal");
					printf( "%s\n" , "III"  );
					break;

				case '4':
					printf( "%s%s\n", "Roman\n Numeral", "Decimal");
					printf( "%s\n" , "IV"  );
					break;

				case '5':
					printf( "%s%s\n", "Roman\n Numeral", "Decimal");
					printf( "%s\n" , "V"  );
					break;

				case '6':
					printf( "%s%s\n", "Roman\n Numeral", "Decimal");
					printf( "%s\n" , "VI"  );
					break;

				case '7':
					printf( "%s%s\n", "Roman\n Numeral", "Decimal");
					printf( "%s\n" , "VII"  );
					break;

				case '8':
					printf( "%s%s\n", "Roman\n Numeral", "Decimal");
					printf( "%s\n" , "VIII"  );
					break;

				case '9':
					printf( "%s%s\n", "Roman\n Numeral", "Decimal");
					printf( "%s\n" , "IX"  );
					break;

				case '10':
					printf( "%s%s\n", "Roman\n Numeral", "Decimal");
					printf( "%s\n" , "X"  );
					break;

			}
		}
		
	        if ( choice == 2 )

			for ( num = 1; num <= 10; num++)
				printf( "%s%s\n", "Roman\n Numeral", "Decimal");
				

	return 0;

		}

Dani AI

Generated

Quick diagnosis for (and building on ): two things are causing the behavior you described. Using scanf formats with a trailing \n makes scanf wait for a non-whitespace character (which feels like "enter twice"), and your menu choice must be read inside the loop so the loop can actually change state. Also avoid putting the header print inside every case, and use numeric case labels (not character literals like '1').

Use line-based input (fgets + sscanf) or scanf("%d",&x) without the trailing \n. Validate sscanf return values and check ranges (1..10). Replace the long switch with a small lookup array for roman numerals; that makes option 2 trivial (print the roman row once, then the decimal row).

Example (keeps input robust and prints two neat rows):

#include <stdio.h>

int main(void) {
    const char *roman[] = { "", "I","II","III","IV","V","VI","VII","VIII","IX","X" };
    char buf[100];
    int choice, num;

    while (1) {
        puts("Menu: 1-convert  2-count  other-quit");
        if (!fgets(buf, sizeof buf, stdin)) break;
        if (sscanf(buf, "%d", &choice) != 1) break;

        if (choice == 1) {
            puts("Enter number 1..10:");
            if (!fgets(buf, sizeof buf, stdin)) break;
            if (sscanf(buf, "%d", &num) != 1 || num < 1 || num > 10) {
                puts("Invalid number");
                continue;
            }
            printf("%-10s %s\n", "Roman", "Decimal");
            printf("%-10s %d\n", roman[num], num);
        } else if (choice == 2) {
            int i;
            printf("%-10s", "Roman");
            for (i = 1; i <= 10; ++i) printf("%-6s", roman[i]);
            putchar('\n');
            printf("%-10s", "Decimal");
            for (i = 1; i <= 10; ++i) printf("%-6d", i);
            putchar('\n');
        } else break;
    }
    return 0;
}

Checklist:

  • Remove \n from scanf formats or switch to fgets+sscanf.
  • Read choice inside the loop (so it can change).
  • Validate numeric input and ranges.
  • Use an array for roman strings to simplify printing the two rows.
  • Print headers once, not inside each case.

These changes address the "enter twice" issue, the single-number output, and make option 2 print two aligned rows.

Recommended Answers

All 3 Replies

In your while loop, you never change the choice variable to something else then 1.
It is testing for choice==1 or choice==2 so it will keep looping forever.

Okay I edited my code and now the first part works correctly except the user has to input 1 twice before it prompts the user to enter number to convert and no matter what number the user inputs its only showing conversion for the number 1. Also I am having a block on how to start part two. Heres new code

#include <stdio.h>

int main( void )
{
    int choice;
    int num;

    do{
    printf("This program can display any Roman numeral between 1 and 10.\n");
    printf("This program can also count from 1 to 10 in Roman numeral.\n\n");



    printf("Enter the number 1 to display the Roman equivelent of a number.\n");
    printf("Enter the number 2 to count from 1 to 10 in Roman numerals.\n");
    printf("Enter any other number to end the application.\n\n");

    printf("Enter your choice:");
    scanf("%d\n", &choice);



        if ( choice == 1 ){
            printf( "enter any number from 1 to 10\n" );
            scanf( "%d\n", &num );

            switch ( num ){

                case 1:
                    printf( "%7s%21s\n", "Roman\nNumeral", "Decimal");
                    printf( "%1s%21s\n" , "I", "1"  );
                    break;

                case 2:
                    printf( "%7s%21s\n", "Roman\nNumeral", "Decimal");
                    printf( "%1s%21s\n" , "II", "2"  );
                    break;

                case 3:
                    printf( "%7s%21s\n", "Roman\nNumeral", "Decimal");
                    printf( "%1s%21s\n" , "III","3" );
                    break;

                case 4:
                    printf( "%7s%21s\n", "Roman\nNumeral", "Decimal");
                    printf( "%1s%21s\n" , "IV", "4"  );
                    break;

                case 5:
                    printf( "%7s%21s\n", "Roman\nNumeral", "Decimal");
                    printf( "%1s%21s\n" , "V","5"  );
                    break;

                case 6:
                    printf( "%7s%21s\n", "Roman\nNumeral", "Decimal");
                    printf( "%1s%21s\n" , "VI","6"  );
                    break;

                case 7:
                    printf( "%7s%21s\n", "Roman\nNumeral", "Decimal");
                    printf( "%1s%21s\n" , "VII", "7"  );
                    break;

                case 8:
                    printf( "%7s%21s\n", "Roman\nNumeral", "Decimal");
                    printf( "%1s%21s\n" , "VIII","8"  );
                    break;

                case 9:
                    printf( "%7s%21s\n", "Roman\nNumeral", "Decimal");
                    printf( "%1s%21s\n" , "IX", "9"  );
                    break;

                case 10:
                    printf( "%7s%21s\n", "Roman\nNumeral", "Decimal");
                    printf( "%1s%21s\n" , "X","10" );
                    break;


            }
        }



            if ( choice == 2 )

            for ( num = 1; num <= 10; num++)
                printf( "%s%s\n", "Roman\n Numeral", "Decimal");
    }   while ( choice == 1 || choice == 2 );
            return 0;

        }

set choice to 3 before the while-loop
do the scanf in the
while loop
1--func 1
2--func 2
3--quit the while loop , do something like while (choice != 3) etc.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.