Search code examples
ccs50

I have an error displayed called: error: expected expression and I don't know what's the ide expecting


My code goes as follows:

#include <stdio.h>
#include <cs50.h>

int main (void)
{
    string name = get_string("What's your name? ");
    int age = get_int("What's your age? ");
    int phone = get_int("what's your phone number? ");
    do
    {
    char answer = get_char("Are the information bellow correct? (y or n): \n Name: %s \n Age: %i \n Phone: %i", name, age, phone);
    }
    while (char answer = y | char answer = n);

}

the error is in line 13 while (char answer = y || char answer = n); for "expected expression". Anyone knows what should I do in this case? I also would appreciate a feedback on the way I wrote line 13, is that the correct way to get either one of the characters I'm looking for?


Solution

  • Issues:

    • PROBLEM: "y" and "n" weren't defined as variables, hence the compile error.

      SOLUTION: Use character literals instead

    • Use "||" and "&&" for "conditionals" (logical operators), and use "|" and "&" for "boolean arithmetic".

    • You declared variable "answer" locally, inside your "do{}" block. You should declare it OUTSIDE the braces.

    EXAMPLE:

    char answer;
    do {
        answer = get_char("Is the information below correct? (y or n): \n Name: %s \n Age: %i \n Phone: %i", name, age, phone);
    } while (answer != 'y' && answer != 'n');
    

    }