Sunday, August 2, 2009

Beginner C++ Help?

Hi,I'm new to C++. And I mean literally new, as in I just started yesterday. I've read a few tutorials and learned somethings but I still know basically nothing. I'm making some simple programs to practice and the current one is a random number generator. Here is the code.





#include %26lt;iostream%26gt;





using namespace std;





int main()





{





int a,b,c,d,e,f;





string g,h="a";





loop:





a = (rand()%10);





b = (rand()%10);





c = (rand()%10);





d = (rand()%10);





e = (rand()%10);





f = (rand()%10);





cout%26lt;%26lt;"\n";





cout %26lt;%26lt; a;





cout %26lt;%26lt; b;





cout %26lt;%26lt; c;





cout %26lt;%26lt; d;





cout %26lt;%26lt; e;





cout %26lt;%26lt; f;





cout%26lt;%26lt;"\n";





cout%26lt;%26lt;"\n";





cout%26lt;%26lt;"To create a new random number, press a and then enter.";





getline (cin,g);





if (g==h){





goto loop;





}





cin.get();





}

Beginner C++ Help?
As Ahmad said, you need to seed the PRNG. srand with time as an argument works well. But I have a few things to say about your code.





The first is your variable names. Please choose more meaningful variable names. You realize they can be more than one letter long right?





Do not use goto and labels. They have their use, but no beginner and most intermediates will encounter such a situation. There are flow control constructs, like for loops, while loops, and do while loops. Use them. Your code could be rewritten to use a do-while loop and two for loops.





There’s something called an array. You should learn it as soon as possible.
Reply:You need to seed the random number generator.





http://www.cprogramming.com/tutorial/ran...


No comments:

Post a Comment