Thread: Arrange program?

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    67

    Arrange program?

    Hi, I have now complete my assignment for a certain programs but unfortunately my programs was too long and so many code so its cause some "messy". How could I arrange my programs so that my lecturer won't feel too headache? I have included all indentation. So, any tips will be appreciated.

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Fix line 42.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    67
    Erm...sorry. But what mean by fix line 42?

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Basically rags_to_riches is saying you've asked a question with no simple answer. 42 is a the meaning of life, the universe, and everything.

    Code that is hacked together tends to be messy, and more complex than it needs to be, because it tends to grow in arbitrary manners as the hacker simply unleashes fingers on keyboard. The biggest step you can make to simplifying your code is to actually think about the problem you are trying to solve, before you even touch your keyboard. Describe in simple words how you will solve the problem, in the simplest way possible. If your description is messy, rambling, repetitive, and hard to understand then write another description that is simpler, more concise, and without repetition. Once you have a description that you could give to a non-geeky friend without triggering chortles, use that description to guide how you write your code.

    Indentation can only help if your code is well thought out and organised. If your code starts as just a hacked-together mess (and that is what gives your lecturer headaches) indentation just gives you an indented hacked-together mess.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    67
    Thanks. Ok, now I understand, I will try to change some part in my program to make it more simple.

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    67

    Validate user input nothing.

    Hi, I want to include a validation which could validate if user input nothing and press enter key an error will prompt out told them to reenter. But I have no idea how to validate this, so I wonder how can I validate such case either in integer or character input?

    Code:
    #include <stdio.h>
    
        int main(void)
        {
            int n;
            char c;
    
            printf("Numbers: ");
            scanf("%d", &n);
    
            printf("Characters: ");
            scanf("%c", &c);
    
    
        }

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Siaw Ys View Post
    Hi, I want to include a validation which could validate if user input nothing and press enter key an error will prompt out told them to reenter. But I have no idea how to validate this, so I wonder how can I validate such case either in integer or character input?

    Code:
    #include <stdio.h>
    
        int main(void)
        {
            int n;
            char c;
    
            printf("Numbers: ");
            scanf("%d", &n);
    
            printf("Characters: ");
            scanf("%c", &c);
    
    
        }
    Pre-set the inputs to impossible values... for example if you are looking for a positive integer set your integer to -1 then when scanf() returns check the value of your integer to see if it changed. Also check the return value of scanf() which tells you how many conversions were completed.

    BUT... scanf() doesn't usually return if you just hit enter... because there was nothing for it to convert.

  8. #8
    Registered User
    Join Date
    Oct 2011
    Posts
    67
    Code:
    #include <stdio.h>
     
        int main()
        {
            int data;
    
            printf("Numbers: ");
            scanf("%d", &data); 
     
            if (data == '\n') {
    
                printf("\nNo data has input.");
            }
    
            return 0;
        }
    I try above code it still can't validate if user input empty integer...I also try '\0' . But still can't...

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Like this....

    Code:
    #include <stdio.h>
     
        int main()
        {
            int data;
    
    
    
            data = -1;
            do
              {
                 printf("Enter numbers: ");
               }
            while( scanf("%d", &data) == 0 || data < 0); 
           
    
    
     
            return 0;
        }

  10. #10
    Registered User
    Join Date
    Nov 2011
    Posts
    2
    Solution 1:

    Check the ASCII code. You can easily make a function that does that for you.


    Solution 2:
    There is also a library that does that for you.
    <ctype.h>


    Solution 3:
    get the input as a string, use "strtol" or "atoi" to convert it to long/int and if you get 0 that means they entered an invalid input.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation Fault in 4 dimensional arrange
    By Tempa in forum C Programming
    Replies: 10
    Last Post: 06-08-2011, 09:16 AM
  2. Replies: 9
    Last Post: 04-01-2011, 04:13 PM
  3. how to arrange the 361 data in 19x19 box?
    By jimmyymmij in forum C# Programming
    Replies: 10
    Last Post: 01-04-2009, 06:56 PM
  4. Arrange letters of the string alphabetically
    By ama_cuber in forum C Programming
    Replies: 16
    Last Post: 03-19-2008, 03:40 AM
  5. re arrange index of numbers
    By bazzano in forum C Programming
    Replies: 9
    Last Post: 11-03-2005, 12:15 PM