Need Help For nested loop

do{
MAIN MENU
1. Count String
2. Random Numbers
3. Exit
Enter Your Choice:

if (1)
Enter String: EXAMPLE
Do you want to try again? y/n (if YES it will just return to Enter String, if NO it will return to MAIN MENU)

same to else if(2)
Enter Number: EXAMPLE
Do you want to try again? y/n (if YES it will just return to Enter String, if NO it will return to MAIN MENU)

else exit

(below is my codes so far, I'm using turbo C

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
  #include<string.h>
#include<stdio.h>
#include<conio.h>
#include<time.h>
#include<stdlib.h>

int main(void)
{
int numm;
char response = 'Y';
char responsee = 'Y';
clrscr();
printf("\n MAIN MENU");
printf("\n 1. Random Numbers");
printf("\n 2. Count Character");
printf("\n 3. Exit");
printf("\n Enter your choice: ");
scanf("%d",&numm);

	if(numm==1)
	{
	srand (time(NULL));
	int num,num1,num2,num3,answer,ans;
		clrscr();
		while (response == 'y' || response == 'Y'){
		clrscr();
		printf("Input a Integer: ");
		scanf("%d",&num);
		num1 = rand() % num + 1;
		int temp1 = num - num1;
		num2 = rand() % temp1 + 1;
		num3 = num - (num1 + num2);
		answer=num1+num2+num3;
		printf("num1 = %d\n",num1);
		printf("num2 = %d\n",num2);
		printf("num3 = %d\n",num3);
		printf("The Total is: %d\n",answer);
		printf("DO YOU WANT TO TRY AGAIN? Y/N: ");
		scanf("%s",&response);
		
		} 
		}
	else if(numm==2)
	{
	int strlength;
	int ans;
	char *str;
		clrscr();
		while (responsee == 'y' || responsee == 'Y'){
		clrscr();
				printf("\nEnter the string: ");scanf("%d",&strlength);
				gets(str);
		strlength=strlen(str);
		printf("\nThe length of the string is %d.\n",strlength);
		printf("\nDO YOU WANT TO TRY AGAIN? Y/N: ");
		scanf("%s",&responsee);
		}
		}
		else exit(0);
		{
		
getch();
//return 0;
}}
Last edited on
Ok, what is your question?
I don't see any nested loops.
how to return to main menu if i type N in here

if (1)
Enter String: EXAMPLE
Do you want to try again? y/n (if YES it will just return to Enter String, if NO it will return to MAIN MENU)
First of all, why don't you use cout? Are you coming from a background of C?
Anyway, in answer to your question, you should use:
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
 #include<string.h>
#include<stdio.h>
#include<conio.h>
#include<time.h>
#include<stdlib.h>

int main(void)
{
int numm;
char response = 'Y';
char responsee = 'Y';
clrscr();
printf("\n MAIN MENU");
printf("\n 1. Random Numbers");
printf("\n 2. Count Character");
printf("\n 3. Exit");
printf("\n Enter your choice: ");
scanf("%d",&numm);
        while(numm != "N")
        {
	if(numm==1)
	{
	srand (time(NULL));
	int num,num1,num2,num3,answer,ans;
		clrscr();
		while (response == 'y' || response == 'Y'){
		clrscr();
                while(numm != "N")
                {
		printf("Input a Integer: ");
		scanf("%d",&num);
		num1 = rand() % num + 1;
		int temp1 = num - num1;
		num2 = rand() % temp1 + 1;
		num3 = num - (num1 + num2);
		answer=num1+num2+num3;
		printf("num1 = %d\n",num1);
		printf("num2 = %d\n",num2);
		printf("num3 = %d\n",num3);
		printf("The Total is: %d\n",answer);
		printf("DO YOU WANT TO TRY AGAIN? Y/N: ");
		scanf("%s",&response);
		
		} 
		}
	else if(numm==2)
	{
	int strlength;
	int ans;
	char *str;
		clrscr();
		while (responsee == 'y' || responsee == 'Y'){
		clrscr();
				printf("\nEnter the string: ");scanf("%d",&strlength);
				gets(str);
		strlength=strlen(str);
		printf("\nThe length of the string is %d.\n",strlength);
		printf("\nDO YOU WANT TO TRY AGAIN? Y/N: ");
		scanf("%s",&responsee);
		}
		}		
		
getch();
//return 0;
}}


Does this fix the problem?
Last edited on
@theturk1234 & OP - you have some problems in the code you posted which were inherited from the OP.

line 18-19: You're doing a scanf into numm which is an int. That's never going to compare to a quoted string ("N"). You probably want to be comparing numm to 3, which is what is specified in the menu.

Line 42,59: You're using the wrong scanf operator. You should be using %c to read into a char.

Line 23: Do not call srand() within a loop or a random number function. srand() sets the RNG to a particular starting point. Calling srand() repeatedly can cause the RNG to return the same random numbers. srand() should be called ONCE at the beginning of main().
http://www.cplusplus.com/reference/cstdlib/srand/


Last edited on
Topic archived. No new replies allowed.