0

Helllo I am still new to programing and had a question about using if statements while using user input with the research I have conducted i can't seem to find what I am doing wrong? Below is my posted simple multiplication calculator.

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
int a ;
int b ;
int c ;
printf("\n");
printf("\n");
printf("Welcome to calculator");
printf("\n");
printf("\n");
printf("what would you like to choose for first value?");
scanf("%d", &a);
printf("\n");
printf("What would you like to input for the second value?");
scanf("%d", &b);
c = a * b;
printf("\n");
printf("\n");
printf(" Here is your product");
printf("\n");
NSLog(@"a * b =%i", c); 

char userinput ;
char yesvari = "yes" ;
char novari = "no";

printf("\n");
printf("\n");
printf("Would you like to do another calculation?");
scanf("%i", &userinput);



if (userinput == yesvari) {
    NSLog(@" okay cool");



}

if (userinput == novari) {

    NSLog(@"okay bye");
}

return 0; }

C noob
  • 21
  • 1
  • 4

3 Answers3

2

You are scanning the character incorrectly with %i and you need to compare them using strcmp. If you are looking for a string from the user you need to use %s and you need a character buffer large enough to hold the input.

Try this

//Make sure userinput is large enough for 3 characters  and null terminator
char userinput[4];

//%3s limits the string to 3 characters
scanf("%3s", userinput);

//Lower case the characteres
for(int i = 0; i < 3; i++)
    userinput[i] = tolower(userinput[i]);

//compare against a lower case constant yes
if(strcmp("yes", userinput) == 0)
{
    //Logic to repeat
    printf("yes!\n");
}
else
{
    //Lets just assume they meant no
    printf("bye!\n");
}
Joe
  • 56,979
  • 9
  • 128
  • 135
  • thanks btw and i get everything u said except for the for (int i = 0; i < 3; i++) userinput[i] = tolower(userinput[i]); – C noob Jul 23 '11 at 03:17
  • are you just saying that the program is just scanning for lowercase character? – C noob Jul 23 '11 at 03:18
  • `tolower()` will lower case the userinput so it wont fail if they enter "YES" or "Yes". It only works on 1 character at a time so you just need to loop over each character. Also your example is in all C, no Objective-C calls which would make what you are trying to do easier. – Joe Jul 23 '11 at 13:35
  • really okay thats great to know. I got all of the functions and the syntax from an object-C book thanks for al the help!!!! – C noob Jul 23 '11 at 23:50
1

I think you are reading a char using the wrong format %i: scanf("%i", &userinput);

And I think it is a better to use @NSString instead of simple char (I am not even sure what will happen in ObjC if you write char a = "asd", since you are giving a char a char[] value) . In that case, since strings are pointers, you cannot use == to compare them. You could use isEqualToString or isEqualTo instead. If you are interested in the difference between the two, look at this post would help.

Community
  • 1
  • 1
zw324
  • 26,764
  • 16
  • 85
  • 118
0

In C, you can't compare strings using ==, so you would have to use a function like strcmp(), like this:

if ( !strcmp(userinput, yesvari) ) {
   //etc.
}

The bang (!) is used because strcmp() actually returns 0 when the two strings match. Welcome to the wonderful world of C!

Monolo
  • 18,205
  • 17
  • 69
  • 103
  • thanks guys soo much i am literately learning from a book and stack overflow tho is a lot of help – C noob Jul 23 '11 at 02:50