Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

emeraldstone

macrumors newbie
Original poster
Sep 7, 2003
7
0
Ireland
I'm using Project Builder.
When I write a program in C without "scanf", the program runs perfect, however when I use "scanf", it refuses to run correctly. I have to type numbers/letters and press return then it excutes the entire program, In the Project Builder console. I have used other IDE's, there is no error in the code.

Maybe some one could tell me the equivilent of "scanf" for Objective-C

Thanks
 

Taft

macrumors 65816
Jan 31, 2002
1,319
0
Chicago
Could you post the code?

Also, which project type did you put the code into under Project Builder?

Taft
 

emeraldstone

macrumors newbie
Original poster
Sep 7, 2003
7
0
Ireland
"scanf" problem in Project Builder

I selected Cocoa as the project type.
Cocoa is objective-C, it should understabd C perfectly. If the code contains no "scanf" the code runs fine, as soon as "scanf" is placed in the code it doesn't work correctly. It compiles without errors or warnings though.

I'm just a beginner, if that matters.

Here is some code examples:

this code runs fine:

#include <stdio.h>

int main()
{
printf("text");

return 0;
}

this doesn't:

#include <stdio.h>

int var;

int main()
{
printf("text");
scanf("%d", var);

return 0;
}
 

thegfunk

macrumors newbie
Sep 8, 2003
2
0
the problem is you need to scanf into the address of the variable.
so, "scanf( "%d", &var);", otherwise you are trying to write the input value to the address specified by var. If you want to get text, then "scanf("%s", somestring);". Generally I wouldn't use scanf to get a string, instead I would use gets. But that also has the downside of introducing security issues, which may not be an issue with your application.

Grant
 

cubist

macrumors 68020
Jul 4, 2002
2,075
0
Muncie, Indiana
Personally, I prefer not to call scanf directly, as it does funny things if you need multiple values. I prefer to use gets and then sscanf, e.g.

char buf[ 82 ];
int var1, var2, cnt;

printf( "prompt:" );
gets( buf ); // user enters two numbers and presses return

// sscanf returns the number of items processed from the
// buffer

cnt = sscanf( buf, "%d %d", &var1, &var2 );

if( cnt < 2 ) {
printf( "you should have entered two numbers\n" );
return 1;
}

....
 

emeraldstone

macrumors newbie
Original poster
Sep 7, 2003
7
0
Ireland
I'm sorry I made a mistake in the example code above, I left out the &, thats not what I'm doing in my source code.

I'll try to explain the problem again.

I can write code without the scanf() function, it compiles, and runs without problems. As soon as I place a scanf() function in the code it causes problems, it compiles, but when the program runs it waits for input when it should be printing text. as soon as it recives input it excutes the entire program when it shouldn't. It also makes the Project Builder window inactive. This is a problem with Project Builder, not the code. I need to know a way to get around it.

I'm using MacOS 10.2.3,
Project Builder 2.1
 

thegfunk

macrumors newbie
Sep 8, 2003
2
0
It sounds like the stdout isn't getting flushed out before the scanf... I forget the call (flush maybe?) but it would be worth a try to flush stdout before calling scanf.

You might also want to try runing it from the console if you haven't tried that.

Grant
 

emeraldstone

macrumors newbie
Original poster
Sep 7, 2003
7
0
Ireland
The problem is sorted. PB has a bug. The program runs perfect if I compile and run from the Terminal.
I read a previous post"Project Builder and C"

Thanks anyway
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.