please help to with this program by mainly using arrays and pointers ,... i need this program to check 2 matrix files "if they contain any character and if they contain cout<<"invalid"; " and to open them from .txt file

#include<iostream>
#include<fstream>
#include<string>
#include<cmath>
using namespace std;

int main()
{
 char y, n; 
 ifstream file1;
 ifstream file2;
 string filename1;
 string filename2;
 string c;
 int number;
 int choice;

cout<<"xxxxxxx matrix calculator \n";
cout<<"please choose from the following menu :\n";

while(choice!=8&&choice==n)
{
 cout<<"1. matrix addition\n";
 cout<<"2. matrix subtraction\n";
 cout<<"3. matrix multiplication\n";
 cout<<"4. matrix transpose\n";
 cout<<"5. checking matrix equality\n";
 cout<<"6. check matrix properties\n";
 cout<<"7. matrix power\n";
 cout<<"8. exit\n";
 cout<<"please enter your choice\n";
 cin>>choice;

 while(//condition when there is character)// for the first file and the second one :D
         {                   
                            cout<<"invalid file!!\n";
                            cout<<"do you want to continue (y) or return to menu(n)\n"; 
/* "y" to continue to try to enter the file name again & "n" to go back to the menu*/
                            cin>>choice1;
                            if(choice1=='y')
                            {
                             cout<<"please enter the first filename\n";
                         cin>>filename1;
                          file1.open(filename1.c_str(),ios::in);
                          
                                 if(!file1.fail())
                                 {
                                                 cout<<"good\n";
                                                 break;
                                                 }
                          }
                          else if(choice1=='n')
                          {
                               cout<<"assdasdasdad\n";
                              break;
                               }   
                               }
         


 switch(choice)
{
 case 1 :
          
 case 2 :
      
 case 3 :
      
 case 4 :
 
 case 5 :
 
 case 6 :
      
 case 7 :
 
 case 8 :
      cout<<"thank you for using my program\n";
      exit(1);
      }

 system ("pause");
 return 0; 
}
}

Dani AI

Generated

— make the file format and the validation rules explicit, then write a small, strict parser. A reliable convention is: first two integers are row and column counts, followed by exactly rows*cols numeric tokens (whitespace separated). This makes it trivial to detect non-numeric content (for example a stray cout<< "invalid"; will fail parsing). As noted, initialize menu/state variables and validate every user/file input before using it.

Example parser (uses C-style number validation so you can detect leftover alphabetic characters). This code demonstrates arrays + pointers and reports failure when any token is not a pure number:

#include <fstream>
#include <string>
#include <cstdlib>
#include <cerrno>

bool parseNumber(const std::string& s, double &out) {
    if (s.empty()) return false;
    char *end = nullptr;
    errno = 0;
    out = strtod(s.c_str(), &end);
    if (end == s.c_str()) return false;   // nothing converted
    if (*end != '\0') return false;       // trailing non-numeric chars
    if (errno == ERANGE) return false;    // overflow/underflow
    return true;
}

bool loadMatrix(const std::string &fname, double *&mat, int &rows, int &cols) {
    std::ifstream f(fname.c_str());
    if (!f.is_open()) return false;
    if (!(f >> rows >> cols)) return false;
    if (rows <= 0 || cols <= 0) return false;
    mat = new double[rows * cols];
    for (int i = 0; i < rows * cols; ++i) {
        std::string tok;
        if (!(f >> tok)) { delete[] mat; mat = nullptr; return false; }
        double val;
        if (!parseNumber(tok, val)) { delete[] mat; mat = nullptr; return false; }
        mat[i] = val;
    }
    return true;
}

Use the allocated array as a flat buffer: element (r,c) is mat[r * cols + c]. Always delete[] the buffer when done or on error. Test with simple files like:

3 2
1 2
3 4
5 6

Troubleshooting tips: try deliberately malformed files (letters, extra tokens, missing numbers) to confirm the parser prints/returns invalid; trim BOMs if files come from Windows editors; check locale (decimal separator). Initialize menu variables and loop only after validating input (as suggested). This approach keeps parsing simple, makes invalid files easy to detect, and satisfies the “arrays and pointers” requirement.

First of all: a thread called helpzz plzzz fast cuz im NOOB, is not really a great way to explain your problem. Clear titles make people help you faster.

Next: Please use code tags. It makes your code easier to read

Please click

Then for your program. There are quite a lot of things wrong with it.
For example: char y,n; you are declaring 2 chars named 'y' and 'n', but they never get a value. Choice never gets a input. What you want is something like:

char choice = 'x'; //give it some value
	while (choice < '0' || choice > '8') 	
               choice = cin.get(); // loop until good input

Adjust the code above to fit your program.

commented: Say it bro, say it loud and often! +15
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.