Question

http://pastebin.com/uws2Ts96 - my program so far

I'm attempting to write a C program for my class where the program will display a menu giving 3 options, option 1 will lead to the function named ExtractLargestDigit, and option 2 will lead to DigitOccurrence, and option 3 will end the program.

The problem I'm having is that i want to make it so if the user enters any value other than 1 2 or 3 the program will print out WRONG OPTION and reprint out the menu and ask for another option.

I thought the way I was going about it was correct but every time I run the program it crashes. I believe there's a problem with my while loop.

I also am having problems with the 2 functions:

For the first function I want to have the user enter in a number, for example 123456, and have the program go through each digit of the number and print out the largest digit, and at what position did it occur in the number. For example in this number (123456) the largest digit is 6 and it is in the ones digit position.

For the second function I want the user to enter in 2 numbers (123423) and (452313) and have the program break the numbers down into their digits, assign them to an array, count the number of times each digit occurs and print out a list from 1-0 with the number of times the number occur along side it.


Any thoughts on these problems would be greatly appreciated.

Was it helpful?

Solution

You must pass the address of the variable using & to scanf:

scanf("%d", &option);

That's why it crashes when you run it.

OTHER TIPS

You're using the assignment operator (=) as opposed to the comparison operator (==) in four of your while conditions:

while(option = 0) should be while(option == 0)

You also shouldn't have a ; straight after the condition, otherwise if the condition is true it'll always be true and you'll have an infinite loop.

while(option == 0); { ... } should be while(option == 0) { ... }

Otherwise the body of the loops aren't actually the body of the loops, they're completely unrelated to the loop.


Also, scanf("%d", option); will treat the value of option (0) as an address and try to store the inputted integer into the wrong spot of memory, causing undefined behaviour (most likely a crash). If it doesn't crash, option will remain as 0.

You should also check the return value of scanf to ensure that the right amount of items were inputted. That may also involve getting rid of any junk input up until the next newline (or EOF).


I also don't understand why you have all of those while loops. If you're trying to create a menu that loops, have one single loop where the termination condition is the option that indicates exit (in your case it looks like that's 3). You could have a switch statement inside that single loop to call the right function based on the value of option.

And a return; isn't necessary at the end of a void function.


EDIT: About those two functions, the common way of splitting a number into it's digits is:

WHILE num IS NOT 0:
   digit = num MOD base
   num = num DIV base

Since decimal is base 10, you'd compute the modulus (% in C) and division of num with 10. If you wanted binary digits, you'd use a base of 2. Then just do whatever you need to with each digit at every iteration. The digits will obviously be extracted starting from the end (least significant digit).

Lastly, turn up the warnings on your compiler so it'll tell you about the problems above, ie:

gcc -Wall -o test test.c
test.c: In function ‘displayProgramMenu’:
test.c:44:2: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘int’ [-Wformat]
test.c:46:2: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
test.c:48:2: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
test.c:49:2: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
test.c:50:2: warning: suggest parentheses around assignment used as truth value [-Wparentheses]

And (lastly + 1) you can get rid of conio.h and replace getch() with getchar() or getc(stdin).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top