-1

I'm trying to make a program that can read and count how many digits a number has, this is my code so far

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

int main(){
   int a, b=0;
   printf("Hey welcome to how many digits your number has!\n");
   xyz:
   printf("To begin enter a Number = ");
   scanf("%d",&a);

   while (a!=0) {
    a/=10;
    ++b;
   }

   printf("your number has %d digits\n",b);
   int choice;
   printf("do you want to use another number?\nIf yes enter \"1\"\nif no enter \"2\"\n");
   scanf("%d",&choice);

   if (choice==1){
    goto xyz;
   }

   else if (choice==2){
    return 0;
   }

return 0;
}

This works great for the first time around but when I go back up and repeat it seems that value of 'b' from previous attempt has been stored.. how can I start again without it storing the value of the variable 'b' and keep the value of b = 0?

2 Answers2

2

This is because your goto doesn't include an initialisation of b = 0

xyz:
b = 0;

I strongly suggest you forget the goto keyword. It easily leads to unreadable and undebuggable code. Try using a loop instead :

int main()
{
   int choice = 1;
   while (choice == 1)
   {
       int a, b = 0;
       printf("Hey welcome to how many digits your number has!\n");
       printf("To begin enter a Number = ");
       scanf("%d", &a);

       while (a != 0)
       {
           a /= 10;
           ++b;
       }

       printf("your number has %d digits\n",b);
       //removed declaration of choice
       printf("do you want to use another number?\nIf yes enter \"1\"\nif no enter \"2\"\n");
       scanf("%d", &choice);
    }
    return (0);
}
D.Go
  • 171
  • 9
Cid
  • 14,968
  • 4
  • 30
  • 45
  • thank you so much! exactly what was looking for thanks! – Joshua Shawns Dec 06 '18 at 10:28
  • `do ... while` would catch the corner case when `0` is entered. – Weather Vane Dec 06 '18 at 10:30
  • 1
    I'd advice against `return (0);` instead of `return 0;`. It makes it look like a function call. See this: https://stackoverflow.com/questions/161879/parenthesis-surrounding-return-values – Jabberwocky Dec 06 '18 at 10:33
  • @Jabberwocky I always have been used to use parenthesis for the return value. As stated in the accepted answer of the link, *"The parenthesis became a habit and it stuck."* – Cid Dec 06 '18 at 10:39
0

The mistake is most obviously that b is not re-initialized to 0 inside the part within the xyz label. Hence the values just keep adding up instead of starting the count for a new input. so the fix would be:

xyz:
b = 0;

But it's advised not to use goto as it tends to create confusing code and may lead to infinite loops. Refer to this article below:

Why should you avoid goto?

Use while or do-while instead... as follows:

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

int main(){
   int a, b=0, choice; //declared choice here
   printf("Hey welcome to how many digits your number has!\n");
   do {
   b = 0;
   printf("To begin enter a Number = ");
   scanf("%d",&a);

   while (a!=0) {
    a/=10;
    ++b;
   }
             // removed declaration of choice from here and placed it along with other declarations in main()
   printf("your number has %d digits\n",b);
   printf("do you want to use another number?\nIf yes enter \"1\"\nif no enter \"2\"\n");
   scanf("%d",&choice);
   }while(choice == 1); //repeat the process again if user inputs choice as 1, else exit

return 0;
}
Harshith Rai
  • 3,018
  • 7
  • 22
  • 35