473,324 Members | 2,268 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

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:
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
1 1996
weaknessforcats
9,208 Expert Mod 8TB
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

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

Similar topics

2
by: help | last post by:
I want to open a file and write some string between the the 3rd line and the 4th line. my code (as follows) can insert after the 3rd line but somehow it owerwrites the 4th line. So i lost its...
2
by: Pete | last post by:
Hi Could someone kindly help with the C# equivilent of the following 4 lines of C code. I'm *really* struggling with this. ( Colours.dat contains 300 RGB values ) COLORREF Colours;
4
by: jm0 | last post by:
Hi, im developing this program, and then i ran into some trouble "oooh no!" hate to ask.. have search google and this page for something to spilt things up in arrays or whatever can be used, spent...
4
by: Jason | last post by:
Could anyone spare some time and try to help me out. I've got a .txt data file with a name, pin, balance seperated by commas. I am opening the file and using Split to split it between the ,'s I...
3
by: xuxf055 | last post by:
Hi Everybody, I am confused for a few days, how to read the data file seperated by comma. for exampe following file. data.txt 1,2,3,4,5,6,7,8,9,10 11,12,13,14,15,16,17,18,19,20 how to read it...
7
by: ianenis.tiryaki | last post by:
well i got this assignment which i dont even have a clue what i am supposed to do. it is about reading me data from the file and load them into a parallel array here is the question: Step (1) ...
4
by: personguy | last post by:
I'm trying to read one column at a time from this data file: 24551 145.5 62.3 2.13 24582 153.7 63.0 2.52 26553 160.4 58.8 2.51 26613 159.5 58.9 2.02 26624 160.5 61.5 1.98 27725 170.9 62.5...
1
by: radskate360 | last post by:
Hi I am newer to programming and need a bit of help with this program. OK, heres the directions. The distance between two places on earth can be calculated by using their latitudes and...
6
by: trl10 | last post by:
Hi Everyone, I'm still trying to learn C++ and this is my final project. We have to read sales data from a file into an array, process data in a partially filled array via functions, pass an array...
1
by: supahsain | last post by:
I got this assignment, and almost have no clue, and my teacher doesn't even teaches us anything This is what i am suppose to do...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.