main.m 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. //
  2. // main.m
  3. // chartest
  4. //
  5. // Created by Kevin Bradley on 9/24/23.
  6. // Copyright © 2023 nito. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. /*
  10. + (NSString *)getNumberWithLimit:(NSInteger)limit {
  11. NSString *errorString = @"\nChoose a number, 0 to exit. [1]: ";
  12. char c;
  13. printf("%s", [errorString UTF8String] );
  14. c=getchar();
  15. while (c > limit) {
  16. printf("c = %c", c);
  17. c=getchar();
  18. }
  19. printf("c = %c", c);
  20. return @"";
  21. }
  22. */
  23. @interface Dumb: NSObject
  24. + (NSInteger)getNumberFromPrompt:(NSString *)prompt;
  25. @end
  26. @implementation Dumb
  27. + (NSInteger)getNumberFromPrompt:(NSString *)prompt {
  28. char c;
  29. char buf[64];
  30. int i = 0;
  31. printf("%s", [prompt UTF8String]);
  32. c=getchar();
  33. while (c != '\n') {
  34. buf[i] = c;
  35. i++;
  36. c=getchar();
  37. }
  38. buf[i] = '\0';
  39. return atoi(buf);
  40. }
  41. @end
  42. int main(int argc, const char * argv[]) {
  43. @autoreleasepool {
  44. // insert code here...
  45. NSInteger limit = 20;
  46. NSInteger number = [Dumb getNumberFromPrompt:@"\nChoose a number, 0 to exit. [1]: "];
  47. NSLog(@"number: %lu", number);
  48. while (number > limit) {
  49. number = [Dumb getNumberFromPrompt:@"\nChoose a number, 0 to exit. [1]: "];
  50. NSLog(@"number: %lu", number);
  51. }
  52. }
  53. return 0;
  54. }