Маленький жучок в коду
#include <cstdio>
#include <iostream>
#include <string>
#include <unistd.h>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <cstring>
using namespace std;
int PasswordArmor() {
long int a, password, my;
cin >> a;
password = rand() % a;
while (my != password) {
printf("Enter your password: ");
cin>> my;
if (my == password) {
return 0;
} else {
printf("Invalid password!\n");
}
}
return 0;
}
int NameArmor() {
string names[4] = {
"Leha Khorsun", "Dima Khorsun",
"Vlad Khorsun", "Olga Fomkina"
};
string name;
printf("Enter your name: ");
cin >> name;
for (auto a : names) {
if (name == a) {
return 0;
}
}
return 1;
}
int Armor() {
if (PasswordArmor() == 0 && NameArmor() == 0) {
return 0;
}
return 1;
}
void Calculator() {
double a, b, c;
char op;
printf("Calculator\n");
scanf("%lf %c %lf", &a, &op, &b);
switch(op) {
case '+':
c = a + b;
break;
case '-':
c = a - b;
break;
case '*':
c = a * b;
break;
case '/':
c = a / b;
break;
case '^':
c = pow(a, b);
break;
case '%':
c = fmod(a, b);
break;
default:
printf("There isn't such operator in my list! Sorry!");
}
cout << a << " " << op << " " << b << " = " << c << endl;
}
void Timer() {
long long int time;
bool ready = false;
printf("Enter time you need to wait: ");
scanf("%lli", &time);
while (ready != true) {
usleep(999999);
time -= 1;
printf("%lli \n", time);
if (time <= 0) {
ready = true;
}
}
printf("Ready!\n");
return;
}
int main() {
if (Armor() == 0) {
string act1;
printf("Enter activity you need: ");
cin >> act1;
size_t actlen = sizeof(char) * act1.length();
char *act = (char*) malloc(actlen);
for (int i = 0; i < actlen; i++) {
act[i] = act1[i];
}
for (int i = 0; i < actlen; i++) {
act[i] = tolower(act[i]);
}
cout << '\n';
if (strncmp(act, "calculator", actlen)) {
Calculator();
} else if (strncmp(act, "timer", actlen)) {
Timer();
} else {
cout << "Sorry, but we haven't " << act << "!" << endl;
}
printf("Thank's for using us!");
} else {
return 1;
}
}
Тут какой-тот баг (скорее всего в функции PasswordArmor), но но отображается только такая ошибка:

Помогите, пожалуйста!
Ответы (1 шт):
Автор решения: Harry
→ Ссылка
Проблема у вас в том, что вы вводите два слова, но читается-то одно -
string name;
printf("Enter your name: ");
cin >> name;
for (auto a : names) {
if (name == a) {
return 0;
}
}
Так что у вас возвращается и в конечном счете и main возвращает 1... О чем и сообщается на экране.
Но вот поясните глубокий смысл вот этого:
string act1;
printf("Enter activity you need: ");
cin >> act1;
size_t actlen = sizeof(char) * act1.length();
char *act = (char*) malloc(actlen);
for (int i = 0; i < actlen; i++) {
act[i] = act1[i];
}
for (int i = 0; i < actlen; i++) {
act[i] = tolower(act[i]);
}
cout << '\n';
if (strncmp(act, "calculator", actlen)) {
Calculator();
Если можно было просто и незатейливо:
string act1;
printf("Enter activity you need: ");
cin >> act1;
if (act1 == "calculator") ...
В крайнем случае, добавить
transform(act1.begin(), act1.end(), act1.begin(),
[](unsigned char c){ return tolower(c); });