Good day! I wrote a program, but it does not start, there are not even any error messages. Can you help? Actually, the program itself:

#include <iostream> #include <windows.h> #include <fstream> using namespace std; int keybard_enter(int matrix[3][7]) { fstream file1; file1.open("C:\\SOURCE\\Project\\matrix.txt", ios::out); int i, j; for (i = 0; i < 3; i++) { for (j = 0; j < 7; j++) { cin >> matrix[i][j]; file1 << matrix[i][j] << " "; } } cout << endl; file1.close(); return matrix[3][7];} int file_enter(int matrix[3][7]) { int i, j; fstream file1; file1.open("C:\\SOURCE\\Project\\matrix.txt", ios::in); cout << "Массив:" << endl; for (i = 0; i < 3; i++) { for (j = 0; j < 7; j++) { file1 >> matrix[i][j]; cout << matrix[i][j] << " "; } cout<<endl; } file1.close(); return matrix[3][7];} void out_matrix_file(int matrix[3][7]) { int i, j; fstream file1; file1.open("C:\\SOURCE\\Project\\end_matrix.txt", ios::out); file1 << "Конечная матрица:"; file1 << "\r"; for (i = 0; i < 3; i++) { file1 << "\n"; for (j = 0; j < 7; j++) { file1 << matrix[i][j] << " "; } file1 << "\r";} file1.close(); cout << "\nФайл успешно создан!\n\n"; } void rewrite_matrix (int matrix[3][7]) {char answer2; cout << "1. Найти положительные числа 2. Найти отрицательные числа 3. Найти нулевые числа " << endl; cin>>answer_2; switch (answer2) {case '1': int n=0; for(int i=0;i< 3;i++){ for(int j=0;j< 6;j++){ if(matrix[i][j]>0) {n=n+1;}}} fstream file1; file1.open("C:\\SOURCE\\qwerty.dot", ios::out); file1<<"Положительных чисел "<<n; file1.close(); break; case '2': int m=0; for(int i=0;i< 3;i++){ for(int j=0;j< 6;j++){ if(matrix[i][j]>0) m=m+1;}}} fstream file1; file1.open("C:\\SOURCE\\qwerty.dot", ios::out); file1<<"Отрицательных чисел "<<m; file1.close(); break; case '3': int k=0; for( int i=0;i< 3;i++){ for(int j=0;j< 6;j++){ if(matrix[i][j]>0) {k=k+1;}}} fstream file1; file1.open("C:\\SOURCE\\qwerty.dot", ios::out); file1<<"Нулевых чисел "<<k; file1.close(); break; default: cout << "Выберите 1, 2, 3!";break; } int main() { int matrix[3][7]; char answer1, answer2; setlocale(LC_ALL, "russian"); cout << "\n Подсчёт элементов.\n" << endl; inception: cout << "\nПрограмма подсчёта.\n" << endl; cout << "1. Загрузить массив из файла.\n2. Ввести вручную.\n" << endl; cin>>answer1; switch (answer1) { case '1': file_enter(matrix); break; case '2': keybard_enter(matrix); break; default: cout << "Выберите 1 или 2!"; goto inception; break; } cout << endl; for (;;) { cout << "\n1. Изменить элемент массива.\ \n2. Записать конечный массив в файл.\ \n3. Выйти из программы.\n" << endl; cin>>answer2; if (answer2 == '3') {break;} switch (answer2) { case '1': rewrite_matrix(matrix); break; case '2': out_matrix_file(matrix); break; default: cout << "\nВыберите 1, 2 или 4!\n"; break; } } return 0; } 
  • It seems that such a question already existed, but for some reason it was deleted) the code contains the use of the variable answer_2 but it is not declared anywhere. here's one of the compilation errors. I can not believe that the compiler did not give anything at all. - cybrex
  • Thank you, kind man. I was also surprised that they deleted. I'll fix it. Thanks again! - Ksenia
  • According to community rules, questions should not be reduced to completing tasks for students. Give an example of your implementation and a description of specific problems. - Nicolas Chabanovsky

1 answer 1

Here is the log, used the same Code :: Blocks (gcc-4.9.2)

||=== Build: Debug in test (compiler: GNU GCC Compiler) ===| D:\Development\Projects\C++\test\main.cpp||In function 'void rewrite_matrix(int (*)[7])':| D:\Development\Projects\C++\test\main.cpp|65|error: jump to case label [-fpermissive]| D:\Development\Projects\C++\test\main.cpp|60|error: crosses initialization of 'std::fstream file1'| D:\Development\Projects\C++\test\main.cpp|56|error: crosses initialization of 'int n'| D:\Development\Projects\C++\test\main.cpp|71|error: 'm' was not declared in this scope| D:\Development\Projects\C++\test\main.cpp|72|error: break statement not within loop or switch| D:\Development\Projects\C++\test\main.cpp|74|error: case label ''3'' not within a switch statement| D:\Development\Projects\C++\test\main.cpp|78|error: redeclaration of 'std::fstream file1'| D:\Development\Projects\C++\test\main.cpp|69|note: 'std::fstream file1' previously declared here| D:\Development\Projects\C++\test\main.cpp|81|error: break statement not within loop or switch| D:\Development\Projects\C++\test\main.cpp|82|error: case label not within a switch statement| D:\Development\Projects\C++\test\main.cpp|82|error: break statement not within loop or switch| ||=== Build failed: 10 error(s), 0 warning(s) (0 minute(s), 1 second(s)) ===|

Now in order:

  1. In the function void rewrite_matrix(int (*)[7]) ALL declarations of variables should be placed before the switch construction.

  2. Banal error: due to poorly designed code, the number of opening and closing brackets does not match.

Slightly put the code in order (corrected and brought to a readable form using the Source code formatter (AStyle) plugin built into the Code :: Blocks):

 void rewrite_matrix (int matrix[3][7]) { char answer2; int n=0; int m=0; int k=0; fstream file1; cout << "1. Найти положительные числа 2. Найти отрицательные числа 3. Найти нулевые числа " << endl; cin>>answer_2; switch (answer2) { case '1': for(int i=0; i< 3; i++) { for(int j=0; j< 6; j++) { if(matrix[i][j]>0) { n=n+1; } } } file1.open("C:\\SOURCE\\qwerty.dot", ios::out); file1<<"Положительных чисел "<<n; file1.close(); break; case '2': for(int i=0; i< 3; i++) { for(int j=0; j< 6; j++) { if(matrix[i][j]>0) m=m+1; } } file1.open("C:\\SOURCE\\qwerty.dot", ios::out); file1<<"Отрицательных чисел "<<m; file1.close(); break; case '3': for( int i=0; i< 3; i++) { for(int j=0; j< 6; j++) { if(matrix[i][j]>0) { k=k+1; } } } file1.open("C:\\SOURCE\\qwerty.dot", ios::out); file1<<"Нулевых чисел "<<k; file1.close(); break; default: cout << "Выберите 1, 2, 3!"; break; } } 

Just replace your void rewrite_matrix (int matrix[3][7]) function void rewrite_matrix (int matrix[3][7]) with the one I gave, and at the beginning after using namespace std; add std::string answer_2; .