code not returning to start

hello there. if a quick question about my code, i seem to have a problem at the end and i cant solve it. i was hopeing that after the program was finished its calculation it would give me a option to press Y to carry on or N to exit, i can get they options but when i press either of them they do noting and just freeze up the program, can anyone see where I'm after messing up. thanks in advanced

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <stdio.h>

void calc (long); 

int main ()
{
int removelastnumber,  number, mvalue, i, sumnumber=0, next , finalnumber;
char choice;

  {
    sumnumber = 0;
    do 
        {
           long value;
           printf ("Enter number above zero\n");
	       scanf("%d",&number);
	       
        }
     while (number<0 ); // number has to be greather then zero 
     mvalue = number*9;   // multiply by 9 
     removelastnumber = mvalue/10; // removes last number
     while (removelastnumber > 0) //  greather then zero 
        {
           sumnumber += removelastnumber % 10; 
	       removelastnumber = removelastnumber/10; //devide my 10 again 
        }
     for (i=0; i<sumnumber+9;i+=9)  //multiplication of 9 closes to sum
        {
	       next = i;
        }
     finalnumber=next-sumnumber;
     printf("The last number enterd  %d multiplied by 9 is %d\n " ,number, finalnumber ); //gives out the wanted number
     getchar();
     do
        {
           printf("To go again      press Y\n"); // send back to start 
           printf("To exit program  press N\n"); // exits program            
           scanf("%c", &choice);
        } 
    while(choice != 'Y' && choice != 'y' && choice != 'N' && choice != 'n');
    while(choice == 'Y' || choice == 'y');
  }  
return 0;
}
There are problems at line 41.
 
    while(choice == 'Y' || choice == 'y');

That's a logic error, the compiler will be happy enough, since the code is valid.

What you have there is an empty loop. The semicolon at the end of the line counts as a valid "empty statement".

We could rewrite line 41 like this:
1
2
3
4
    while(choice == 'Y' || choice == 'y')
    {
        // empty loop body
    }

One reason this is so easy to do is that visually a do-while loop and a while loop can be hard to distinguish. Code formatting can help the human reader (the compiler doesn't care).

Personally I adopt the convention that I place the opening and closing braces of a plain while loop on a line by themselves, like this:
1
2
3
4
    while (condition)
    {
    
    }

On the other hand, I'd write a do-while loop like this:
1
2
3
    do {
    
    } while (condition);

How to format the code is a whole subject in its own right, and I only mention it as a way to add legibility - the human reading the code needs all the help they can get :)
You are missing the keyword "do" at line 10.

The while statement at line 41 needs to be outside the bracket at line 42.

thanks for the help. ill try changing fixing it up a bit, hopefully it will work then, thanks again
hi lads well i changed it about but still no look with it, could i be getting my problem any place else

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <stdio.h>
#include <conio.h>
void calc (long); 

int main ()
{
int removelastnumber,  number, mvalue, i, sumnumber=0, next , finalnumber;
char choice;
do 
  {
    sumnumber = 0;
    do 
        {
           printf ("Enter number above zero\n");
	       scanf("%d",&number);
	       
        }
     while (number<0 ); // number has to be greather then zero // 
     mvalue = number*9;   // multiply by 9 
     
     removelastnumber = mvalue/10; // removes last number
     while (removelastnumber > 0) // must be greather then zero 
        {
           sumnumber += removelastnumber % 10;  
	       removelastnumber = removelastnumber/10;
        }
     for (i=0; i < sumnumber+9; i +=9)
        {
	       next = i;
        }
        int n, count=0;
        n=mvalue;
        while(n!=0)
        {
        	n= n/10;
        	++count;
		}
     finalnumber =next - sumnumber;
     printf("when the number %d is multiplied by 9 , the last number in the answer is %d\n " ,number, finalnumber );  // last number 
     printf("ammount of numbers in the answer is %d\n",count); // amount of figures 
     getchar();
     do
        {
           printf("To go again press y:\n");
           printf("To exit program  press 2\n");
           scanf("%c", &choice);
        }
     while(choice != 'y'&& choice != '2');
     
        }
     while(choice=='y'||choice =='2');
     while(choice == 'Y' || choice == 'y');
     

return 0;
}
Last edited on
You only need one of these two lines:
51
52
     while(choice=='y'||choice =='2');
     while(choice == 'Y' || choice == 'y');


Delete line 51 and it should be ok (or at least better).
Topic archived. No new replies allowed.