Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Do any of you program C/C++in Xcode?
#1
I am taking an intro to C. Right now I am using slickedit as an IDE which I can access remotely on campus through the X11 client. It's fine but it really is much more full featured and complicated than what I need. At this point all we are doing is "hello world" kind of stuff and simple functions.

I have tried X code and have been able to get a program to build with no syntatic errors, but I am stuck as to how to use the debugger to ferret out a logic error somewhere. Specifically, I can't figure out how to use the "step into" as it seems to differ from what I have been doing with slickedit.

In slick edit, I can step into the function as it executes each individual line. But in Xcode it seems to jump ahead and I never see parts of my code in the GDB debugger screen.

Here is the code as it stands now:
#include

void printRows(int);
int binomial(int n,int r);
int factorial(int n);

int main(void){

int numRows = 0;

/* Ask for a positive integer and repeat if negative.*/
do{
printf("Enter a positive integer\n");
scanf("%d",&numRows);
if(numRows<0)
printf("Negative number entered!\nTry again.\n");
}while(numRows<0);

printf("\nThe Pascal's triangle to the %dth row:\n",numRows);
printRows(numRows);

printf("\n\n");

return(0);
}

/* Prints the Pascal's triangle from row 0 to row numRows. */
void printRows(int numRows){

int i, j;
/*for each row in the triangle*/
for(i=0; i < numRows; i++){
/*for each column in this row. (note that there are N+1 columns in
the Nth row.) */
for(j=0; j <= i; j++){
/* Each element in the triangle is the binomial coefficient
(row column) i.e. row choose column. */
printf("%d ", binomial(i,j));
}
printf("\n");
}

}

/*Calculate and return the binomial coefficient n to r (i.e. n choose r)*/
int binomial(int n, int r){
return factorial(n)/(factorial®*factorial(n-r));
}

/*Recursively calculate and return the value of factorial n*/
int factorial(int n) {

/* factorial of 0 or 1 is 1 by definition.*/
if(n <= 1)
return 1;

else
/* for all the other numbers, the factorial is the product of the
number and the factorial of the immediately preceding number. */
return n*factorial(n-1);
}


Let me make it clear that I am not cheating on homework. This problem was due earlier in the week and I have already turned it in. So I am just practicing with Xcode because I would rather use it. Here are some screenshots of what I am seeing, just before it stops.





and after it stops, you can see that step into is greyed out:





Do I have to be setting break points or something? I tried putting one at the end, but it didn't help. Any assistance would be appreciated


Craig
Reply
#2
With C functions, sometimes (always?) you do jump into the assembly code when you click on "Step Into" in the debugger. I suggest using "Step Over," which should work fine for individual lines of code. Only use "Step Into" when you have an individual function call. In your application, you would do this for the following line:

printRows(numRows);

Otherwise, just use "Step Over."

-Tofer
Reply
#3
I'm just re-reading your post, and I am wondering: did you even set a breakpoint? To do so, you need to simply click on the left side in the grey space next to your code. You get a little blue arrow, like this:



Once you set a breakpoint, you can then click Build-->Build and Debug. Oh, and make sure you have Project-->Set Active Build Configuration-->Debug enabled (but disable this if you want to actually use your program on another computer).

Feel free to PM me if you have any other XCode issues.

-Tofer
Reply
#4
Sorry for all the multiple posts...I just stepped through your code in XCode, and you can use Step Into at each step. Set a breakpoint (like I mentioned above), and you should be all set. Oh, and you'll need the Debug-->Standard IO Log window open to enter the number for your input.

-Tofer
Reply
#5
OK to answer your questions:

I did set break points at various points, but it still wasn't doing what I wanted. Admittedly, I wasn't really sure where to break though.

I did notice that the debugger was jumping into the assembly code when the trap routine was called. I assume that this is the stuff going on behind the scenes to get the number that is entered. At least that was how I remember it when I learned assembly.

The option that you metioned was set to debug and not release.

I also have the IO log open to enter the number and that worked fine when I executed the whole thing.

So it seems like you are saying that I can "step into" if I have breakpoints set at all the function calls. Otherwise I should "step over" until a function is called, then "step in" to the function, then continue to step over until when- the return? My best bet sounds like setting my breakpoints better and then let you know if there are still issues.

Tofer, thanks greatly for your help. Great place, this forum.


Craig
Reply
#6
I think I have it. I will report back if I have further problems.

Thanks again.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)