problem in C

This is a discussion on problem in C within the C Programming forums, part of the General Programming Boards category; I am new to using C language and also to programing and would like some help on the following problem. ...

  1. #1
    esi
    esi is offline
    Registered User
    Join Date
    Nov 2006
    Posts
    18

    problem in C

    I am new to using C language and also to programing and would like some help on the following problem.

    Write a function to take integer input from user: If user input 1 then the program prints ONE and if user input 2 then program print TWO. If user input is neither 1 nor 2 then program prints: DEFAULT VALUE.
    The program should ask user whether he want to repeat the programe again. If student enters ‘y’ then program is repeated and if student enters ‘n’ then program is terminated.
    Note:
    Use while selection statement to decide the repetition of program and use switch multiple selection statement to print ONE or TWO or DEFAULT VALUE based on the user input.

    im having problem in repeating the whole thing. can anyone help me in this case. the source cde ive writen is as follows:
    Code:
    #include<stdio.h>
    int main()
    {
    int a;
    char b;
    char y;
    char n;
    char counter;
    y=3;
    printf("Enter Number \n");
    scanf("%d",&a);
    switch (a)
    {
    case 1:
    printf("\n You Entered one \n");
    break;
    
    case 2:
    printf("\n You Entered Two \n");
    break;
    default:
    printf("default");
    }
    printf("do u want to repeat?\n");
    scanf("%d",&counter);
    while (counter=y)
    {
    printf("Enter Number \n");
    scanf("%d",&a);
    switch (a)
    {
    case 1:
    printf("\n You Entered one \n");
    break;
    
    case 2:
    printf("\n You Entered Two \n");
    break;
    default:
    printf("default");
    }
    }
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    1. Moved from C# to C board
    2. Code tags added
    You might want to edit your post to make the code indented.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    You want to englobe your whole switch statement with a while loop. So you'd do something like :

    Code:
    char continue ='y';
    
    while(continue == 'y')
    {
              //  Do your stuff
    
             printf("Do you want to continue ?");
             c = getchar();  // Or a combination of fgets and sscanf.
    }
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  4. #4
    Registered User
    Join Date
    Jun 2006
    Posts
    75
    It's good that you showed us your attempt.

    Here are some suggestions for improving your code:
    1. Use meaningful variable names.
    2. Understand the difference between == (a comparison operator) and = (the assignment operator).
    3. Try to reduce repetition in codes that you develop.
    4. Use proper indentation to make your code more readable.

    This is what your code should look like after putting the above suggestions in use:
    Code:
    #include<stdio.h>
    int main()
    {
      int number;
      char answer='y';
      
      while (answer =='y' || answer == 'Y')
      {
        printf("Enter Number\n");
        scanf("%d",&number);
        
        switch (number)
        {
        case 1:
          printf("You Entered one\n");
          break;            
        case 2:
          printf("You Entered Two\n");
          break;
        default:
          printf("default\n");
        }
        
        printf("do u want to repeat?\n");
        scanf(" %c", &answer); /*The space before %c tells scanf to ignore
                                 any white space (like the newline) left in the input*/
      }
    }

  5. #5
    esi
    esi is offline
    Registered User
    Join Date
    Nov 2006
    Posts
    18
    thanx a lot for helping me out. i understand my mistakes and working now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

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