Need help to repeat the programme

#include <stdio.h>;
#include <cstdlib>;
void main ()
{
int option;
char repeat;
do
{

printf("*************************************\n");
printf("(1) Addition\n");
printf("(2) Subtraction\n");
printf("(3) Multiplication\n");
printf("(4) Division\n");
printf("(5) Optional function 1 (please name it)\n");
printf("(6) Optional function 2 (please name it)\n");
printf("(7) Optional function 3 (please name it)\n");
printf("(8) Optional function 4 (please name it)\n");
printf("(9) Quit\n");
printf("*************************************\n");
printf("Please Input Your Choice:");
scanf("%d",&option);
system ("cls");

if(option = 1)
{
int I, N;
float number,sum=0;
printf("How many numbers do you want to add:");
scanf("%d",&N);
for (I=1; I<=N; I++)
{
printf("Please enter number %d:", I);
scanf("%f",&number);
sum = sum + number;
}
printf("Total = %.2f\n",sum);
}
}
{
printf( "would you like to repeat? (y/n)");
while((repeat != 'y') && (repeat != 'Y') && (repeat != 'n') && (repeat != 'N'));
i wish to repeat to the 1st part pls help thanks :)
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
 #include <stdio.h>;
 #include <cstdlib>;
 void main ()
 {
 int option;
 char repeat;
 do
 {

 printf("*************************************\n");
 printf("(1) Addition\n");
 printf("(2) Subtraction\n");
 printf("(3) Multiplication\n");
 printf("(4) Division\n");
 printf("(5) Optional function 1 (please name it)\n");
 printf("(6) Optional function 2 (please name it)\n");
 printf("(7) Optional function 3 (please name it)\n");
 printf("(8) Optional function 4 (please name it)\n");
 printf("(9) Quit\n");
 printf("*************************************\n");
 printf("Please Input Your Choice:");
 scanf("%d",&option);
 system ("cls");

 if(option = 1)
 {
 int I, N;
 float number,sum=0;
 printf("How many numbers do you want to add:");
 scanf("%d",&N);
 for (I=1; I<=N; I++)
 {
 printf("Please enter number %d:", I);
 scanf("%f",&number);
 sum = sum + number;
 }
 printf("Total = %.2f\n",sum);
 }
 }
 {
 printf( "would you like to repeat? (y/n)");
 scanf("%c",&repeat);
 while((repeat != 'y') && (repeat != 'Y') && (repeat != 'n') && (repeat != 'N'));


Last edited on
sorry >.< i still cant run it it said expected a while @ line 40 n i dun get it -_-||
please help me thanks :)
Im not exactly qualified so im just taking a shot in the dark but do you need a flow control statment between the brackets on 39/40? Just guessin so feel free to laugh in my face :P

Edit: also you could try to move the bracket to after your while statement if possible
Last edited on

scanf () function is to take data from the keyboard buffer, not straightforward to understand as I press a key it was taken away, because you press the keys are stored in the buffer, that is. This function does not work you is because you have character keyboard buffer, so scanf () is not waiting for you to press the key directly from the buffer taken away.
Remember that scanf () is not directly deal with you, you and scanf () there is a keyboard buffer between the middle of such an agent.


Solution:
In the scanf () before adding fflush (stdin); statement, you first need to view how this function, you need to know, you do so, it was clear the keyboard buffer.

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
#include <stdio.h>;
#include <cstdlib>;
void main ()
{
	 int option;
     char repeat;
	 do
	 {
		 printf("*************************************\n");
         printf("(1) Addition\n");
         printf("(2) Subtraction\n");
         printf("(3) Multiplication\n");
         printf("(4) Division\n");
         printf("(5) Optional function 1 (please name it)\n");
         printf("(6) Optional function 2 (please name it)\n");
		 printf("(7) Optional function 3 (please name it)\n");
		 printf("(8) Optional function 4 (please name it)\n");
		 printf("(9) Quit\n");
		 printf("*************************************\n");
		 printf("Please Input Your Choice:");
		 scanf("%d",&option);
		 system ("cls");
		 if(option = 1)
		 {
			 int I, N;
			 float number,sum=0;
			 printf("How many numbers do you want to add:");
			 scanf("%d",&N);
			 for (I=1; I<=N; I++)
			 {
				 printf("Please enter number %d:", I);
				 scanf("%f",&number);
				 sum = sum + number;
			 }
			 printf("Total = %f\n",sum);
		 }
		 printf( "would you like to repeat? (y/n)");
		 fflush(stdin);\\\\\\\\\\\\\\\\\add code;
		 scanf("%c",&repeat);\\\\\\\\\\\\\\\\\true code;
	 }while(repeat == 'y'|| repeat == 'Y');
}














o thanks :)
#include <stdio.h>;
#include <cstdlib>;
#include <math.h>;
void main ()
{
int option;
char repeat;
do
{

printf("*************************************\n");
printf("(1) Addition\n");
printf("(2) Subtraction\n");
printf("(3) Multiplication\n");
printf("(4) Division\n");
printf("(5) Optional function 1 (please name it)\n");
printf("(6) Optional function 2 (please name it)\n");
printf("(7) Optional function 3 (please name it)\n");
printf("(8) Optional function 4 (please name it)\n");
printf("(9) Quit\n");
printf("*************************************\n");
printf("Please Input Your Choice:");
scanf("%d",&option);
system ("cls");

if(option == 1)
{
int I, N;
float number,sum=0;
printf("How many numbers do you want to add:");
scanf("%d",&N);
for (I=1; I<=N; I++)
{
printf("Please enter number %d:", I);
scanf("%f",&number);
sum = sum + number;
}
printf("Total = %.2f\n",sum);
}

else
if(option == 2)
{
int G, J=0;
float number,subtract=0, number1;
printf("How many numbers do you want to Subtract:");
scanf("%d",&J);
{
printf("PLease Enter The Biggest Number To Be SubTract");
scanf("%f",&subtract);

for (G=1; G<=J; G++)
{
J=J-1;
printf("Please enter number %d:", G);
scanf("%f",&number);
subtract = subtract - number;
}

printf("Total = %.2f\n",subtract);
}
}
{
printf( "would you like to repeat? (y/n)");
fflush(stdin);//add code;
scanf("%c",&repeat);//true code;
}while(repeat == 'y'|| repeat == 'Y');
}

i still having problem help >.<
Bumppppppsssss
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <stdio.h>
#include <cstdlib>
#include <math.h>
int main ()
{
int option;
char repeat;
repeat:
system("CLS");

printf("*************************************\n");
printf("(1) Addition\n");
printf("(2) Subtraction\n");
printf("(3) Multiplication\n");
printf("(4) Division\n");
printf("(5) Optional function 1 (please name it)\n");
printf("(6) Optional function 2 (please name it)\n");
printf("(7) Optional function 3 (please name it)\n");
printf("(8) Optional function 4 (please name it)\n");
printf("(9) Quit\n");
printf("*************************************\n");
printf("Please Input Your Choice:");
scanf("%d",&option);
system ("cls");

if(option == 1)
{
int I, N;
float number,sum=0;
printf("How many numbers do you want to add:");
scanf("%d",&N);
for (I=1; I<=N; I++)
{
printf("Please enter number %d:", I);
scanf("%f",&number);
sum = sum + number;
}
printf("Total = %.2f\n",sum);
}

else
if(option == 2)
{
int G, J=0;
float number,subtract=0, number1;
printf("How many numbers do you want to Subtract:");
scanf("%d",&J);
{
printf("PLease Enter The Biggest Number To Be SubTract");
scanf("%f",&subtract);

for (G=1; G<=J; G++)
{
J=J-1;
printf("Please enter number %d:", G);
scanf("%f",&number);
subtract = subtract - number;
}

printf("Total = %.2f\n",subtract);
}
}
{
printf( "would you like to repeat? (y/n)");
fflush(stdin);//add code;
scanf("%c",&repeat);//true code;
}if(repeat == 'y'|| repeat == 'Y')
{goto repeat;}
else
return 0;
}

Just ran this breifly but it should do what you want.
thanks:)
You shouldn't ever, ever use a goto.

Unless you really forced to!

The code posted by frozendog11 at 12:22pm on Jul 30, 2011 was almost there!

If it was correctly indented, you would have spotted the misplaced braces - 3 of them. (Or is the poor indenting due to this web site?)

Also note the #include statement to not take a ;

#include <stdio.h>

NOT

#include <stdio.h>;

And that was all that was wrong with the code.

Andy
oo i c thanks
Topic archived. No new replies allowed.