well ok what am i doing wrong o.o any1 know cuz i tried everything that would stop the windows from closing and it just closes no matter what after i press y again and i also looked through the forums and tried system puase cin.get and some other stuff
#include <iostream>
#include <cmath>
#include <stdlib.h>
usingnamespace std;
int main()
{
cout<< "Do you want to generate a number \n";
char x;
int y;
cin>> x;
while (x == y);
{
srand ((unsigned) time (NULL));
y = 0 + rand() % 3999999999;
cout<< y << endl;
cout<< "Do you want to generate a number\n";
cin >> x;
}
cout << " Thank You for using the Random number generator\n";
cin.get();
return 0;
}
1. You're using the variable y for comparison, when you actually mean the character 'y' (use descriptive variable names!).
2. You terminated the while with a semicolon, which causes the following block not to be part of the loop.
3. srand should be called once at the beginning of the program.
You never set the variable "y" equal to anything, the fact that it doesn't skip that loop at all is a fluke I missed the semicolon. The command "cin.get()" pulls the next character from the buffer, since the ">>" operator skips all whitespace the character from when you press 'Enter' for Line 17 is still in the buffer.
#include <iostream>
#include <cmath>
#include <stdlib.h>
usingnamespace std;
int main()
{
cout<< "Do you want to generate a number (1= yes) \n";
srand ((unsigned) time (NULL));
int z;
int x;
cin>> x;
while (x == 1)
{
z = 0 + rand() % 3999999999;
cout<< z << endl;
cout<< "Do you want to generate a number\n";
cin >> x;
}
cout << "Thank You for using the Random number generator\n";
system ("pause");
return 0;
}
Yes, some things:
1. stdlib.h is the C variant, the header is called cstdlib in C++.
2. Variables should generally be as local as possible. Since z is not used outside the loop, it should be declared at the point of initialization.
3. Use descriptive variable names. An acceptable variable name for x is choice and randomNum(ber) (or at least rn) for z.
4. y was better than 1, IMO.
5. Don't use system("pause"), see http://www.cplusplus.com/forum/beginner/1988/
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <string>
#include <limits>
usingnamespace std;
int main()
{
cout<< "Do you want to generate a number? \n";
srand ((unsigned) time (NULL));
string x;
cin>> x;
while (x == "y")
{
int z;
z = 0 + rand() % 3999999999;
cout<< z << endl;
cout<< "Do you want to generate a number?\n";
cin >> x;
}
cout << "Thank You for using the Random number generator\n";
cout << "Press ENTER to continue...";
cin.sync();
return 0;
}