Please HELP with random numbers

Input a number/integer:_____
num1 =
num2 =
num3 =
The Total is:______

Let say I input 50
num1 = 10
num2 = 15
num3 = 25
The Total is: 50

If I input 50 again it should be generate random numbers but still equal to 50 like:
num1 = 20
num2 = 20
num3 = 10
The Total is: 50
Last edited on
What do you need? An algorithm or code?
code for C++
here you go try this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;

int main() {
srand (time(NULL));
int num,num1,num2,num3;
cout << "input a number \n";
cin >> num;
num1 = rand() % num + 1;
int temp1 = num - num1;
num2 = rand() % temp1 + 1;
num3 = num - (num1 + num2);

cout << "num1 = " << num1 <<  endl;
cout << "num2 = " << num2 <<  endl;
cout << "num3 = " << num3 <<  endl;
cout << "total is " << num1+num2+num3 <<  endl;
return 0;
}
TYVM Vidus!!! It's working Yey!!!! :D appreciate it
the codes is not working in Turbo C++
Try a better compiler.

Turbo C++ is antiquated.
please help it says "an access violation (segmentation fault) raised"



#include <iostream>
#include <string.h>
#include <iomanip>
#include <fstream>
#define p printf

using namespace std;

void menu(int &selection);
int the_count();
int the_random();
int main()
{
	int selection;
	do
	{
		menu(selection);

		if (selection == 1)
		{
			system("cls");
			the_count();
		}
        
        else if (selection == 2)
        {
             system("cls");
             the_random();
        }
        
		else if (selection == 3)
		{
			system("cls");
			cout << "The End!!!" << endl;
			system("pause");
		}

		else
		{
			system("cls");
			cout << "Invalid selection!" << endl;
			system("pause");
		}
	}
	while (selection != 0);

	return 0;
}


void menu(int &selection)
{
	system("cls");
	cout << "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$" << endl;
	cout << "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$" << endl;
	cout << "$$$$   T H I S  I S  A  T E S T   $$$$" << endl;
	cout << "$$$$                              $$$$" << endl;
	cout << "$$$$        P R O G R A M         $$$$" << endl;
	cout << "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$" << endl;
	cout << "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$" << endl;
	cout << "$$$$                              $$$$" << endl;
	cout << "$$$$                              $$$$" << endl;
	cout << "$$$$ 1     Count the Characters   $$$$" << endl;
	cout << "$$$$ 2     Random Numbers         $$$$" << endl;
	cout << "$$$$ 3     Exit program           $$$$" << endl;
	cout << "$$$$                              $$$$" << endl;
	cout << "$$$$                              $$$$" << endl;
	cout << "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$" << endl;
	cout << "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$" << endl  << endl << endl << endl;

	cout << "Enter your selection : " ;
	cin >> selection;
}

int the_count()
{
	
	int items = 0;
	char another = 'Y';

	do
			{

				cout << "Would you like to run the receipt program again? (Y/N) : ";
				cin >> another;	
			if(toupper(another)=='N');
			{
				cout << "**************************************" << endl;
				cout << "**************************************" << endl;
				cout << "*******    T H A N K  Y O U    *******" << endl;
				cout << "*******    F O R  U S I N G    *******" << endl;
				cout << "******* T H I S  P R O G R A M *******" << endl;
				cout << "**************************************" << endl;
				cout << "**************************************" << endl;
				return 0;
			}
			}while(toupper(another)=='Y');
}

int the_random()
{
    int strlength;
    int item = 0;
    char *str;
    char another = 'Y';
    do
    {
         p("\nEnter the string: ");
                    gets(str);
                    strlength=strlen(str);
                    p("\nThe length of the string is %d.",strlength);
                    cin >> another;
                    if(toupper(another)=='N');
                    {
                                              cout << "Thank You For Using This Program" << endl;
                                              return 0;
                                              }
                                              }while(toupper(another)=='Y');

}


try changing your the_random function to this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

int the_random()
{
    int strlength;
    int item = 0;
    string str;
    char another = 'Y';
    do
    {
         p("\nEnter the string: ");
               cin >> str;
                    strlength= str.length();
                    p("\nThe length of the string is %d.",strlength);
                    cin >> another;
                    if(toupper(another)=='N');
                    {
                                              cout << "Thank You For Using This Program" << endl;
                                              return 0;
                                              }
                                              }while(toupper(another)=='Y');

}
It's working now thnx Vidus
This one Vidus in Turbo C it say undefined symbol function_random()/function_exit()/function_count() in module
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
#include<stdio.h>
#include<conio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define p printf
#define s scanf
void main()
{
int z,a,b,c;
int function_count();
int function_random();
int function_exit();
//clrscr();
p("*************");
p("\n\nMAIN MENU");
p("*************");
p("\n\nEnter Compilation:\t");
p("a - Count The Characters");
p("b - Random Numbers");
p("c - Exit");
scanf("%d", &z);
if(z=='a')
{
function_count();
}
else if(z=='b')
{
function_random();
}
else if(z=='c')
{
function_exit();
}
function_count();
{
 int strlength;
char *str;
clrscr();
      printf("\nEnter the string: ");
      gets(str);
strlength=strlen(str);
printf("\nThe length of the string is %d.",strlength);
getch();
 }
function_random();
{
srand (time(NULL));
int num,num1,num2,num3,answer;
clrscr();
printf("Input a interger: ");
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",answer);
getch();


}
}
Lines 11-13: Your forward prototypes should come before main().

line 35,46: You have an extraneous ; This is causing the lines to be interpreted as forward prototypes, not a function header.

Line 48: 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/
Turbo C++ is ancient.
You'll want to randomize() somewhere at the beginning of main() and call random() to get a random number.
Topic archived. No new replies allowed.