How to open a string data file, reads, and assign to an array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • star01daisy
    New Member
    • May 2010
    • 2

    How to open a string data file, reads, and assign to an array

    This is what i have, but when i run the program the only thing that comes out is the inro line "This program opens...". What do i have to do for it to run smoothly.The program is supposed to sort the data either in ascending or descending order depending on the user’s choice. Finally, the sorted data is stored in a data file as well as shown on the screen.

    this is what i have so far:
    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    using namespace std;
    
    // Function declaration
    
    void intro (); 
    void getData (string name[], int& item); 
    void ascending (string name[], int item); 
    void descending (string name[], int item);
    void output (string name[], int item);
    int getChoice();
    
    // Main Function
    
    int main()
    {
       string name[100], temp;
       int item, choice;
       intro ();
    
    
       getData(name, item);
       
       choice = getChoice();
    
       if (choice == '1'|| choice == '1')
          ascending(name, item);
       if (choice == '2'|| choice == '2')
          descending(name, item);
       
       output(name, item);
     }
    
    
    
    //Intro to the program
    
    void intro () 
    {
    	cout << "This program opens a string data file, reads the data from that file" <<endl;
    	cout <<"and assigns them to an array" <<endl<<endl;
    
    }
    
    // Input Choice
    
    int getChoice()
    {
       int choice;
       cout << "Please enter 1 for ascending." <<endl;
       cout << "or 2 for descending." <<endl<<endl;
       cin >> choice;
       return choice;
    }
    
    // Get Data
    
    void getData(string name[], int& item)
    {
       ifstream  fin;
       fin.open("E:\\p9.txt");
    
       item = 0;
    
       while (!fin.eof())
       {
          fin >> name[item];
    	  item++;    
       }
       cout << "item = " << item << endl << endl;
    }
    
    // Ascending
    
    void ascending(string name[], int item)
    {
       string temp;
    
       for(int j=0; j<item-1; j++)
       {
          for(int i=0; i<item-1; i++)
             if(name[i] > name[i+1])
    		 {
    		    temp      = name[i];
    			name[i]   = name[i+1];
    		    name[i+1] = temp;
    		 }
       }
    }
    
    // Descending
    
    void descending(string name[], int item)
    {
       string temp;
       for(int j=0; j<item-1; j++)
       {
          for(int i=0; i<item-1; i++)
             if(name[i] < name[i+1])
    		 {
    		    temp      = name[i];
    			name[i]   = name[i+1];
    		    name[i+1] = temp;
    		 }
       }
    }
    
    
    
    // Output
    
    void output(string name[], int item)
    {
       for(int i=0; i<item; i++)
       cout << name[i] << endl;
       cout << endl << endl;
    }
    Last edited by RedSon; May 28 '10, 05:24 PM. Reason: Put [CODE] tags around your code next time! This is a warning!
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Now is the time to learn how to use your debugger. That will verify if your functions are being called and whether the array is filled correctly and whether your sort works.

    You are using the >> operator but you never check that it worked. There should be a call cin.good() or cin.fail() after each >> call. If cin.good() returns false or cin.fail() returns true, then your input stream is on a fail state. If this happens all subsequent >> will fail. The effect looks like the >> operations have been removed from the program. Please try this.

    Comment

    Working...