Thread: Make the main menu always appear at the end of the function

  1. #1
    Registered User
    Join Date
    Feb 2020
    Posts
    7

    Make the main menu always appear at the end of the function

    I created a main menu on my program, but when the function is completed, it just closes, even if I put "do" and "while".
    Also, there is something wrong in the code? I can't check it because the program closes down everytime.
    This program must take numbers in input, and it should be able to output them and make the sum of the rows and the columns, all of this trough a main menu.
    Thanks in advance.


    Code:
    #include<stdio.h>
    #include<string.h>
    int main(void)
    {
      int selection;
      int matrice[30][30];
      int i;
      int j;
      int m;
      int n;
      int MAXn;
      int vet;
    
      do {
        printf
            ("+----------------------------------------------------------------------------------------------+\n");
        printf("1) input \n");
        printf("2) view input \n3) sum rows\n4) sum colums \n9) exit\n--->");
        scanf("%d", &selection);
        if (selection == 1) {
          printf("how many numbers will be inserted? ");
          scanf("%d", &n);
    
    
          printf("input a sequence of %d numbers\n", n);
          for (i = 0; i < n; i++) {
    
            for (j = 0; j < n; j++) {
              printf("element %d: ", i + 1);
              scanf("%d", &matrice[j]);
            }
            printf("\n");
          }
    
        }
        if (selection == 2) {
          printf("the sequence is the following:\n");
          for (i = 0; i < n; i++)
            for (j = 0; i < n; j++)
              printf("element %d: %d\n", i + 1, matrice[j]);
          printf("\n");
        }
        if (selection == 3) {
          int somma;
    
          for (i = 1; i <= n; i++) {
            int somma = 0;
            for (j = 1; i <= m; i++) {
              somma = somma + matrice[j];
              printf("sum: %d", &matrice[j]);
            }
            printf("sum: %d", somma);
          }
    
    
        }
    
        if (selection == 4) {
          int somma;
          int righe;
          int colonne;
          int a[10][10];
    
          for (righe = 0; righe < i; righe++) {
            somma = 0;
            for (colonne = 0; colonne < j; colonne++) {
              somma = somma + a[colonne][righe];
            }
            printf("The sum of Column Elements in a Matrix =  %d \n", somma);
          }
    
        }
    
        if (selection > 10)         // errore
        {
          printf("your choice is not valid");
        }
        if (selection == 9) {
          printf("Exiting...");
          getc;
        }
      }
      while (selection == 9);
    
    
    
    }
    Last edited by Salem; 02-22-2020 at 11:02 PM. Reason: Removed eyebleed crayola colour scheme and font

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    This logic is wrong!

    Code:
    while(selection == 9);
    Likely you want
    Code:
    while(selection != 9);
    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Feb 2020
    Posts
    7
    Hi, thanks for the suggestion. It worked!
    I've been struggling all day to the 2, 3, and 4 functions.

    The function 2 prints random numbers in a loop.
    The function 3 doesn't print anything
    The function 4 prints just "0".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-04-2017, 11:44 PM
  2. Replies: 3
    Last Post: 06-01-2011, 03:08 AM
  3. main menu
    By bazzano in forum C Programming
    Replies: 25
    Last Post: 04-25-2007, 06:18 AM
  4. Going back to a main menu from anywhere
    By Gades in forum C Programming
    Replies: 5
    Last Post: 11-13-2001, 04:22 PM

Tags for this Thread