Random generator

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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <cmath>
#include <stdlib.h>
using namespace 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.

Try setting a number of characters to pull on Line 21, here's the prototype:
http://www.cplusplus.com/reference/iostream/istream/get/
To hold the program open.
Last edited on
Thanks alot guys lol now i c the stupidity revised code let me know if i can enhance it
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <cmath>
#include <stdlib.h>
using namespace 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;
    
}
Last edited on
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/
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
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <string>
#include <limits>
using namespace 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;
    
}
Topic archived. No new replies allowed.