how to exit code

hi there i have my code fully working as i want apart from exiting it. when i press 2 to exit it now it just gives me two lines of code saying "to go again press 1" how can i change that so when i press 2 the program will exit, ? thanks in advanced. and for the help i got of ye before

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>
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;
        }
     finalnumber =next - sumnumber;
     printf("The last number enterd  %d multiplied by 9 is %d\n " ,number, finalnumber ); 
     getchar();
     do
        {
           printf("To go again press 1:\n");
           scanf("%c", &choice);
        }
     while(choice != '1' && choice != '1');
        }
     while(choice=='1'||choice =='1');

 printf("To exit program  press 2\n");
 scanf("%c", &choice);
 {
 return 0;
 }
return 0;
}
your loops are all messed up. line 14 will never run because you never defined it with {} again on line 24 you have the same problem. and the main do...while loop has no end point. You MUST start by defining where your loops start and end with { and }. If you aren't already using it try using Notepad++ it will point some of these things out for you before you try and compile the code.
I'm running it on dev++ its seams to be compiling it and giving me no errors.
when i put brackets in before the while loops it keeps giving me errors and wont run
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
#include <stdio.h>
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; 
                }
             while (removelastnumber > 0) 

        {
           sumnumber += removelastnumber % 10;  
	       removelastnumber = removelastnumber/10; 
        }
     for (i=0; i < sumnumber+9; i +=9)
        {
	       next = i;
        }
     finalnumber =next-sumnumber;//minus sum  
     printf("The last number enterd  %d multiplied by 9 is %d\n " ,number, finalnumber ); 
     getchar();
     do
        {
           printf("To go again press 1:\n"); // send back to start 
           scanf("%c", &choice);
        }
     while(choice != '1' && choice != '1'); 
        }
     while(choice=='1'||choice =='1');

 printf("To exit program  press 2\n"); //exits program 
 scanf("%c", &choice);
return 0;
}
nu123 wrote:
I'm running it on dev++
please don't.
I'm suggesting you to use code::blocks or visual studio 2015 or something similar...
when i press 2 to exit it now it just gives me two lines of code saying "to go again press 1" how can i change that so when i press 2 the program will exit, ?

The reason you get two lines is because the scanf is not ignoring the newline remaining when the user presses enter.
First, change the scanf statement:
 
scanf(" %c", &choice);

Note the leading space before the % symbol. That's important here.
see http://www.cplusplus.com/reference/cstdio/scanf/

Next, it would be useful to let the user know what character you'd like them to type in order to exit.

Finally, make sure your do-while loop tests for those specified characters
1
2
3
4
5
     do {
           printf("To go again      press 1\n"); // send back to start 
           printf("To exit program  press 2\n"); // exits program            
           scanf(" %c", &choice);
     } while (choice != '1' && choice != '2');


And last, make sure the main do-while loop repeats only when a '1' has been entered.

} while (choice == '1');


that's great thanks. thanks for the link its really helpful
I have changed my code about as you have it show, but now I keep getting a error on my very last lines, return 0; and the one above
i have tried moving this about putting in more brackets taking some away, deleting bits ect but I'm still getting it, any chance you can point me towards why I'm getting this error.
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
#include <stdio.h>
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;
        }
     finalnumber =next - sumnumber;
     printf("The last number enterd  %d multiplied by 9 is %d\n " ,number, finalnumber ); 
     getchar();
     do
        {
           printf("To go again      press 1\n"); // send back to start 
           printf("To exit program  press 2\n"); // exits program            
           scanf(" %c", &choice);
        } 
	while (choice != '1' && choice != '2');
  }  
return 0;
}

Last edited on
At line 6 there is a do - the start of a do-while loop.

Where is the corresponding while ?
that's great thanks. i didn't realise that would affect the code so far down after where i made a mistake, the code is compiling and working now,
only thing iv to fix now is when i press 1 or 2 they both exit the code, but i only want this to happen with 2
thanks for the helps lads i really appreciate it
Topic archived. No new replies allowed.