nothing prints!?

Pages: 12
Hi.
Why is it that nothing prints with my code?
Here is my code:
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
void generate( int *A, int m, int s, int n);//impl. is left out for the //sake of berevity. it  just fills the array with random numbers

int main( )
{	 int 	*A;   			 //Array to hold numbers
	 int 	i, j, k;   		 //Loop indices
	 long	n = 3, r = 5;   		 //Used as the range for numbers
	 long	m = 3;				 //m numbers will be generated
	 
	 long iSize = r - n + 1;
	 

	 A = new int[iSize];
	 
	 for(int j=0;j<iSize;++j)
	    cout << "here";// <<  A[j];
	    
	 generate(A, m, iSize, n);
 
	 for(int j=0;j<m;++j)
	    cout << A[j];
	    
	    while(1);
	 
   return 0;
} 

warning C4101: 'i' : unreferenced local variable
warning C4101: 'k' : unreferenced local variable
warning C4101: 'j' : unreferenced local variable



For starters, you dont need line 5. You seem to be declaring the variables you need when you need them (which is good).

edit: if i run this i do get some (garbage to the screen).
Last edited on
try using endl after your cout statement
We may need to see the contents of the "generate()" function since it is what actually populates the array you are trying to display.

EDIT: About Line 22, you get points for being clever there. That's one I don't think I've seen before.
Last edited on
About Line 22, you get points for being clever there. That's one I don't think I've seen before.


I hope that's sarcasm.
Why? It's original. OP is making an effort to hold the screen open without copping out and using a system call. Sure it spikes their CPU and you can't just press a key to exit but it shows creativity. I have without a doubt seen worse ways of doing this.

And who are you to criticize? Mr.:
if i run this i do get some (garbage to the screen).

I hope you realize that this is normal behavior when you try to print out data from an uninitialized array. Without the content of the "generate()" function how can you even pretend to know what is in OPs array?
You think putting an infinite loop into your code is a good idea?
Jesus....
I'm applauding originality. Why don't you like it? Because you don't know what to press to exit it? Try Ctrl+C, you should be running console applications from within the command prompt anyway. You're the one expecting to see meaningful data in an array that doesn't have values set. Would you like to see a poll here on which one people consider to be dumber?
closed account (Dy7SLyTq)
thats actually quite a clever way of keeping it open. we could all get pissed at him for using system("pause") or cin.get() without checking the buffer, but he found a sure fire way to keep it open. i have never seen an intentional inifinite loop used that way and is good for practicing writing software
I defined Generate that way:
1
2
3
4
5
6
7
8
void generate( int *A, int m, int s, int n)
{
/*********initialize random seed*********/
	srand( time(0) );
			
	for(int i = 0;i<10;++i)
	  A[i] = rand() % s + n;
}
It looks like you're app is crashing. The size of you array is being calculated as 'r - n + 1', 'r' equaling '5' and 'n' equaling '3' plus '1' gives us a number less then '10'.
still, I am unable to execute the program:permission denied.
I changed 10 to m
If you are getting a 'permission denied' error then that is completely different from 'nothing is printing to the screen'.

Permission denied usually means the file you are trying to over write when you compile your project is still open, that is of course assuming you have write permissions on the target directory. Check your running processes and kill it if it is there, if you don't see it then try rebooting the PC to release any weird file locks that might be on it.
How to see thelist?
To see the list of running processes on Windows use Task Manager or Process Explorer.

To see it on *Nix use System Monitor.
No, sys monitor doesn't show 'a.out'
I'm not really a *Nix guy so someone else correct me if I'm wrong, but isn't 'a.out' the default output FOLDER for GCC and not the executable name itself?
No, it's the executable's name.
@geek:
when you compile a file under *nix, gcc or g++ it, you can specify the output file's name. Otherwise it stores as 'a.out'
@ Zhuge: Thanks for fielding that one. I really only dabble in Linux and even then I rarely write code in it.

@ OP: I'd say try rebooting but I didn't think Linux locked files just because they are in use. Have you checked your privileges on that folder?
Pages: 12