| | |
Trying to get a program to extract data from a text file
![]() |
•
•
Join Date: Mar 2008
Posts: 8
Reputation:
Solved Threads: 0
Hey guys, I'm new here, I looked through the help also searched and couldn't find anything that helped. Although I will be using the Algorithms you guys have on this site.
anyways I only took a intro course to C++ about four years ago and don't remember much I wrote a program for a class and I can't get it to work I feel as though I am missing something.
What I'm trying to do is have the programs prompt the user for a node. Then the program refrence a text file with four coloums and x amount of nodes (I'll post the numbers i have). and it post all the row's that match that number.
Here's what I have so far
The text file is this
1 2 2 4
1 3 3 7
2 4 2 6
2 5 6 8
2 6 8 8
3 2 5 5
3 5 3 8
3 8 5 9
4 3 2 4
4 5 3 4
4 6 3 9
4 7 1 5
5 7 6 4
5 8 1 2
7 6 1 5
8 7 2 9
So for example what I'm trying to get it to do is if user enters 3 as the node in the prompt, it will display
3 2 5 5
3 5 3 8
3 8 5 9
If possible I would like to try and figure out how to have it display the titles on top of the numbers... for example
Tail Head Cost Capacity
3 2 5 5
3 5 3 8
3 8 5 9
Thanks for your guy's help!!
anyways I only took a intro course to C++ about four years ago and don't remember much I wrote a program for a class and I can't get it to work I feel as though I am missing something.
What I'm trying to do is have the programs prompt the user for a node. Then the program refrence a text file with four coloums and x amount of nodes (I'll post the numbers i have). and it post all the row's that match that number.
Here's what I have so far
c++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> using namespace std; int main() { int i = 0; int a; int head[16]; int tail[16]; int capacity[16]; int cost[16]; int count; ifstream inFile; // initalize the text file, and make sure it works inFile.open("fstarin.txt"); if (!inFile) { cout << "Unable to open file"<<endl; exit(1); // terminate with error } for (count= 0; count< 16; count = count++) { // input so it knows what each collum stands for inFile>>tail[count]>>head[count]>>cost[count]>>capacity[count]; } // enter node cout<<"enter a node"<<endl; cin>>a; // output node info regarding what node was entered for (i=0;i<15;i++) if (tail[i]==a) { cout<<tail<<head<<cost<<capacity<<endl; } return 0; }
The text file is this
1 2 2 4
1 3 3 7
2 4 2 6
2 5 6 8
2 6 8 8
3 2 5 5
3 5 3 8
3 8 5 9
4 3 2 4
4 5 3 4
4 6 3 9
4 7 1 5
5 7 6 4
5 8 1 2
7 6 1 5
8 7 2 9
So for example what I'm trying to get it to do is if user enters 3 as the node in the prompt, it will display
3 2 5 5
3 5 3 8
3 8 5 9
If possible I would like to try and figure out how to have it display the titles on top of the numbers... for example
Tail Head Cost Capacity
3 2 5 5
3 5 3 8
3 8 5 9
Thanks for your guy's help!!
>>cout<<tail<<head<<cost<<capacity<<endl;
use the loop counter in this line
>>If possible I would like to try and figure out how to have it display the titles on top of the numbers...
Simple -- print the titles just before line 36 (before the loop starts)
use the loop counter in this line
cout<<tai[i]l<<head[i]<<cost[i]<<capacity[i]<<endl; >>If possible I would like to try and figure out how to have it display the titles on top of the numbers...
Simple -- print the titles just before line 36 (before the loop starts)
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Mar 2008
Posts: 8
Reputation:
Solved Threads: 0
•
•
•
•
>>cout<<tail<<head<<cost<<capacity<<endl;
use the loop counter in this line
cout<<tai[i]l<<head[i]<<cost[i]<<capacity[i]<<endl;
>>If possible I would like to try and figure out how to have it display the titles on top of the numbers...
Simple -- print the titles just before line 36 (before the loop starts)
Ok now I have another problem I'm trying to solve...
I have two cpp files, one was written by "DJ" it's dijsktras algorithm, and I want to utilize my program into it.
I was trying to merge the two files together but couldn't get it to work... here is each file separate, and then what I tried to go to get it to work.
Any and all help would be amazing!
Here is what I tried
C++ Syntax (Toggle Plain Text)
#include<iostream.h> #include<conio.h> #include<stdio.h> #include<fstream> #define infi 999 int i = 0; int a; int head[16]; int tail[16]; int capacity[16]; int cost[16]; int count; ifstream inFile; class queue { private: int q[100]; int front, rear; protected: queue() { front=rear=-1; } int isempty() { if((front==-1 && rear==-1) || (front>rear)) { front=rear=-1; return 1; } return 0; } void push(int a) { if(isempty()) front++; q[++rear]=a; }; int del() { return q[front++]; } }; class dj :public queue { private: int mat[10][10], dist[10], path[10]; public: int n; void input() { int i = 0; int a; int head[16]; int tail[16]; int capacity[16]; int cost[16]; int count; ifstream inFile; // initalize the text file, and make sure it works inFile.open("fstarin.txt"); if (!inFile) { cout << "Unable to open file"; exit(1); // terminate with error } for (count= 0; count< 16; count = count++) { // input so it knows what each collum stands for inFile>>tail[count]>>head[count]>>cost[count]>>capacity[count]; } cout<<tail[count]; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) cin>>mat[i][j]; } void init(int m) { push(m); dist[m]=0; for(int i=1;i<=n;i++) { if(i!=m) { push(i); dist[i]=infi; } path[i]=0; } } void min_dist(int m) { int v, w; init(m); while(!(isempty())) { v=del(); for(int i=1;i<=n;i++) { if(mat[v][i]!=0) { w=i; if((dist[v]+mat[v][w])<(dist[w])) { dist[w]=dist[v]+mat[v][w]; path[w]=v; } } } } } void disp_path(int m) { int p=path[m]; if(p==0) return; disp_path(p); cout<<" "<<m; } void disp_dist(int m) { cout<<"cost: "<<dist[m]<<endl<<endl; } }; void main() { clrscr(); int c=0; dj a; a.input(); cout<<"\n\nPress any key to continue"<<endl; getch(); clrscr(); for(int i=1;i<=a.n;i++) { a.min_dist(i); for(int j=1;j<=a.n;j++) { if(i!=j) { if(++c==10) { cout<<"\n\nPress any key to continue"<<endl; getch(); clrscr(); c=0; } cout<<"From "<<i<<" to "<<j<<":"<<endl; cout<<"------------"<<endl; cout<<"Minimum distance route: ("<<i; a.disp_path(j); cout<<")"<<endl; a.disp_dist(j); } } } cout<<"\n\nPress any key to exit"<<endl; getch(); }
Here is the files for each, first is my file to refrence the nodes in the text file earlier in post and second is dijstras
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> using namespace std; int main() { int i = 0; int a; int head[16]; int tail[16]; int capacity[16]; int cost[16]; int count; ifstream inFile; // initalize the text file, and make sure it works inFile.open("fstarin.txt"); if (!inFile) { cout << "Unable to open file"; exit(1); // terminate with error } for (count= 0; count< 16; count = count++) { // input so it knows what each collum stands for inFile>>tail[count]>>head[count]>>cost[count]>>capacity[count]; } cout<<"enter a node"<<endl; cin>>a; for (i=0;i<15;i++) if (tail[i]==a) { cout<<tail[i]<<head[i]<<cost[i]<<capacity[i]<<endl; } return 0; }
C++ Syntax (Toggle Plain Text)
#include<iostream.h> #include<conio.h> #include<stdio.h> #define infi 999 class queue { private: int q[100]; int front, rear; protected: queue() { front=rear=-1; } int isempty() { if((front==-1 && rear==-1) || (front>rear)) { front=rear=-1; return 1; } return 0; } void push(int a) { if(isempty()) front++; q[++rear]=a; }; int del() { return q[front++]; } }; class dj :public queue { private: int mat[10][10], dist[10], path[10]; public: int n; void input() { cout<<"Enter number of nodes:\n"; cin>>n; cout<<"\n\nEnter adjacency matrix\n"<<endl; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) cin>>mat[i][j]; } void init(int m) { push(m); dist[m]=0; for(int i=1;i<=n;i++) { if(i!=m) { push(i); dist[i]=infi; } path[i]=0; } } void min_dist(int m) { int v, w; init(m); while(!(isempty())) { v=del(); for(int i=1;i<=n;i++) { if(mat[v][i]!=0) { w=i; if((dist[v]+mat[v][w])<(dist[w])) { dist[w]=dist[v]+mat[v][w]; path[w]=v; } } } } } void disp_path(int m) { int p=path[m]; if(p==0) return; disp_path(p); cout<<" "<<m; } void disp_dist(int m) { cout<<"Cost: "<<dist[m]<<endl<<endl; } }; void main() { clrscr(); int c=0; dj a; a.input(); cout<<"\n\nPress any key to continue"<<endl; getch(); clrscr(); for(int i=1;i<=a.n;i++) { a.min_dist(i); for(int j=1;j<=a.n;j++) { if(i!=j) { if(++c==10) { cout<<"\n\nPress any key to continue"<<endl; getch(); clrscr(); c=0; } cout<<"From "<<i<<" to "<<j<<":"<<endl; cout<<"------------"<<endl; cout<<"Minimum distance route: ("<<i; a.disp_path(j); cout<<")"<<endl; a.disp_dist(j); } } } cout<<"\n\nPress any key to exit"<<endl; getch(); }
Last edited by mofoparrot; Apr 14th, 2008 at 5:45 pm.
![]() |
Similar Threads
- Convert .wav file into bytes (HTML and CSS)
- how to update data in mysql table using data from an excel table (MySQL)
- Extracting fields from text file containing raw data.. (C)
- saving database file into a text file using vb6 (Visual Basic 4 / 5 / 6)
- extract columns from a text file in vb (Visual Basic 4 / 5 / 6)
- C++ Reading from a text file (C++)
- help reading from .txt file (C++)
- Need help with HJT log (Viruses, Spyware and other Nasties)
- I've got Trojan.Holax... is this bad? (Viruses, Spyware and other Nasties)
- not-a-virusadware (Viruses, Spyware and other Nasties)
Other Threads in the C++ Forum
- Previous Thread: Segmentation Core Dump
- Next Thread: character arrays...help please!!!
| Thread Tools | Search this Thread |
api array based binary bitmap business c++ c/c++ char class classes code coding commentinghelp compile console conversion count decide delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error faq file forms fstream function functions game givemetehcodez graph gui hash homeworkhelp homeworkhelper iamthwee ifpug ifstream incrementoperators infinite input int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem proficiency program programming project python random read recursion reference rpg string strings temperature template test text text-file tree url variable vector video win32 windows winsock word wordfrequency wxwidgets






