Thread: Newbie need help

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    12

    Newbie need help

    i need to produce a program which display Odd and Even number. User must be able to enter how many time the program loop.

    example:
    Enter loop: 2

    Enter Selection: o
    Enter Input 1: 1
    Enter Input 2: 10

    output:

    number: 1 3 5 7 9
    total: 25

    Enter Selection: e
    Enter Input 1: 1
    Enter Input 2: 11

    output:

    number: 2 4 6 8 10
    total: 30

    Code:
    int main()
    {
          int i, total=0, input1, input2, counter;
          char selection;
    
          printf("Enter loop: ");
          scanf("%d", &counter);
    
          for(i=0; i<counter; i++)
          {
              printf("\nEnter selection: ");
              scanf("%s", &selection);
              printf("\nEnter input 1: ");
              scanf("%d", &input1);
              printf("\nEnter input 2: ");
              scanf("%d", &input2);
    
              switch (selection)
              {
                  case 'e':
                           if(input1 % 2 == 0)
                           {
                               printf("\nEven Number: ");
                               while(input1<=input2)
                               {
                                  printf("%d ",input1 );
                                  total = total + input1;
                                   input1+=2;
                               }
                               printf("\nTotal: %d", total);
                               printf("\n");
                           }
                           else
                           {
                               input1++;
                               printf("\nEven Number: ");
                               while(input1<=input2)
                               {
                                    printf("%d ",input1 );
                                    total = total + input1;
                                    input1+=2;
                               }
                               printf("\nTotal: %d", total);
                               printf("\n");
                           }
    
                           break;
    
                  case 'o':
                           if(input1 % 2 != 0)
                           {
                               printf("\nOdd Number: ");
                               while(input1<=input2)
                               {
                                   printf("%d ",input1 );
                                   total = total + input1;
                                   input1+=2;
                               }
                               printf("\nTotal: %d", total);
                               printf("\n");
                           }
                           else
                           {
                               input1++;
                               printf("\nEven Number: ");
                               while(input1<=input2)
                               {
                                   printf("%d ",input1 );
                                   total = total + input1;
                                   input1+=2;
                               }
                               printf("\nTotal: %d", total);
                               printf("\n");
                           }
                           break;
    
              }
           }
    
          system("pause");
          return 0;
    }
    my program always terminte after it runs once. where do i make the mistake ?

    Thank You

  2. #2
    Register User andor's Avatar
    Join Date
    Aug 2006
    Location
    Novi Sad
    Posts
    42
    this is wrong
    Code:
    scanf("%s", &selection);
    use this instead
    Code:
    getchar(); /* kludge */
    selection = getchar();
    becouse selection is single char. Read this. When you use switch always use also default.
    Last edited by andor; 10-25-2006 at 07:38 AM.

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    12
    got it. thank you for helping

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newbie: array question :(
    By cstudent in forum C Programming
    Replies: 2
    Last Post: 04-09-2008, 06:46 AM
  2. getting to grips with allegro and ms vc++ (newbie)
    By jimjamjahaa in forum C++ Programming
    Replies: 4
    Last Post: 11-18-2005, 07:49 PM
  3. Newbie in problem with looping
    By nrain in forum C Programming
    Replies: 6
    Last Post: 11-05-2005, 12:53 PM
  4. C++ newbie / linux not so newbie question
    By goldmonkey in forum C++ Programming
    Replies: 7
    Last Post: 12-13-2003, 12:27 PM
  5. Newbie Game Develpoers Unite!
    By Telenosis in forum Game Programming
    Replies: 10
    Last Post: 06-22-2002, 02:02 PM