Scanf countdown - objective-c

I am making a scanf countdown via objective-C, so the program will count down from what ever number you input. However, there's an annoying semantic error in the code saying:Data argument not used by format string. Also the program doesn't countdown, it just displays the output as zero once I input a number.
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
#autoreleasepool {
int x,number;
NSLog(#"please enter a number:");
scanf("i", &number);
for (x = number; x>=0; x--)
NSLog(#"%i",x);
}
return 0;
}

You need to pass %i, not i in the format string of scanf.
When you pass i, the format string has zero format specifiers, leading to the semantic analyzer to produce a warning. That's also the reason why nothing gets entered into your number variable, so the countdown does not happen either.

Related

How to use scanf in a Swift environment

I am practicing Objective C to get a better understanding of C and was using the newest Xcode, but using the terminal to write simple programs. In the the program below is can't seem to get the scanf function to work. Is there a different function that I can use to input data into the terminal to check the rest of syntax and coding?
#import <Foundation/Foundation.h>
int main (int argc, char *argv[])
{
int n, number, triangularNumber;
NSLog (#"What triangular number do you want?");
scanf ("%i", &number);
triangularNumber = 0;
for ( n = 1; n <= number; ++n )
triangularNumber += n;
NSLog (#"Triangular number %i is %i\n", number, triangularNumber);
return 0;
}
You can't have a space in between the scanf and (). The scanf function should turn purple when done correctly. Just take out the space and you should be fine.
You can try this (this is Swift, Objective C is the same):
let handle = NSFileHandle.fileHandleWithStandardInput()
let input = NSString(data: handle.availableData, encoding: NSUTF8StringEncoding)

Using arg[v] as int? - Objective C

I'm trying to pass arguments into and integer variable at the start of my program in xcode using Objective C. So far most methods I've found have just given me a variety of error messages or crashed my VM.
int numcols, numrows;
int main(int argc, const char * argv[]) {
#autoreleasepool {
if (argc != 3){
NSLog(#"%2s", "Wrong number of arguments");
exit(1);
}
//numrows = (int)(argv[1] - '0');
//numcols = (int)(argv[2] - '0'); These lines cause a crash
numrows = 3;
numcols = 4;
I want numrows to take the first argument and numcols to take the second. I went into the settings in xcode to change the starting arguments to 3 and 4, but if I check them or print them they come out as random numbers.
argv is an array of C strings. You cannot just cast a string to an integer, you need to parse the string to produce the integer value it represents.
You could write code to do this yourself, but fortunately there are library functions which handle this, for example see strtol which will parse a string and produce a long.
HTH
First of all your question is not clear, how much i understand i can say. As argc means argument count and argv means argument value. If you want to cast char to int. try this
[[NSString stringWithFormat:#"%s", argv[2]] intValue];

scanf function in objective-c, float and double

I just a beginner in objective-C.
Below is a calculator of temperature.
I find a solution on the internet. The problem is the scanf.
At first, I set the f as a double, but program has problem.
So I change it to float.
May I ask what's going on on scanf function in objective-c?
Only can set character, int and float?
Another question is, what if I want to set a double, to use in another function which only accept double variable?
Thanks
import
int main(int argc, const char * argv[])
{
#autoreleasepool {
double c;
float f;
NSLog(#"Please enter F temp");
scanf("%f", &f);
c = (f-32) / 1.8;
//c = 1.3E-3;
// insert code here...
NSLog(#"The C temp is %.3f", c);
}
return 0;
}
Use %f for float and %lf for double. However be sure to check the return value from scanf() (or sscanf()) to ensure it parsed the correct number of values:
double d;
printf("Entry thy number mortal: ");
if (scanf("%lf", &d)) == 1) {
printf("Oh that's nice, you entered %f\n", d);
}

ObjC: Duplicate NSLog() output in the console when reading in char's with scanf()

And here I thought I was getting competent at ObjC, and this little C-type problem is giving me fits. :) This program is intended to read in a character from user input and print an expression that gives the character's decimal value. This program is generating duplicate NSLog() statements and I can't figure out why:
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
char a;
for (int i = 1; i <= 10; i++)
{
NSLog(#"Type in an ASCII character or type 'command-.' to exit.");
scanf("%c", &a);
NSLog(#"%c = %d", a, a);
}
[pool drain];
return 0;
}
Here's the output:
Type in an ASCII character or type 'command-.' to exit.
a
a = 97
Type in an ASCII character or type 'command-.' to exit.
= 10
Type in an ASCII character or type 'command-.' to exit.
When I change the read-in variable from a char to an int and make the according format specifier modification in the scanf(), the program runs in the console as intended. (Prompting the user for a character, printing out the value, and prompting again.) As soon as I go back to a char though, it does this. What am I doing wrong? Also, regardless of what type of char I enter, there's always the "= 10" output. What's the deal with that? Thanks in advance, guys.
"= 10" is the ascii code for the enter key.
So change your code into:
scanf("\n%c", &a);

simple code with EXC_BAD_ACCESS

I am new to the objective c and i write the code according to a reference book.
but something went wrong and I don't know why.
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
if (argc==1){
NSLog(#"you need to provide a file name");
return (1);
}
FILE *wordFile = fopen(argv[1], "r");
char word[100];
while(fgets(word , 100, wordFile)){
word[strlen(word)-1] = '\0';
NSLog(#"the length of the %s is %lu", word, strlen(word));
}
fclose(wordFile);
return 0;
}
the tool indicates that the while part went wrong, EXC_BAD_ACCESS.
Any idea?
It compiles and runs fine on my machine. But imagine you have an empty line in your file. Then strlen(word) will return zero. Hence word[strlen(word)-1] = '\0'; will try to set some memory which might not be valid since word[-1] might not be a valid memory cell, or a memory cell that you can legally access.
Oh, and by the way, it has nothing to do with objective-c. This is mostly (but for the NSLog call) pure ansi C.