Thread: help with simple :)

  1. #1
    Registered User Mike Garber's Avatar
    Join Date
    Sep 2013
    Posts
    59

    Talking help with simple :)

    >>I am trying to make a menu for my programming class and i'm having trouble making it compile, can someone help me??<<

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    int main(void)
    {
        int num_trains, max_train_length, track_length, train_length;
        int avail_track_for_trains, avail_small_cars, small_cars, num_people;
        int true_train_cars, true_train_length, amount_of_trains;
        int tl, max_track, win_people, n_cars, max_tl;
        int best_car, c_car_length;
        int n, repeat, i;
        char str1[50], str2[50];
        
        printf("Type in which choice you want: 1, 2, 3, or 4");
        scanf("%d" , &n);
        
        while(n!=4)
        {
                   if(n==1)
                   {
                           int main(void)
    {
        int num_trains, max_train_length, track_length, train_length;
        int avail_track_for_trains, avail_small_cars, small_cars, num_people;
        int true_train_cars, true_train_length, max_track_length, amount_of_trains;
           
       //Ask for user input. Max track length and max train length
        printf("What is the total length of the track?\n");
        scanf("%d" , &track_length);
       
        printf("What is the maximum length you want the trains to be?\n");
        scanf("%d" , &max_train_length);
        
       //Calculations
        avail_track_for_trains = track_length*.25;
       
        small_cars = (max_train_length-10)/8;
       
        true_train_cars = small_cars+1;
       
        true_train_length = small_cars*8+10;
       
        amount_of_trains = avail_track_for_trains/true_train_length;
       
        num_people = true_train_cars*amount_of_trains*4;
        
       
       //Display results
        printf("You can fit %d people on your trains.\n" , num_people);
        getchar();
        while(getchar()!='\n');
        return 0;
    }
                   }
                   
                   else if(n==2)
                   {
                        int main(void)
    {
        int tl, num_people, max_track, win_people, n_cars, max_tl, num_trains;
        int best_car, c_car_length;
       
        float ratio_array[1000], ratio;
       
        n_cars=0;
        win_people=0;
        
        //Prompt user for maximum train length and maximum track length
        printf("What is the maximum track length?\n");
        scanf("%d" , &max_track);
       
        printf("What is the maximum train length?\n");
        scanf("%d" , &max_tl);
       
       //Run through all possible train combinations
        for(tl=10; tl<=max_tl; tl+=8)
        {
            n_cars++;
            num_trains=(.25*max_track)/tl;
            num_people=n_cars*num_trains*4;
           
            if(num_people>win_people)
            {
                win_people=num_people;
                best_car=n_cars;
            }
            c_car_length=tl*num_trains;
            ratio=(1.0*num_people)/c_car_length;
            ratio_array[n_cars-1]=ratio;
        }
       
       //Display Results
        printf("You can fit %d people on your trains with %d "
               "cars per train.\n" , win_people, best_car);
       
        printf("Your ratio is %f.\n" , ratio);
        
        getchar();
        while(getchar()!='\n')
        return 0;
    }
                   }
                   
                   else if(n==3)
                   {
                        int main(void)
    {
        int n, repeat, i;
        char str1[50], str2[50];
        
        //Prompt user for strings for repeat test
        printf("How many times do yoo want your program to run?\n");
        scanf("%d" , &n);
        
        printf("What are the %d last names you want to check for repeats?\n" , n);
        scanf("%s" , str1);
        
        //Set Flag
        repeat=0;
        
        //Compare string 1 with the rest of the strings
        for(i=1; i<=n-1; i++)
        {
                 scanf("%s" , str2);
                 
                 if(strcmp(str1, str2)==0)
                                 repeat=1;
        }
        
        //Display Output
        if(repeat==1)
        printf("Your first word repeats.\n");
         
        else
        printf("Your first word doesnt repeat.\n");
        
        while(getchar()!='n');
        getchar();
        return 0;
    }

  2. #2
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    One thing, calling int main(void) in your code is actually you trying to re-define your main program.

    Instead, just use
    Code:
    main();
    to recursively call your main function. Or if you want, because main returns an int, you can use something like
    Code:
    if (!main()) { /* Code goes here */ }
    and that way you can have a form of error handling.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You should not be recursively calling your main function at all.

    You need to be using looping mechanisms to display the menu and process the user input.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    @Mike Garber
    Your indentation needs work. It's hard for me to figure out which loops and if statements are inside what other loops and if statements. If it's hard to read your code, it's easy to make mistakes and hard to find or fix them. Good indentation/formatting is one of the easiest ways to make your life as a programmer (and ours helping you) much easier.

    Don't use magic numbers. Instead, #define some constants.

    You should be careful about redefining all those variables. Any variables you declared, e.g. on lines 24-26, "shadow" any variables by the same name that you declared on lines 7-13. To elaborate, when you read track_lenth on line 30, it changes the track_length you defined on line 24, but NOT the one you defined on line 7.

    Can you narrow down the compilation problem? Maybe provide error messages and line numbers (copy-pasted so they're exact, please)? You should not have written 141 lines before you tried compiling. Come up with a plan (with paper and pencil) and test your plan. Once that's done, start coding. Write small bits (5-10 lines), compile, fix all errors and warnings (make sure your compiler is set to max warning level), and test that bit of code. That helps you localize and track down any compilation issues.

    Approach this in a logical, compartmentalized fashion. Start with a function to get a menu choice. The first thing it does is just print the menu. Get that working. Then read the user input. Get that working. Then validate the user input: was it an integer? Was it between 1-4 inclusive? If not, inform the user and re-ask them for input. Hopefully you get the idea.

    You should also try to break your code up into functions. When you have sections of repeated code, move it into a function. That will give you only one place to make/find/fix a mistake, plus it will shorten your code, turning several lines into a one-line function call. Along with sections of repeated code, you should break up long sections of code into functions that are smaller and more maintainable. I use a "one screenful" rule of thumb, so basically I try to keep functions to around 50-75 lines.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    A development process
    I'm wondering how much typing you did before spotting that the edifice you were creating didn't stand a cat in hells chance of compiling, never mind running.

    You can't just write 100's of lines of code off the bat, then dump it on a forum for someone else to fix.

    Start again, and take it SLOW. Write a few lines and compile. Do NOT add more code if what you just wrote fails to compile.
    Every few iterations, make sure the code runs within the limits of what you expect to happen.
    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.

  6. #6
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Try breaking the code up into functions :/

  7. #7
    Registered User Mike Garber's Avatar
    Join Date
    Sep 2013
    Posts
    59
    I have 3 programs in int main(void and they all work.. All I want to do now is print out a menu to choose program1, program2, program3, or 4quit by using if statements to run parts if my code.. I still need help I dont know why they will all work individually but not together in if statements..

    This one works fine. (Choice 1):
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    int main(void)
    {
        int num_trains, max_train_length, track_length, train_length;
        int avail_track_for_trains, avail_small_cars, small_cars, num_people;
        int true_train_cars, true_train_length, max_track_length, amount_of_trains;
           
       //Ask for user input. Max track length and max train length
        printf("What is the total length of the track?\n");
        scanf("%d" , &track_length);
       
        printf("What is the maximum length you want the trains to be?\n");
        scanf("%d" , &max_train_length);
        
       //Calculations
        avail_track_for_trains = track_length*.25;
       
        small_cars = (max_train_length-10)/8;
       
        true_train_cars = small_cars+1;
       
        true_train_length = small_cars*8+10;
       
        amount_of_trains = avail_track_for_trains/true_train_length;
       
        num_people = true_train_cars*amount_of_trains*4;
        
       
       //Display results
        printf("You can fit %d people on your trains.\n" , num_people);
        getchar();
        while(getchar()!='\n');
        return 0;
    }

    This one works fine. (Choice 2):
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    int main(void)
    {
        int tl, num_people, max_track, win_people, n_cars, max_tl, num_trains;
        int best_car, c_car_length;
       
        float ratio_array[1000], ratio;
       
        n_cars=0;
        win_people=0;
        
        //Prompt user for maximum train length and maximum track length
        printf("What is the maximum track length?\n");
        scanf("%d" , &max_track);
       
        printf("What is the maximum train length?\n");
        scanf("%d" , &max_tl);
       
       //Run through all possible train combinations
        for(tl=10; tl<=max_tl; tl+=8)
        {
            n_cars++;
            num_trains=(.25*max_track)/tl;
            num_people=n_cars*num_trains*4;
           
            if(num_people>win_people)
            {
                win_people=num_people;
                best_car=n_cars;
            }
            c_car_length=tl*num_trains;
            ratio=(1.0*num_people)/c_car_length;
            ratio_array[n_cars-1]=ratio;
        }
       
       //Display Results
        printf("You can fit %d people on your trains with %d "
               "cars per train.\n" , win_people, best_car);
       
        printf("Your ratio is %f.\n" , ratio);
        
        getchar();
        while(getchar()!='\n')
        return 0;
    And this one works fine (choice 3):
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    int main(void)
    {
        int n, repeat, i;
        char str1[50], str2[50];
        
        //Prompt user for strings for repeat test
        printf("How many times do yoo want your program to run?\n");
        scanf("%d" , &n);
        
        printf("What are the %d last names you want to check for repeats?\n" , n);
        scanf("%s" , str1);
        
        //Set Flag
        repeat=0;
        
        //Compare string 1 with the rest of the strings
        for(i=1; i<=n-1; i++)
        {
                 scanf("%s" , str2);
                 
                 if(strcmp(str1, str2)==0)
                                 repeat=1;
        }
        
        //Display Output
         if(repeat==1)
         printf("Your first word repeats.\n");
         
         else
         printf("Your first word doesnt repeat.\n");
         
         while(getchar()!='n');
         getchar();
         return 0;
    }
    But together they fail inside if statements. How come?

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    What's stopping you from renaming main in each of those programs to something else that actually describes what the function does, then putting them all in one file and calling them from that file's main function, which contains just your simple menu code and a switch/if-else if statement? Don't try to mash it all into some giant, ugly if statement. Break it up into sub-functions like I suggested.

  9. #9
    Registered User Mike Garber's Avatar
    Join Date
    Sep 2013
    Posts
    59
    I figured it out guys, thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple program, simple problem
    By KAUFMANN in forum C Programming
    Replies: 5
    Last Post: 02-16-2011, 01:16 PM
  2. Simple Socket Library Not so simple
    By jbully in forum C Programming
    Replies: 4
    Last Post: 12-23-2010, 09:23 AM
  3. Simple program...simple problem?
    By deadherorising in forum C Programming
    Replies: 2
    Last Post: 03-12-2009, 08:37 PM
  4. Simple program, not so simple problem
    By nolsen in forum C++ Programming
    Replies: 2
    Last Post: 01-18-2008, 10:28 AM
  5. Replies: 1
    Last Post: 07-20-2002, 06:33 PM