Thread: Help with first C program

  1. #1
    Registered User
    Join Date
    Apr 2018
    Posts
    14

    Unhappy Help with first C program

    Hi. I'm new here and also a newbie programmer.
    I'm taking a course in C and I'm having trouble with my assignment
    I'm suppose to create a menu to do a couple of things, one of them is to make the menu print again every time the user inputs '1' into the variable I've assigned. problem is it does reprint but only once, when I input '1' again it quit the program.

    Code:
    #define _CRT_SECURE_NO_WARNINGS#include <stdio.h>
    void main(){
    	int choice; 
    	printf("The menu:\n");
    	printf("0. Exit.\n");
    	printf("1. Print menu again.\n");
    	printf("2. Number details in range.\n");
    	printf("3. Layers square printing.\n");
    	printf("Please enter your choice :\n");
    	scanf("%d", &choice);
    	if (choice == 0){
    		printf("bye bye!\n");
    	}
    	while (choice == 1){
    		printf("The menu:\n");
    		printf("0. Exit.\n");
    		printf("1. Print menu again.\n");
    		printf("2. Number details in range.\n");
    		printf("3. Layers square printing.\n");
    		printf("Please enter your choice :\n");
    		scanf("%d", &choice);
    	}
    BTW, I'm not allowed to use switch or almost anything so I have to make it minimal..
    Also, the 2nd option is supposed to take a number and print all the number up to it and wether it's a prime number, a palindrome number and a few more.. I know how to check wether a number is prime or not but I'm having trouble printing a list and have it check each number and also print it's description.. Thanks in advance

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,436
    main returns int, not void - despite what your compiler will let you get away with.

    Perhaps start with this.
    Code:
    int main(){
        int choice; 
        do {
            printf("The menu:\n");
            printf("0. Exit.\n");
            printf("1. Print menu again.\n");
            printf("2. Number details in range.\n");
            printf("3. Layers square printing.\n");
            printf("Please enter your choice :\n");
            scanf("%d", &choice);
        } while ( choice != 0 );
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 03-15-2016, 06:35 AM
  2. Replies: 4
    Last Post: 12-21-2015, 07:17 AM
  3. Replies: 2
    Last Post: 09-09-2014, 02:36 PM
  4. Replies: 1
    Last Post: 03-03-2009, 04:47 PM
  5. Replies: 18
    Last Post: 11-13-2006, 01:11 PM

Tags for this Thread