By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,264 Members | 1,318 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,264 IT Pros & Developers. It's quick & easy.

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

P: 2
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:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. using namespace std;
  5.  
  6. // Function declaration
  7.  
  8. void intro (); 
  9. void getData (string name[], int& item); 
  10. void ascending (string name[], int item); 
  11. void descending (string name[], int item);
  12. void output (string name[], int item);
  13. int getChoice();
  14.  
  15. // Main Function
  16.  
  17. int main()
  18. {
  19.    string name[100], temp;
  20.    int item, choice;
  21.    intro ();
  22.  
  23.  
  24.    getData(name, item);
  25.  
  26.    choice = getChoice();
  27.  
  28.    if (choice == '1'|| choice == '1')
  29.       ascending(name, item);
  30.    if (choice == '2'|| choice == '2')
  31.       descending(name, item);
  32.  
  33.    output(name, item);
  34.  }
  35.  
  36.  
  37.  
  38. //Intro to the program
  39.  
  40. void intro () 
  41. {
  42.     cout << "This program opens a string data file, reads the data from that file" <<endl;
  43.     cout <<"and assigns them to an array" <<endl<<endl;
  44.  
  45. }
  46.  
  47. // Input Choice
  48.  
  49. int getChoice()
  50. {
  51.    int choice;
  52.    cout << "Please enter 1 for ascending." <<endl;
  53.    cout << "or 2 for descending." <<endl<<endl;
  54.    cin >> choice;
  55.    return choice;
  56. }
  57.  
  58. // Get Data
  59.  
  60. void getData(string name[], int& item)
  61. {
  62.    ifstream  fin;
  63.    fin.open("E:\\p9.txt");
  64.  
  65.    item = 0;
  66.  
  67.    while (!fin.eof())
  68.    {
  69.       fin >> name[item];
  70.       item++;    
  71.    }
  72.    cout << "item = " << item << endl << endl;
  73. }
  74.  
  75. // Ascending
  76.  
  77. void ascending(string name[], int item)
  78. {
  79.    string temp;
  80.  
  81.    for(int j=0; j<item-1; j++)
  82.    {
  83.       for(int i=0; i<item-1; i++)
  84.          if(name[i] > name[i+1])
  85.          {
  86.             temp      = name[i];
  87.             name[i]   = name[i+1];
  88.             name[i+1] = temp;
  89.          }
  90.    }
  91. }
  92.  
  93. // Descending
  94.  
  95. void descending(string name[], int item)
  96. {
  97.    string temp;
  98.    for(int j=0; j<item-1; j++)
  99.    {
  100.       for(int i=0; i<item-1; i++)
  101.          if(name[i] < name[i+1])
  102.          {
  103.             temp      = name[i];
  104.             name[i]   = name[i+1];
  105.             name[i+1] = temp;
  106.          }
  107.    }
  108. }
  109.  
  110.  
  111.  
  112. // Output
  113.  
  114. void output(string name[], int item)
  115. {
  116.    for(int i=0; i<item; i++)
  117.    cout << name[i] << endl;
  118.    cout << endl << endl;
  119. }
May 28 '10 #1
Share this Question
Share on Google+
1 Reply


weaknessforcats
Expert Mod 5K+
P: 9,197
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.
May 30 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.