Program has stopped working problem.

This is a discussion on Program has stopped working problem. within the C Programming forums, part of the General Programming Boards category; Here is my entire code: This is not for any homework or anything, just messing around but can't spot this ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    1

    Program has stopped working problem.

    Here is my entire code: This is not for any homework or anything, just messing around but can't spot this problem.

    Code:
    #include <stdio.h>
    
    void displayMenu();
    void fillArray(float array[], int n, int i);
    //float computeMean(arguments here);
    //float computeStandardDeviation(arguments here);
    //float computeMode(arguments here);
    //float computeRange(arguments here);
    void restartProgram(char);
    
    int main()
    {
    int n = 0;
    int i = 0;
    int input = 0;
    int const MAX_DATA = 20;
    int invalid = 1;
    int temp = 1;
    char var;
    
    do
    {
    
    do
    {
    printf("Please give a number of inputs: ");
    scanf("%d", &n);
    
    if(n > 0 && n <= MAX_DATA)
    {
    invalid = 0;
    }
    else
    {
    printf("\nThat was a wrong choice. Please try again.\n\n");
    }
    }
    while(invalid);
    
    invalid = 1;
    
    float data[n];
    fillArray(data, n, i);
    //displayMenu();
    
    //do
    //{
    
    displayMenu();
    
    do
    {
    printf("Please choose an operation: ");
    scanf("%d", &input);
    
    if(input > 0 && input < 6)
    {
    invalid = 0;
    }
    else
    {
    printf("\nThat was a wrong choice. Please try again.\n\n");
    }
    }while(invalid);
    
    invalid = 1;
    
    switch(input)
    {
    case 1:
    //computeMean(arguments here);
    //Print Here
    break;
    case 2:
    //computeStandardDeviation(arguments here);
    //Print Here
    break;
    case 3:
    //computeMode(arguments here);
    //Print Here
    break;
    case 4:
    //computeRange(arguments here);
    //Print Here
    break;
    case 5:
    restartProgram(var);
    break;
    }
    
    }while(1);
    
    system("pause");
    return 0;
    }
    
    void displayMenu()
    {
    printf("Statistical Calculator Menu:\n\n");
    printf("(1) Mean\n");
    printf("(2) Standard Deviation\n");
    printf("(3) Mode\n");
    printf("(4) Range\n");
    printf("(5) Restart / Exit\n");
    }
    
    void fillArray(float data[], int n, int i)
    {
    for(i = 0; i < n; i++)
    {
    printf("Please enter input %d: ", i + 1);
    scanf("%f", &data[i]);
    }
    
    printf("\nThank you.\n\n");
    }
    
    //float computeMean(arguments here){}
    
    //float computeStandardDeviation(arguments here){}
    
    //float computeMode(arguments here){}
    
    //float computeRange(arguments here){}
    
    void restartProgram(char answer)
    {
    printf("Would you like to enter another data set (Y/N)?: ");
    scanf("%s", &answer);
    
    switch(answer)
    {
    case 'Y': case 'y':
    printf("Restarting Program...");
    break;
    case 'N': case 'n':
    printf("\nExiting Program...\n\n");
    system("pause");
    exit(0);
    break;
    }
    }
    Input I have been using is:

    3
    1
    2
    3
    5
    y

    I get Restarting Program... and then "Program has stopped working."
    Last edited by Salem; 10-05-2012 at 11:03 AM. Reason: Restored

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    DON'T DELETE YOUR QUESTION!!! Just edit it (or make another post) saying you (think you) solved it.
    My response relates to the code that used to be there....

    Why do you pass i to fillArray? You're not using it as a parameter but as a local variable, so declare it as such (in fillArray) and remove it from the parameter list. You've made the same mistake with restartProgram. Don't use parameters as local variables!

    Your use of the "invalid" flag is amateurish and error-prone and can be easily replaced like this:
    Code:
        while (1)
        {
             printf("Please give a number of inputs: ");
             scanf("%d", &n);
             if(n > 0 && n <= MAX_DATA)
                 break;
             else
                 printf("\nThat was a wrong choice. Please try again.\n\n");
        }
    And in restartProgram, you're using the string format to read a character (this is probably the source of your problem). Change the scanf to
    Code:
    scanf(" %c", &answer);  // note the space before %c (to eat the newline left by previous scanf's)
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,678
    Quote Originally Posted by johnatrik View Post
    I figured it out.
    Can you say what was the problem because it can be useful for many

    Also you had an unused variable temp

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program has stopped working...
    By asediugeneral in forum C Programming
    Replies: 4
    Last Post: 09-03-2012, 11:42 AM
  2. Replies: 3
    Last Post: 07-19-2012, 07:15 PM
  3. ****.exe has stopped working
    By kawaikx15 in forum C Programming
    Replies: 10
    Last Post: 11-19-2011, 06:38 AM
  4. .exe has stopped working
    By bluesky16 in forum C++ Programming
    Replies: 8
    Last Post: 02-21-2011, 11:58 AM
  5. Replies: 4
    Last Post: 03-26-2008, 08:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21