Random numbers and if-else statements

okay so the program works but I need the computer to pick the numbers not the user. At this point, the program is asking the user to input the number and then it runs the rest of the program but like I said I need the computer to pick the numbers.
#include <iostream>
#include <cmath>
#include <ctime>
#include <cstdlib>

using namespace std;

int main() {

//define variables
double a, b, c, x1, x2;
double determinant;

//generate the random numbers
srand(static_cast<signed int>(time(0)));
a = rand() % -5 + 5;
b = rand() % -5 + 5;
c = rand() % -5 + 5;

//print out the random numbers
cout << "The first number is: ";
cin >> a;
cout << a;

cout << "The second number is: ";
cin >> b;
cout << b;

cout << "The third number is: ";
cin >> c;
cout << c;


determinant = (b * b - 4 * a * c);

//write out the if-else statements
if (a == 0) {
cout << "We cannot divide by 0";
}
else {
if (determinant > 0) {
x1 = (-b + sqrt(determinant)) / (2 * a);
x2 = (-b + sqrt(determinant)) / (2 * a);
cout << "Result 1: " << x1 << endl;
cout << "Result 2: " << x2 << endl;
}
else {
cout << "No solution for x." << endl;
}
}
return 0;
}
Last edited on
Ok so first, I would change srand(time(0)) to srand(static_cast<signed int>(time(0))); or srand(static_cast<signed int>(time(NULL)));
I think it's just for elegance sake really, but at least it specifies that it is a signed integer, I don't think you HAVE to do it, but like I said, for elegance sake (plus some people might be really picky especially the SubrDubrL33bCoderz here)

Next, I don't know why you are doing this char a, b, c, x1, x2
I think you should change that to int a, b, c, x1, x2

Next, you have this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
if (a == 0) {
	cout << "We cannot divide by 0.";
}
else {
	if (determinant > 0) {
		x1 = (-b + sqrt(determinant)) / (2 * a);
		x2 = (-b - sqrt(determinant)) / (2 * a);
		cout << "Result 1: " << x1 << endl;
		cout << "Result 2: " << x2 << endl;
	}
	else if (determinant == 0) {
		x1 = (-b + sqrt(determinant)) / (2 * a);
		cout << "Result: " << x1 << endl;
	}
	else {
		cout << "No solutionn for x." << endl;
	}
}


Why do you have the else, right after the if and then you continue to do elseif?
Check this out: http://www.cplusplus.com/doc/tutorial/control/

Look at how if/else if/else works.

I haven't done your homework but I thought I'd just show you some things you can change, and I hope you can figure it out.

Edit: Turn your PM's on, maybe I can try to work it out with you

Edit2: For the if{}else part you should do this instead

1
2
3
4
5
6
7
8
9
10
11
12
if(a == 0){
    // code here
}
else if(determinant > 0){
    // code here
}
else if(determinant == 0){
    // code here
}
else{
    // code here
}


Also, I know that
1
2
3
std::cin >> a
std::cin >> b
std::cin >> c 


Is cool and all, but you can also just do std::cin >> a >> b >> c just for brevity sake I guess, even though the aforementioned method (the one you are using) is probably safer..
Last edited on
srand(static_cast<signed int>(time(0)));

Technically, it should be
srand(static_cast<unsigned int>(time(0)));

He had the negative numbers almost right.
1
2
3
int n = rand() % 11 - 5
// n = rand() % 11 = {0 -> 10}
// n - 5 = {-5 -> 5} 


<edit> Tired and had the mod wrong</edit>
Last edited on
Hey admkrk

srand(static_cast<signed int>(time(0))); Will get you -2147483648 to 2147483647

srand(static_cast<unsigned int>(time(0))); Will get you 0 to 4294967295

http://www.tutorialspoint.com/cplusplus/cpp_data_types.htm


He needed to generate random numbers between -5 and 5

int n = rand() % 10 + -5 is working for me, not int n = rand() % -5 + 5, don't know why but I assisted him in fixing that through PM


EDIT: I just PM'ed user the full solution I came up with.
Last edited on
newatthis wrote:
Hey admkrk

1
2
3
srand(static_cast<signed int>(time(0))); Will get you -2147483648 to 2147483647

srand(static_cast<unsigned int>(time(0))); Will get you 0 to 4294967295



No, that's not how srand() works. admkirk is correct. The static_cast is there to match the type returned by time(). It does not control the range of numbers.

time() returns a value of type time_t. In some (most?) implementations, time_t is a signed int. srand() expects an argument of unsigned int. Without the static cast, the compiler will generate a warning message for a type mismatch.
Topic archived. No new replies allowed.