Thread: Similar to the Loop thread just posted(help please)

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    38

    Similar to the Loop thread just posted(help please)

    Hey guys, I am new to programming, and for the sake of learning how to create loops constructively I have tried to make a very simple calculator program (below) however I am having issues making it loop or 'restart'.

    I read the Loop thread posted 2hours ago, however I didnt get any resolve so hence the new thread

    I can create 'for'/'while' and 'do while' loops but not with this switch statement inside (the program will just crash(compiles fine))

    My code is ment to;
    1. Ask for a input(number)
    2. Ask for a 2nd
    3. ask for +, -, or *
    4. Supply answer and restart
    *It works fine without the for loop.
    *This is just to understand loops more practically

    Code:
    #include <stdio.h>
    
    
    int main(int argc, char *argv[]){
        
        int numb1;
        int numb2;
        int sum;
        int choice;
        int restart;
        int restart1 = 1;
        
        for (restart = 0; restart > restart1; NULL) {
    
    
        
        printf("Enter a number:\n");
        scanf("%d", &numb1);
        
        printf("Enter a second number:\n");
        scanf("%d", &numb2);
        
        printf("Press 1 for Adding the two numbers\n");
        printf("Press 2 for Subtraction\n");
        printf("Press 3 for Multiplication\n");
        scanf("%d", &choice);
        
        
        switch (choice) {
            case 1:
                sum = numb1 + numb2;
                printf("The result is %d\n", sum);
                break;
             
            case 2:
                sum = numb1 - numb2;
                printf("The result is %d\n", sum);
                break;
            case 3:
                sum = numb1 * numb2;
                printf("The result is %d\n", sum);
                break;
            default:
                printf("You did enter a valid choice\n");
                break;
        }
        }
    }
    Thanks
    Last edited by Ryan Huddo; 04-23-2013 at 01:42 AM. Reason: Spoolling mistake.

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    First of all, you don't need NULL in your for loop - You can leave that part blank

    Second, you the loop will keep going until restart is smaller than restart1 -> But they never change values
    Fact - Beethoven wrote his first symphony in C

  3. #3
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    What you should probably do is add a do/while list

    i.e.
    Code:
    do
    {
      ...
      printf("Or push '4' to exit...");
      ...
    
    } 
    while (choice != 4);
    Fact - Beethoven wrote his first symphony in C

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    I have no idea what other thread you are referring to.

    In any event, your code with the loop works perfectly, assuming your intent is that the body of the loop is never executed.
    Code:
        int restart;
        int restart1 = 1;
         
        for (restart = 0; restart > restart1; NULL) {
    The first action of the loop is to set restart to zero. Then it checks if restart is greater than restart1 (i.e. if 0 > 1). That condition is false, so the loop body is never executed.


    The names of the variables (restart and restart1) may mean something in your mind. But they mean nothing to the compiler. All it does is look at their values (0 and 1 in your code). A compiler does not employ any logic like "a variable is named restart, so something must be restarted".
    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
    Apr 2013
    Posts
    38
    Quote Originally Posted by grumpy View Post
    I have no idea what other thread you are referring to.

    In any event, your code with the loop works perfectly, assuming your intent is that the body of the loop is never executed.
    Code:
        int restart;
        int restart1 = 1;
         
        for (restart = 0; restart > restart1; NULL) {
    The first action of the loop is to set restart to zero. Then it checks if restart is greater than restart1 (i.e. if 0 > 1). That condition is false, so the loop body is never executed.


    The names of the variables (restart and restart1) may mean something in your mind. But they mean nothing to the compiler. All it does is look at their values (0 and 1 in your code). A compiler does not employ any logic like "a variable is named restart, so something must be restarted".
    AH thanks, I had greater and less than mixed up, I am such an idiot. haha.

    And Reference you're last statement of my naming variables; yeah I do understand that, its more for ease of coding in my head. Never the less thanks for the help.

    Click_here thanks aswel, getting different options on creating loops is very helpful in my learning progress (Trying to self teach)
    Last edited by Ryan Huddo; 04-23-2013 at 02:06 AM. Reason: Spoolling mistake.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. prime numbers re-posted
    By crypto_quixote in forum C Programming
    Replies: 3
    Last Post: 01-17-2006, 08:44 PM
  2. Govtcheez Posted This On Flashdaddee
    By Dean in forum Linux Programming
    Replies: 0
    Last Post: 09-05-2002, 05:44 PM
  3. Sorry if this has been posted before, but...
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 08-06-2002, 04:39 AM
  4. posted on slashdot.org
    By ober in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 02-05-2002, 01:37 PM
  5. Hopefully this can be posted here...
    By ScrewzLuse in forum C Programming
    Replies: 6
    Last Post: 12-03-2001, 09:08 PM