4

I continue to learn programming in C, and today I met a problem. In my program, a user must enter a value of time in minutes, and my program will calculate it seconds(very simple, actually). But I want to set a rule, that time cannot be negative. So I used this code:

    if(a<=0)
    {
        printf("Time cannot be equal to, or smaller than zero, so the program will now terminate\n");
        exit(EXIT_FAILURE);
    }

but now, I don`t want to terminate my program, I want it to return to the state when a user has to enter a value.

I had a problem with terminating my program, but some search helped me, however I did not get any result searching how to restart my program.

This is the text of my program(I am working on Linux):

#include<stdio.h>
#include<stdlib.h>
main()
{
    float a;
    printf("\E[36m");
    printf("This program will convert minutes to seconds");
    getchar();
    printf("Now enter your time in minutes(e.g. 5):");
    scanf("%f", &a);
    printf("As soon as you will press the Enter button you`ll get your time in seconds\n");
    getchar();
    getchar();


    if(a<=0)
    {
        printf("Time cannot be equal to, or smaller than zero, so the program will now terminate\n");
        printf("\E[0m");
        exit(EXIT_FAILURE);
    }
    else
    {
        float b;
        b=a*60;
        printf("\E[36m");
        printf("The result is %f seconds\n", b);
        printf("Press Enter to finish\n");
        getchar();
    }
    printf("\E[0m");
}

P.S. I don`t know how to correctly name this function, so I call it restart, maybe it has a different name?

abelenky
  • 63,815
  • 23
  • 109
  • 159
454b
  • 133
  • 2
  • 6
  • 12

3 Answers3

4

Both the solutions that have been posted work, but I personally like this approach better:

// ...
printf("Now enter your time in minutes(e.g. 5):");
scanf("%f", &a);

while(a <= 0){
   printf("Time cannot be equal to, or smaller than zero, please enter again: ");
   scanf("%f", &a);
}

I think it is more clear, and it gives the opportunity to have an error message and a regular message independent of each other.

Gordon Bailey
  • 3,881
  • 20
  • 28
  • thanks, just a second, I'm trying it. looks like I understood what for 'while' is used – 454b Jul 30 '12 at 16:44
  • THANKS!!!! It worked, and I understood why(I think it's the most important thing :D) – 454b Jul 30 '12 at 16:47
  • Great, I'm glad I could help, and you're right, understanding why is the most important thing! Happy coding :) – Gordon Bailey Jul 30 '12 at 16:48
  • one more question... which way I can increase your reputation? :D upps, I did it already :D and I understood it too :D – 454b Jul 30 '12 at 16:49
0

You can simply use a do ... while loop (including your program source code).

do {
    /* Get user input. */
} while (a <= 0);

Or the goto statement too to emulate a loop (discouraged with beginners).

 start:
    /* Get user input. */
    if (a <= 0)
        goto start;
md5
  • 23,373
  • 3
  • 44
  • 93
  • first way I think is another type of 'if'? because it says 'do' /this/ while 'the input is invalid' – 454b Jul 30 '12 at 16:26
  • but I need to return to the state where you have to enter a value, 'if' the input is invalid – 454b Jul 30 '12 at 16:27
  • So, it seems to be right. You can translate this `do ... while` loop with : "While the input is invalid, enter a value". – md5 Jul 30 '12 at 16:29
0

you could try if-else where:

do
{
/* get user input*/
if (a > 0)
    {
     /* ... */
    }
else
   printf ("Time cannot be negative, please re-enter") 
}while(<condition>);

*condition may be until when you want to continue.

heretolearn
  • 6,387
  • 4
  • 30
  • 53
  • interesting solution, will try it now. but I would be very happy, if there is a way to make program to ask you to enter a value again after an invalid input – 454b Jul 30 '12 at 16:29
  • i have updated the code.You could just get the user input within the do while loop before checking for the condition. – heretolearn Jul 30 '12 at 16:31
  • please, look to my code in first post, I want to replace 'exit(EXIT_FAILURE)' with a code that will make program to ask you to enter a value again – 454b Jul 30 '12 at 16:35
  • This will make the program to ask for the value until your while condition is false. – heretolearn Jul 30 '12 at 16:59