Esempio n. 1
0
/*
 * The function which returns string entered by user.
*
 * @return equation - the equation string.
*/
string getUserString() {
    string equation;
    do {
        cout << "Enter the equation: (to exit, enter 'q' as the first character)" << endl;
        //cin.ignore(); // just for sync io streams
        getline(cin, equation);
        if (inputIsCorrect(equation)) {
            if (DEBUG)
                cout << ": user equation [" << equation << "]" << endl;
            break;
        }

    } while (true);

    return equation;
}
Esempio n. 2
0
int main(int argc, char * argv[]) {
    nameOfFile = argv[1];
    data = fopen(nameOfFile, "a+");
    if (data == NULL)
        perror("Error");
    phBook.size = 0;
    phBook.contact = malloc(phBook.size * sizeof(Note));
    freeInd.top = 0;
    freeInd.size = 1;
    freeInd.num = (int *)malloc(freeInd.size * sizeof(int));
    rewind(data);
    int id;
    while (fscanf(data, "%d ", &id) == 1) {
        phBook.size++;
        phBook.contact = realloc(phBook.contact, phBook.size * sizeof(Note));
        phBook.contact[phBook.size - 1].id = id;
        phBook.contact[phBook.size - 1].name = getword(data);
        phBook.contact[phBook.size - 1].nmbr = getword(data);
    }
    if (phBook.size == 0)
        currentID = 1;
    else
        currentID = phBook.contact[phBook.size - 1].id + 1;
    char *cmd, *name, *number, *input;
    while (42) {
        cmd = getword(stdin);
        if (!strcmp(cmd, "find")) {
            input = getword(stdin);
            if (inputIsCorrect(input))
                find(input);
            free(input);
        }
        else if (!strcmp(cmd, "create")) {
            name = getword(stdin);
            number = getword(stdin);
            if (inputIsCorrect(name) && inputIsCorrect(number))
                create(name, number);
        }
        else if (!strcmp(cmd, "delete")) {
            scanf("%d\n", &id);
            del(id);
        }
        else if (!strcmp(cmd, "change")) {
            scanf("%d ", &id);
            cmd = getword(stdin);
            input = getword(stdin);
            if (inputIsCorrect(input))
                change(id, cmd, input);
            free(input);
        }
        else if (!strcmp(cmd, "exit")) {
            fclose(data);
            free(phBook.contact);
            free(freeInd.num);
            return 0;
        }
        else {
            printf("Unknown command.\n");
        }
        free(cmd);
        fflush(stdout);
    }
}