Problem with reading in a word

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MariyaGel
    New Member
    • Nov 2006
    • 7

    Problem with reading in a word

    I have wrote the program and it worked fine until I had to include one more array into it. As you can see below the two arrays of volume and mass are working properly now I need to include a third array which reads in the sample name that coincides with the mass and the volume. I have tried the getline function and that didnt work. This is my first class in computer programming and im very new to it so if i have made obviouse mistakes i apologize. I tried as much as i can myself and Im not exactly sure how to proceed. Please help

    Here is the program:
    Code:
    # include <string>
    # include <iostream>
    # include <fstream>
    using namespace std;
    void readinput (double[], double[], string, int, ifstream &infile);
    void printvalues (double[], double[], string, int, ofstream &outfile);
    void linearsort (double[], double[], string, int);
    void bubblesort (double[], double[], string, int);
    int line_search (double[], double, int);
    int main()
    {
        ofstream outfile ("prog7.txt");
        ifstream infile ("assignment7.txt");
        int n, position;
        if (infile.bad())
        {
           cout<<"unable to read from file";
           exit(1);
        }
        infile>>n;
        double mass[n], volume[n], newnumber; 
        string name;  
        readinput(mass, volume, name, n, infile);
        cout<<name[0]<<name[1]<<name[2]<<name[3]<<name[4]<<name[5];
        printvalues(mass, volume, name, n, outfile);
        linearsort(mass, volume, name, n);
        printvalues(mass, volume, name, n, outfile);
        bubblesort(volume, mass, name, n);
        printvalues(mass, volume, name, n, outfile);     
        while(infile>>newnumber)
        {
              position=line_search(mass, newnumber, n);
              if (position == -1)
              {
                  cout<<"The number "<<newnumber
                      <<" does not occure in the mass array"<<endl;  
              }
              else
              {
                  cout<<newnumber<<" is found at position "<<position
                      <<". The volume of that sample is "<<volume[position]<<endl;
              }
              position=line_search(volume, newnumber, n);
              if (position == -1)
              {
                  cout<<"The number "<<newnumber
                      <<" does not occure in the volume array"<<endl;
              }
              else
              {
                  cout<<newnumber<<" is found at position "<<position
                      <<". The mass of that sample is "<<mass[position]<<endl;
              }
              cout<<endl;
        }       
        infile.close();
        outfile.close();
        getchar();
        return 0; 
    }
     
        
    //Function readinput:
    //Input: 2 arrays, int n, and infile
    //Process: Read the data values from the input values into 2 arrays.
    void readinput (double a[], double b[], string c[], int n, ifstream &infile)
    {
         for (int i=0; i<n; i++)
              infile>>a[i]>>b[i]>>c[i];
         return;
    }//read input
        
    
    
    //Function bubblesort:
    //Input: 2 arrays
    //Process: Sorts the arrays in ascending order of volume
    void bubblesort (double a[], double b[], string c[], int n)
    {
           double temp, temp2;
           string temp3;
           bool swapped;
           do
           {
                swapped = false;
                for (int i=0; i<n-1; i++)
                     if (a[i] > a[i+1])
                     {
                        temp = a[i];
                        temp2 = b[i];
                        temp3 = c[i];
                        a[i] = a[i+1];
                        b[i] = b[i+1];
                        c[i] = c[i+1];
                        a[i+1] = temp;
                        b[i+1] = temp2;
                        c[i+1] = temp3;
                        swapped = true;
                     }
           }
           while (swapped);
           return;
    }//bubblesort
    
    
    //Function linearsort:
    //Input: 2 arrays
    //Process: Sorts the arrays in descending order of mass
    void linearsort (double a[], double b[], string c[], int n)
    {
         double temp, temp2;
         string temp3;
         for (int i=0; i<n-1; i++)
              for (int j=i+1; j<n; j++)
                   if (a[i] < a[j])
                   {
                      temp = a[i];
                      temp2 = b[i];
                      temp3 = c[i];
                      a[i] = a[j];
                      b[i] = b[j];
                      c[i] = c[j];
                      a[j] = temp;
                      b[j] = temp2;
                      c[j] = temp3;
                   }
         return;
    }// linearsort
    
    
    //Function linsearch:
    //Input: double array, number to be searched for and n
    //Process: Determines whether or not the input number occurs in the array
    //Output: The position of that number or -1 if it cannot be found
    int line_search (double a[], double newnumber, int n)
    {
        for (int position=0; position<n; position++)
             if (a[position] == newnumber)
                 return position;
        return -1;
    }//line_search
    
    
    //Function printvalues:
    //Input: 2 arrays, int n and outfile
    //Process: To print the arrays into a table.
    void printvalues (double a[], double b[], string c[], int n, ofstream &outfile)
    {
         outfile<<"        Collected Data"<<endl;
         outfile<<"Mineral      Mass      Volume"<<endl;
         for (int i=0; i<n; i++)
         {
              outfile.setf(ios_base::fixed, ios_base::floatfield);
              outfile.precision(2);
              outfile.setf(ios::left);
              outfile.width(12);
              outfile<<c[i]<<a[i]<<b[i]<<endl;
         }      
         outfile<<endl<<endl;
         return;
    }//printvalues
    Last edited by Banfa; Nov 30 '06, 03:21 AM. Reason: Changed B tags to code tags
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    I think you problem is that using you syntax

    string name;

    should be

    string name[n];

    In fact you have notably left out the array specifiers [] in a lot of places where you are passing the name parameter.

    Additionally the code

    double mass[n];

    doesn't compile for me as standard C++ does not support dynamically sized arrays (not sure which version you are using though).

    Comment

    • MariyaGel
      New Member
      • Nov 2006
      • 7

      #3
      That was absolutely correct I can't believe I missed that. It worked out perfectly , Im using Dev-C++ and double mass[n] workes with no problem. I just got confused how i was suppose to declare an array of words, I wasnt sure you can have an array or words and not just characters.
      Thank you very much

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Originally posted by MariyaGel
        Im using Dev-C++ and double mass[n] workes with no problem.
        That is fine but you had better be sure that your tutor/lecturer is too (assuming you have one.

        ...

        Now on a point of style you have code similar to the following

        Code:
            temp = a[i];
            temp2 = b[i];
            temp3 = c[i];
            a[i] = a[i+1];
            b[i] = b[i+1];
            c[i] = c[i+1];
            a[i+1] = temp;
            b[i+1] = temp2;
            c[i+1] = temp3;
        In both of your sort routines. Now this code works (which is why it is a style point and not a logic point) but this is not very elegant, this code logical performs

        For array a swop the values located at i and i+1
        For array b swop the values located at i and i+1
        For array c swop the values located at i and i+1

        which you have written long handed. To neaten it you could easily write Swop functions for the 2 types of variable use (double and string) something with the prototypes

        void Swop(double &op1, double &op2);
        void Swop(string &op1, string &op2);

        these functions would probably embody the code you already have (i.e. use a temporary variable) and then the code becomes

        Code:
            Swop(a[i], a[i+1]);
            Swop(b[i], b[i+1]);
            Swop(c[i], c[i+1]);
        which looks much better and says what is happening.

        If you really wanted to get snazzy rather than write 2 functions you could write a single template function

        template <class T>
        void Swop(T &op1, T &op2);

        but that may be a little bit complex for a beginner.

        Comment

        Working...