cstring arrays

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kylie991
    New Member
    • Feb 2009
    • 41

    cstring arrays

    Hi there just wondering if I am on the right track. I need to load a file in a program and then store the stata in an array. I am now testing this function to see if it is working... when I run the functioni the following happens.

    1) the first number in the txt file that I open is missing. And when I set the list to < 82 (as there is 81 numbers in total) it will show the data and then say there was an error. in fact once it hits over 30 it will come up with this problem. How do I fix that??

    2) also how do I get about including the first number in the file being included.

    Am I on the right track?

    Code:
    int loadFile()
    {
     ifstream infile;
       string filename;
       float list = 0;
       cout << "Enter file name " << endl;
       cin >> filename;
       infile.open(filename.c_str());
       if (infile.fail())
       {
          cout << "File not found " << endl;
          return EXIT_FAILURE;
       }
       string s;
       infile >> filename[list];
       while (infile && list >= 0 && list <9)
        {
           infile >> filename[list];
           cout << filename[list];     
           list++;    
        }
     
       return EXIT_SUCCESS;
    }
    Last edited by JosAH; Feb 27 '09, 07:14 AM. Reason: fixed the [code] ... [/code] tags
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    you read the first character but don't print it. Remove the read before the while loop, e.g.
    Code:
       //infile >> filename[list];
       while (infile && list >= 0 && list <9)
        {
           infile >> filename[list];
           cout << filename[list];
           list++;
        }
    I am not sure what you are tring to do. at present you are reading the data as single characters into a string - if the data is numeric you could read it into an array of ints or floats

    Comment

    • kylie991
      New Member
      • Feb 2009
      • 41

      #3
      wow thanks I didnt realise that that first part took the first character out. Thank you so much. it has been bugging me all day!!

      What I am trying to do is prompt my function and read in a file name and then open the file and read in the grid. I then need to store the array (pass by reference) for a second function so that I can actually display the grid... but at the moment I just want to see if I am getting this first part right.... am i making sense??

      So yes it is a string of characters. I need to read the data and output it 9 per line (81 characters in total can be assumed) At the moment I have it set to <=9 because it seems to work this way however I need it to read all characters. if I type <= 80 the program comes up with an error and closes down.

      does that make sense??

      Comment

      • kylie991
        New Member
        • Feb 2009
        • 41

        #4
        maybe this will make it clearer? I have barely started the program.. I just want to try and work out how to do the first part of it.

        using namespace std;

        void printMenu();
        void loadFile();
        void displayFile();
        void test();
        int main()
        {


        char choice = '*';
        while (choice != 'Q')
        {
        printMenu();
        cin >> choice;
        switch (toupper(choice ))
        {
        case 'L' : loadFile();
        break;
        case 'D' : displayFile();
        break;
        case 'R' : // complete this
        case 'C' : // complete this
        case 'M' : // complete this
        case 'Q' : break;
        default : cout << "Invalid option " << endl; cin.ignore(100, '\n');
        }

        }

        return 0;
        }


        void printMenu()
        {
        cout << "\n\tSudoku Checker " << endl << endl;
        cout << "\t L\t Load file " << endl;
        cout << "\t D\t Display " << endl;
        cout << "\t C \t Check columns " << endl;
        cout << "\t R \t Check rows " << endl;
        cout << "\t M \t Check minigrids" << endl;
        cout << "\t Q\t Quit " << endl;
        cout << " Rows and columns are labelled from 0 to 8 " << endl;
        cout << endl;
        }

        void loadFile()
        {
        ifstream infile;
        string filename;
        float list = 0;
        cout << "Enter file name " << endl;
        cin >> filename;
        infile.open(fil ename.c_str());
        if (infile.fail())
        {
        cout << "File not found " << endl;
        return EXIT_FAILURE;
        }
        string s;
        //infile >> filename[list];
        while (infile && list >= 0 && list <80)
        {
        infile >> filename[list];
        cout << filename[list];
        list++;
        }
        //note need help around here as will cout the whole list.. except for the first number and if I make the while terms over 20 it will corrupt.....?
        // cout << filename[list];
        //system("pause") ;
        return EXIT_SUCCESS;
        }

        Comment

        • horace1
          Recognized Expert Top Contributor
          • Nov 2006
          • 1510

          #5
          as I understand it you want to read in a 9 * 9 array of characters
          you could have a char array 9*9 and read the characters into it, e.g.
          Code:
          #include <iostream>
          #include <string>
          #include <fstream>
          
          using namespace std;
          
          int loadFile(char data[9][9])
          {
           ifstream infile;
             string filename;
             float list = 0;
             cout << "Enter file name " << endl;
             cin >> filename;
             infile.open(filename.c_str());
             if (infile.fail())
             {
                cout << "File not found " << endl;
                return EXIT_FAILURE;
             }
             for(int i=0;i<9;i++)
               for(int j=0;j<9;j++)
                 infile >> data[i][j];
             return EXIT_SUCCESS;
          }
          
          int displayFile(char data[9][9])
          {
              for(int i=0;i<9;i++)
               {
               for(int j=0;j<9;j++)
                 cout << data[i][j];
               cout << endl;
               }
          }
          
          int main()
          {
           char data[9][9];
           loadFile(data);
           displayFile(data);
          }
          reads the following file and prints it
          Code:
          123456789
          123456789
          123456789
          123456789
          123456789
          123456789
          123456789
          123456789
          123456789
          could you give us a sample of your data?

          Comment

          • kylie991
            New Member
            • Feb 2009
            • 41

            #6
            you are brilliant, thanks so much. that makes sense.. why didnt I think of adding a float instead of int ?

            I need to input a space inbetween the output numbers in the grid

            eg.
            1 2 3 4 5 6 7 8 9
            1 2 3 . . . . . ..

            why does it not like it when I do the following? (note: I added a space between array i and j.

            for(int i=0;i<9;i++)
            {
            for(int j=0;j<9;j++)
            cout << data[i] << " " << [j];
            cout << endl;
            }

            Comment

            • kylie991
              New Member
              • Feb 2009
              • 41

              #7
              dont worry I worked it out.. woohoo thanks so much!!! :)

              Comment

              • kylie991
                New Member
                • Feb 2009
                • 41

                #8
                do you still want a sample of my data.. this is only part of what I have to do.. but I want to try to work out the whole thing first :) I know it wont be easy but you have helped me out a LOT

                Comment

                • horace1
                  Recognized Expert Top Contributor
                  • Nov 2006
                  • 1510

                  #9
                  I assume you used skipws from <iomanip> to skip the spaces in your file, e.g.
                  Code:
                     for(int i=0;i<9;i++)
                       for(int j=0;j<9;j++)
                         infile >> skipws >> data[i][j];

                  Comment

                  • kylie991
                    New Member
                    • Feb 2009
                    • 41

                    #10
                    no i didnt actually.. I have never heard of that before probably because I am fairly new to programming... I did the following:
                    infile >> data[i][j] >> " ";

                    Comment

                    Working...