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: -
#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;
-
}
1 1996
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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;
|
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...
|
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...
|
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...
|
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)
...
|
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...
|
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...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
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....
|
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
|
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...
| | |