Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

C Programming: Question about repeats

Status
Not open for further replies.
Hello folks. This is not a micrcontroller but since you guys know C I wanted to ask for your help here... I made a little program (my first one) and it serves well it's purpose. The problem is that when it gives the result, as soon as we hit a key on the keyboard, it exits.
I'd rather have it repeating it's main function.
Thank you very much for any help
 
Last edited:
To get it to repeat use a while(1) loop

Code:
int main(int argc, char *argv[])
{
   int choix;
   double Bpm, Result;

   // List of calculation choices
   printf(" 1. blablabla\n");
   printf(" 2. blablabla\n");
   printf(" 3. blablabla\n");

   while(1)
   {
      printf("Option #");  
      scanf("%d", &choix);          
      printf("Enter the ", choix);

      switch (choix)
      {
         case 1:
            printf("Bpm: ");
            scanf("%lf", &Bpm);
            Result = 1+Bpm;
            printf("1 + bpm = %.lf",Result);
            break;
         case 2:
            printf("Bpm: ");
            scanf("%lf", &Bpm);
            Result = 2+Bpm;
            printf("2 + bpm = %.lf",Result);
            break;
         case 3:
            printf("Bpm: ");
            scanf("%lf", &Bpm);
            Result = 3+Bpm;
            printf("3 + bpm = %.lf",Result);
            break;
         default:  // If the user enters an invalide option, the following message will show up:
            printf("The option you selected is not valid. This program will end because I don't know how to loop it \n");
      }  // end case
      getch();
      return 0;
   } // end while(1)
}

I did not fix other errors. Walk through the program like you were the computer, write down the value of each variable as it changes.

Have fun
 
Hey thank you for the tip, however I can't get it to work. It keeps shutting down after giving the result (when we hit a key)
I corrected my structure a bit tho, thanks for the model, it's a lot more readable (the program is 240 lines).

I'm having fun but I'm a super beginner noob
 
Last edited:
I'm sure 3v0 meant to put the "return 0" below the while-loop. If you copied his example directly, that's what's causing the program to exit.
 
Try this

while(1)

while(choiceix<1 && choiceix>3) {
...scanf("%d", &choiceix)
}

...

switch(choiceix)....

etc.
 
Last edited:
Looks like 3v0 put a "default" case in his example code to catch an invalid value. He just forgot to put "return 0;" in there to exit out after the error message prints.
 
Sorry I can't get it to work...

Can you compile this and correct me please ?
I got it to loop but when I enter an alphabetic value it keeps repeating till infinity

I'd just want to correct that...

Code:
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
   int choix;
   double Rpm, Result;

   // Presentation
   printf("This program will **** (censored)\n\n");

   // List of calculation choices
   printf(" 1. Hello             2. Blablabla              3. Third\n");
   printf(" 4. Another blabla\n\n");

   while(1)
   {
     printf("Option #");
     scanf("%d", &choix); 
     printf("Enter the ", choix);


     switch (choix) // choix awaits for a scanned number from the input (keyboard) at ("%d", &choix)
     {  
        case 1:
           printf("Rpm: ");
           scanf("%lf", &Rpm);
           Result = 360000/Rpm;
           printf("The value is = %.lf dollars.\n\n",Result);
           break;
        case 2:
           printf("Rpm: ");
           scanf("%lf", &Rpm);
           Result = 240000/Rpm;
           printf("A whole %.lf peanuts.\n\n",Result);
           break;
        case 3:
           printf("Rpm: ");
           scanf("%lf", &Rpm);
           Result = 160000/Rpm;
           printf("Aaaaaaaaaaaaa = %.lf turds.\n\n",Result);
           break;
        case 4:
           printf("Rpm: ");
           scanf("%lf", &Rpm);
           Result = 180000/Rpm;
           printf("Numer 4 mofo = %.lf .\n\n",Result);
           break;
        
        default: // If the user enters an invalide option, the following message will show up:
           printf("number, from 1 to 4, corresponding to what you want to know. The number you entered was not valid.\n\n");
     } // End case
   } // End while(1)
   getch();
   return 0;
}
 
Huh, yeah, all kinds of crazy happens when you type in a non-number value for that scanf. I don't know enough about the inner workings of scanf understand what's happening, but clearly it's having a very difficult time converting a letter into an int.

I played around a little and found that if you use %c instead of %d, it'll convert it into the ASCII character value. You have to drop off the upper byte for some reason, but that can be done easily with a mask. Consulting an ASCII table, you can convert an ASCII value to a numerical value by subtracting 48.

So try this:

Code:
while(1)
{
    printf("Option #");
    scanf("%c", &choix);
    choix = choix & 0x00FF;   //mask off the upper byte
    choix = choix - 48;  //subtract 48 to convert from ASCII code to numerical value
    printf("Enter the ");

    switch(choix)
    {
    ....
I'd be happy to elaborate if you need more explanation.
 
Last edited:
Just realized I'm being stupid. There's that extra byte appearing because choix is an int type. Using the %c indicates a char type. So if you cast choix as a char, you don't need to use the mask:

Code:
int main(int argc, char *argv[])
{
    char choix;
    ......
    while(1)
    {
        printf("Option #");
        scanf("%c", &choix);
        choix = choix - 48;  //subtract 48 to convert from ASCII code to numerical value
        printf("Enter the ");
 
        switch(choix)
        {
        ....
 
Status
Not open for further replies.

Latest threads

Back
Top