Thread: How to loop back to the beginning of the function?

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    11

    How to loop back to the beginning of the function?

    Hello, I am writing another program and I cannot find for the life of me how to loop the program back to the start of a function.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main(void)
    {
        int choice;
        char band1, band2, band3, band4;
    
    
        printf ("This program calculates the resistor value based on the colours of the wire.\n(enter the integer near the command)\n");
        printf ("What would you like to do?\n1. Calculate resistance value.\n2. Help.\n3. Exit.\n");
        scanf ("%d", &choice);
    
    
        switch (choice)
        {
        case 1:
            printf ("B=Black\nN=Brown\nO=Orange\nY=Yellow\nG=Green\n");
            printf ("L=Blue\nV=Violet\n\A=Gray\nW=White\nD=Gold\nS=Silver\n");
            printf ("What is the colour of the first, second, third, and fourth bands?\n");
            scanf ("%c", &band1);
            scanf ("%c", &band2);
            scanf ("%c", &band3);
            scanf ("%c", &band4);
            return main;
        case 2:
            printf ("This program takes the four colours of the bands of a resistor and uses them to\ncalculate the resistance of the resistor. ");
            printf ("When first entering the program, you\nare displayed with three functions: calculate the value of the resistance,\n");
            printf ("help, and exit. Help leads here, while exit exits the program. Calculating the\nfunction leads to the main part ");
            printf ("of the program. In there, you will be given a\ntable of different colours and the letter corresponding to each colour. ");
            printf ("You are instructed to enter the colours of the first, second, third (M), and fourth (T) bands. MAKE SURE to enter the ");
            printf ("capital of the letter that corresponds to each\ncolour if you want the program to work. The program will calculate ");
            printf ("the\nresistance with the allowed tolerance. Happy using the program!\n");
            return main;
        case 3:
            exit(main);
        default:
            printf ("ERROR! THIS VALUE DOES NOT CORRESPOND TO A FUNCTION! BOMBING THE HOUSE NOW!\n");
            return main;
        }
        system("pause");
        return 0;
    }
    This is my current iteration of the program. Obviously, I haven't finished writing the case 1 of the first switch, but for the other ones, I need help. I thought that return main would cause the program to loop back to the beginning after executing the case, but I see that it is not the case (pun not intended). Anyways, what would I have to insert to cause the program to loop again (other than for case 3)? And another question, why is it that in scanf in the first case, the program only accepts two characters before finishing?

    Thanks to anyone who helps.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    I thought that return main would cause the program to loop back to the beginning after executing the case
    "return" is an end, not a beginning.

    If you want to play by the right rules, then simply use the loop constructs provided by the language: For, While and Do While Loops in C - Cprogramming.com

    ...but I see that it is not the case (pun not intended).


    And another question, why is it that in scanf in the first case, the program only accepts two characters before finishing?
    Reading individual characters from stdin can be tricky. See here for an explanation: FAQ > How do I avoid a "dangling" newline when reading single character user input? - Cprogramming.com

  3. #3
    Registered User
    Join Date
    Oct 2014
    Posts
    11
    OK, fixed the scanf problem. I implemented the for loop in this manner:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main(void)
    {
        int choice;
        char band1, band2, band3, band4;
    
    
        for (choice=0;choice!=3;choice)
        {
    
    
        printf ("\nThis program calculates the resistor value based on the colours of the wire.\n(enter the integer near the command)\n");
        printf ("What would you like to do?\n1. Calculate resistance value.\n2. Help.\n3. Exit.\n");
        scanf ("%d", &choice);
    
    
        switch (choice)
        {
        case 1:
            printf ("B=Black\nN=Brown\nO=Orange\nY=Yellow\nG=Green\n");
            printf ("L=Blue\nV=Violet\n\A=Gray\nW=White\nD=Gold\nS=Silver\n");
            printf ("What is the colour of the first, second, third, and fourth bands?\n");
            scanf (" %c\n", &band1);
            scanf (" %c\n", &band2);
            scanf (" %c\n", &band3);
            scanf (" %c", &band4);
            printf ("%c %c %c %c", band1,band2,band3,band4);
            break;
        case 2:
            printf ("This program takes the four colours of the bands of a resistor and uses them to\ncalculate the resistance of the resistor. ");
            printf ("When first entering the program, you\nare displayed with three functions: calculate the value of the resistance,\n");
            printf ("help, and exit. Help leads here, while exit exits the program. Calculating the\nfunction leads to the main part ");
            printf ("of the program. In there, you will be given a\ntable of different colours and the letter corresponding to each colour. ");
            printf ("You are instructed to enter the colours of the first, second, third (M), and fourth (T) bands. MAKE SURE to enter the ");
            printf ("capital of the letter that corresponds to each\ncolour if you want the program to work. The program will calculate ");
            printf ("the\nresistance with the allowed tolerance. Happy using the program!\n");
            break;
        case 3:
            exit(main);
        default:
            printf ("ERROR! THIS VALUE DOES NOT CORRESPOND TO A FUNCTION! BOMBING THE HOUSE NOW!\n");
            break;
        }
        }
        system("pause");
        return 0;
    }
    However, the code breaks when at the starting menu, you enter a letter instead of a number (it keeps looping). How do I fix this?

  4. #4
    Registered User
    Join Date
    Sep 2014
    Location
    SE Washington State
    Posts
    65
    Use getchar() to get your input and then you can evaluate it to make sure it is the correct type of input.

    Scanf() requires a defined type or it goes into "error mode". Most of the time it just exits the program all together leaving you wondering what you did wrong.
    Last edited by PaulS; 10-11-2014 at 06:06 PM.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    3,688
    I would use fgets followed by sscanf; but, your problem would be easier to solve by changing the input to all chars.
    So, '1' instead of 1.

    Edit: Could NOT find a FAQ that does it fgets/sscanf but here one that should help.
    http://faq.cprogramming.com/cgi-bin/...wer=1043372399

    Tim S.
    Last edited by stahta01; 10-11-2014 at 06:41 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another beginning function
    By jwall in forum C Programming
    Replies: 18
    Last Post: 02-04-2013, 10:29 PM
  2. going back to beginning of file
    By afflictedd2 in forum C++ Programming
    Replies: 2
    Last Post: 01-14-2011, 09:42 AM
  3. moving valuables from classes to functions and back to beginning
    By military genius in forum C++ Programming
    Replies: 0
    Last Post: 10-08-2009, 06:54 PM
  4. Can I get this to loop back?
    By Unregistered in forum C Programming
    Replies: 9
    Last Post: 05-07-2002, 03:34 AM
  5. Going back to the beginning
    By face_master in forum C++ Programming
    Replies: 5
    Last Post: 09-16-2001, 07:06 PM

Tags for this Thread