blockaj
blockaj

Reputation: 329

I keep getting three errors when trying to define a character array

Errors

Instance variable is already declared

Type name requires a specifier or a qualifier

Expected ';' at end of declaration list

I can't figure out why this is happening. Please help!

Here is my code:

ViewController.m

#import "evalViewController.h"

@interface evalViewController () {
    digits[10] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; //Errors: Instance variable is already declared, Type name requires a specifier or a qualifier, Expected ';' at end of declaration list 

    }

@end

@implementation evalViewController 

@synthesize terminal;
@synthesize expression; 

//Add iEval prefix to output
-(void)writeToTerminal:(NSString *)string {
    self.terminal.text = [NSString stringWithFormat:@"ConsoleCalc> %@", string];
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    //Define operation characters
    self.add = '+';
    self.sub = '-';
    self.mult = '*';
    self.div = '/';

    //Define digits to compare to digits in editableExpression 
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)evaluate:(UITextField *)sender {
    //Set user input to NSString editableExpression
    NSString *editableExpression = self.expression.text;

    //Loop through new NSString
    for (int i=0; i < editableExpression.length; i++ ){
        char charAtPosition = [editableExpression characterAtIndex:i];
        //Check if there is a + sign anywhere in the expression and write "Addition!" if there is
        if (charAtPosition == self.add){
            if ([self checkForDigits]){
                 [self writeToTerminal:@"There are digits in this string!"];
            }
            else {
                 [self writeToTerminal:@"There are no digits in this string!"];
             }
                    
         }
         //Check if there is a - sign anywhere in the expression and write "Subtraction!" if there is
        else if (charAtPosition == self.sub) {
             [self writeToTerminal:@"Subtraction!"];
        
         }
    
        //Check if there is a * sign anywhere in the expression and write "Multiplication!" if there is
        else if (charAtPosition == self.mult) {
             [self writeToTerminal:@"Multiplication!"];
        }
    
        //Check if there is a / sign anywhere in the expression and write "Division!" if there is
        else if (charAtPosition == self.div) {
            [self writeToTerminal: @"Division!"];
        }

    }

 }

//clear the terminal

- (IBAction)clearTerminal:(id)sender {
     [self writeToTerminal:@""];
 }

- (BOOL)checkForDigits {
    NSString *editableExpression = self.expression.text;
    for (int i = 0; i < editableExpression.length; i++){
        char charAtPosition = [editableExpression characterAtIndex:i];
        for (int c = 0; c < 10; c++ ){
            if (charAtPosition == digits[c]){
                return true;
            }
            else {
                return false;
            }
        }
    


    }
}

@end

ViewController.h

#import <UIKit/UIKit.h>

@interface evalViewController : UIViewController {
     char digits[10];

 }

//Large text view
@property (strong, nonatomic) IBOutlet UITextView *terminal;

//User input
@property (strong, nonatomic) IBOutlet UITextField *expression;

//Evaluate expression
- (IBAction)evaluate:(UITextField *)sender;

- (IBAction)clearTerminal:(id)sender;

//Operation characters
@property char add;
@property char sub;
@property char mult;
@property char div;

//Change to information view
 - (void)writeToTerminal: (NSString *) string;
 - (BOOL)checkForDigits;




@end

Upvotes: 0

Views: 121

Answers (2)

Can Poyrazoğlu
Can Poyrazoğlu

Reputation: 34780

You are declaring the array incorrectly. Delete it from the header completely, and declare the digits char array only in the implementation file (.m file). The error should then go away. Also, you are assigning the value incorrectly. You are creating a char array of 10 elements, then you are *assigning another char array to the element at the 10th index, which doesn't exist anyway. There are multiple errors there. It should bedigits = {...}notdigits[10] = {...}`.

If I were you I would first start learning plain C and get the basics before moving into Objective-C, as it would only complicate more. I see that you want to create iOS apps, but to make that process easier, you first need to know the basics of C well.

Upvotes: 2

sbarow
sbarow

Reputation: 2819

My wording might be wrong here but you are initialising digits in your interface. Do this.

You can do this in your .m file (and no need for the @interface evalViewController() @end just showing you where to put it)

char digits[10] = { ... };

@interface evalViewController ()

@end

Upvotes: 0

Related Questions