There is a program, I can not understand what the compiler does not like. In Java, I would do this in 5 minutes. I will forgive forgiveness if I’m frankly stupid somewhere, because I started communicating with C ++ only today. In theory, the main method should call the randomSpeak method.

#include <iostream> #include <locale.h> #include <windows.h> #include <stdlib.h> using namespace std; int main() { setlocale(LC_ALL, "rus"); int i; for (i = 0; i < 20; i++) { cout << "\a" << endl; int randomInt = 0 + rand()%10; //Строка ниже упорно выдаёт ошибку randomSpeak(randomInt); Sleep(1000); } return 0; } void randomSpeak (int s) { if (s == 0) { cout << "0&?!?!?!?" << endl; } if (s == 1) { cout << "1!" << endl; } if (s == 2) { cout << "Винда повисла!" << endl; } if (s == 3) { cout << "3!!!" << endl; } if (s == 4) { cout << "Надо было ставить Linux!" << endl; } if (s == 5) { cout << "5 Microsoft!" << endl; } if (s == 6) { cout << "6 и винда готова!" << endl; } if (s == 7) { cout << "format c:/ is starting..." << endl; } if (s == 8) { cout << "8!!" << endl; } if (s == 9) { cout << "9!!" << endl; } if (s == 10) { cout << "10" << endl; } } 
  • 2
    What kind of mistake? In the meantime, Wang, that you need to move the Maine below the randomSpeak function - Andrey Kurulev

2 answers 2

Wangtu, that the error is that main does not know about the function randomSpeak .

There are two ways to fix it.

The first is to simply transfer the entire definition to the randomSpeak function above the main function.

The second is to declare the randomSpeak function before main . To do this, you need to add one line above the main one, leaving everything else unchanged:

 void randomSpeak (int s); 
  • Thank! Thank you very much! It worked. From now on I will know about this particular advantage! Thank you - Chris Rackfield

Define the main method after the randomSpeak method. In Java, everything will work for you, but in C ++ you can use only the declared methods.