in Education by
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 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; } JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
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"); }

Related questions

0 votes
    I receive a SIGABRT when I try to get a non-null variable. Code: NSLog(@"%@", appDelegate.xmlData ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    WAP in java to input 3 numbers from user and print the largest no. among them using nested if. Select the correct answer from above options...
asked Dec 19, 2021 in Education by JackTerrance
0 votes
    How can I generate a string of size N. I want it to be of uppercase English letters and numbers like this: 7I2D86 ... do it in Python. Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    How will you change case for all letters in string?...
asked Nov 26, 2020 in Technology by JackTerrance
0 votes
    So the idea is that users can create collections with many pairs of words in it. For example, they ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 14, 2022 in Education by JackTerrance
0 votes
    To build SQL statements it is more secure to user PreparedStatement than Statement. (1)True (2)False...
asked May 15, 2021 in Education by JackTerrance
0 votes
    Write a program to accept string from user and search a given character from string without using find() function Select the correct answer from above options...
asked Nov 30, 2021 in Education by JackTerrance
0 votes
    If the letters of the word ‘RAMLEELA’ are arranged in random. What is the probability that it begins with REEL. Select the correct answer from above options...
asked Nov 25, 2021 in Education by JackTerrance
0 votes
    Have you ever created dashboard using procedures with input parameter? If your answer is yes, definitely the next question will be how?...
asked Nov 21, 2020 in Technology by JackTerrance
0 votes
    java program to input any string then print each word along with their length Select the correct answer from above options...
asked Dec 31, 2021 in Education by JackTerrance
0 votes
    WAP in java to input 2 numbers from user and print their difference (Always Positive). Select the correct answer from above options...
asked Dec 19, 2021 in Education by JackTerrance
0 votes
    Just started working with Python and I would like to know how to create a set only from user input ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 17, 2022 in Education by JackTerrance
0 votes
    How can I ask the user to enter a value during the execution of a GnuPlot script? I would like ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 17, 2022 in Education by JackTerrance
0 votes
    I have an XML file which is structured as follows: place1 location1 place2 location2 place3 location3 These ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 2, 2022 in Education by JackTerrance
...