hi.....here is my code:::

int main() 
{ 
    srand((unsigned)time(0));
    int random_integer,num,len; 
	
	cout << "Random numbers: ";
	cin >> num;
	cout<<"Lenght of each numbers: ";
	cin>>len;

	for (int i=0; i<num; i++)
	      {
		         cout<<endl;

                           for(int j=0; j<len; j++)
	                                { 
                                            random_integer = rand() %10 ;
                                             cout << random_integer; 
		                         }
	
           }

my question is...
Is this code correct???

Recommended Answers

All 9 Replies

Strictly speaking, no. You don't have the #includes, no return statement, no closing curly brace. And the indenting (as displayed here) is not so great - goes in too far and too many levels.

If you mean, will it display a certain number of pseudo random numbers,of some specified number of digits, well, OK, it does that.

But you could run it and see that it does that.

Since all it does is display them, what's the point?

Other defects:
1. No input result checking. For example, if an user types not a number(s) the program behaviour is undefined.
2. Incomprehensible and misleading prompts.

sorry guys, i can't post all the program....this is a part of fuction and their are other fuction on this....so logical is correct..... but
i should add if statement if the user add char....the program return... right???

void fuction () 
{
    srand((unsigned)time(0));
    int random_integer,num,len;  
    bool check;
	
       top::
	cout << "Random numbers: ";
	cin >> num;
	cout<<"Lenght of each numbers: ";
	cin>>len;
 if (( num == char) || (len == chr)) 
   { 
       cout<<"You add a character"<<endl;
       cout<<"Num and len must be numbers";
       check=false;
       goto top:       
} 
                  else
          
for (int i=0; i<num; i++)
	      {
		         cout<<endl;

                           for(int j=0; j<len; j++)
	                                { 
                                            random_integer = rand() %10 ;
                                             cout << random_integer; 
		                         }
	
           }

I want to know if my thought for the for loop is correct....
i try to make a program (at this situation) that read how many numbers wants the user then ask how many digits and print the numbers....
example:
NUMBERS: 6
LENGTH: 5

output:
12345
2
3
4
5
6

of corcse this numbers must be random so i but this for loop
logical and in the reality it's works .... but i want onother opinions....

Thank you very much for your help

if (( num == char) || (len == chr)) Not gonna work.

Best way to defend against bad input is put the input statement as the the condition of an if or loop statement, like

if(! cin >> num ) 
{
    //error handler here
}
//  or
while ( !cin >> num )
{
    //error handler
}

How do you handle the error? Use of cin.clear( ) to reset the error state and cin.ignore( ) to remove the offending input.

A little correction:

while (!(cin >> num)) // operator priorities...

Its indentation is obfuscious and the naming scheme is mendacious.

No, it's not right.

ofcorce is wrong with this statement

if (( num == char) || (len == chr))

it's not make sence.... which char??? ;) there are many characters...

thank you for your help.... also you don't anwser to my main question....
my idea for this fuction , I mean my logic for this fuction is corect??
it give me the right output?? i try i and i think it's corect so......
I'm waitting for answer from one more experienced than me!!!


thank you again for your help !!!!!!!!!:D

thank you for your help.... also you don't anwser to my main question....
my idea for this fuction , I mean my logic for this fuction is corect??
it give me the right output?? i try i and i think it's corect so......
I'm waitting for answer from one more experienced than me!!!

But vmanes did answer your question:

"If you mean, will it display a certain number of pseudo random numbers,of some specified number of digits, well, OK, it does that.
But you could run it and see that it does that."

The for loop will work.

MrSpigot thank you it's working........

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.