Thread: Can anyone figure out whats wrong with this.

  1. #1
    Registered User
    Join Date
    Jun 2017
    Posts
    1

    Can anyone figure out whats wrong with this.

    Code:
    #include<stdio.h>
    void main(void)
    {
        int selection;
    
        printf("1) Energy Consumption Of Selected Year For A Selected Sector\n");
        printf("2) Yearly Average Energy Consumption\n");
        printf("3) Lowest Energy Consumption In Each Year\n");
        printf("4) Percentage Of Energy Consumption For Each Sector From 2010 to 2013\n");
        printf("5) Highest Energy Consumption And Its Corresponding Year For a Selected Sector\n");
        printf("6) Two Highest Energy Consumption Among All Sectors from 2010 to 2013\n");
        printf("Please enter 1,2,3,4,5 or 6\n");
    
        scanf_s("%d", &selection);
    
        switch (selection)
        {
        case 1:    
        {
            int selection;
    
            printf("1) EC in Industrial-Related\n");
            printf("2) EC in Commerce And Service-Related\n");
            printf("3) EC in Transport-Related\n");
            printf("4) EC in Household\n");
            printf("5) EC in Others\n");
            printf("Please enter 1,2,3,4 or 5\n");
    
            scanf_s("%d", &selection);
    
        }
        switch (selection)
        {
        case 1:
        {
    
            int selection;
    
            printf("1) 2010\n");
            printf("2) 2011\n");
            printf("3) 2012\n");
            printf("4) 2013\n");
            printf("Please enter 1,2,3 or 4\n");
    
            scanf_s("%d", &selection);
        }
        switch (selection)
        {
            case 1:
            printf("The Energy Consumption Is 11760.7\n");
            break;
            case 2:
            printf("The Energy Consumption Is 18059.1\n");
            break;
            case 3:
            printf("The Energy Consumption Is 18578.0\n");
            break;
            case 4:
            printf("The Energy Consumption Is 18830.1\n");
            break;
    
        }
        break;
    
        case 2:
        {
    
            int selection;
    
            printf("1) 2010\n");
            printf("2) 2011\n");
            printf("3) 2012\n");
            printf("4) 2013\n");
            printf("Please enter 1,2,3 or 4\n");
    
            scanf_s("%d", &selection);
        }
        switch (selection)
        {
        case 1:
            printf("The Energy Consumption Is 15165.4\n");
            break;
        case 2:
            printf("The Energy Consumption Is 15176.4\n");
            break;
        case 3:
            printf("The Energy Consumption Is 16136.4\n");
            break;
        case 4:
            printf("The Energy Consumption Is 16605.6\n");
            break;
        }
        break;
    
        case 3:
        {
    
            int selection;
    
            printf("1) 2010\n");
            printf("2) 2011\n");
            printf("3) 2012\n");
            printf("4) 2013\n");
            printf("Please enter 1,2,3 or 4\n");
    
            scanf_s("%d", &selection);
        }
        switch (selection)
        {
        case 1:
            printf("The Energy Consumption Is 2143.3\n");
            break;
        case 2:
            printf("The Energy Consumption Is 2278.6\n");
            break;
        case 3:
            printf("The Energy Consumption Is 2391.0\n");
            break;
        case 4:
            printf("The Energy Consumption Is 2370.0\n");
            break;
        }
        break;
    
        case 4:
        {
    
            int selection;
    
            printf("1) 2010\n");
            printf("2) 2011\n");
            printf("3) 2012\n");
            printf("4) 2013\n");
            printf("Please enter 1,2,3 or 4\n");
    
            scanf_s("%d", &selection);
        }
        switch (selection)
        {
        case 1:
            printf("The Energy Consumption Is 6647.5\n");
            break;
        case 2:
            printf("The Energy Consumption Is 6494.1\n");
            break;
        case 3:
            printf("The Energy Consumption Is 6641.0\n");
            break;
        case 4:
            printf("The Energy Consumption Is 6766.4\n");
            break;
        }
        break;
    
        case 5:
        {
    
            int selection;
    
            printf("1) 2010\n");
            printf("2) 2011\n");
            printf("3) 2012\n");
            printf("4) 2013\n");
            printf("Please enter 1,2,3 or 4\n");
    
            scanf_s("%d", &selection);
        }
        switch (selection)
        {
        case 1:
            printf("The Energy Consumption Is 534.9\n");
            break;
        case 2:
            printf("The Energy Consumption Is 459.0\n");
            break;
        case 3:
            printf("The Energy Consumption Is 454.3\n");
            break;
        case 4:
            printf("The Energy Consumption Is 350.8\n");
            break;
        }
        break;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,112
    Well I can see a few things wrong
    - it uses void main where it should be int main
    - it is badly indented
    - you have multiple nested definitions of a variable called 'selection'.

    In order to tell you what's wrong, you need to say
    - what you type in
    - what you get
    - what you expected.
    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. Cant Figure Out Whats Wrong!
    By Ashfaqur Rahman in forum C Programming
    Replies: 5
    Last Post: 03-15-2013, 01:44 PM
  2. Replies: 10
    Last Post: 08-09-2012, 12:48 PM
  3. i have a segfault bug and i cant figure out whats wrong
    By ericfode in forum C Programming
    Replies: 3
    Last Post: 03-29-2010, 11:28 PM
  4. i cannot figure out whats wrong with this!!
    By MOH123 in forum C++ Programming
    Replies: 4
    Last Post: 12-11-2005, 05:27 PM
  5. cant figure out whats wrong with this ....
    By ii3ejoe in forum C++ Programming
    Replies: 17
    Last Post: 10-22-2001, 12:47 PM

Tags for this Thread