Trying to get a program to extract data from a text file

Reply

Join Date: Mar 2008
Posts: 8
Reputation: mofoparrot is an unknown quantity at this point 
Solved Threads: 0
mofoparrot mofoparrot is offline Offline
Newbie Poster

Trying to get a program to extract data from a text file

 
0
  #1
Mar 7th, 2008
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

  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4.  
  5.  
  6. int main()
  7. {
  8.  
  9. int i = 0;
  10. int a;
  11. int head[16];
  12. int tail[16];
  13. int capacity[16];
  14. int cost[16];
  15. int count;
  16. ifstream inFile;
  17.  
  18. // initalize the text file, and make sure it works
  19. inFile.open("fstarin.txt");
  20. if (!inFile)
  21. {
  22. cout << "Unable to open file"<<endl;
  23. exit(1);
  24. // terminate with error
  25. }
  26.  
  27. for (count= 0; count< 16; count = count++)
  28. {
  29. // input so it knows what each collum stands for
  30. inFile>>tail[count]>>head[count]>>cost[count]>>capacity[count];
  31. }
  32. // enter node
  33. cout<<"enter a node"<<endl;
  34. cin>>a;
  35. // output node info regarding what node was entered
  36. for (i=0;i<15;i++)
  37. if (tail[i]==a)
  38. {
  39. cout<<tail<<head<<cost<<capacity<<endl;
  40. }
  41.  
  42.  
  43.  
  44.  
  45. return 0;
  46. }

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!!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,161
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1437
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Trying to get a program to extract data from a text file

 
0
  #2
Mar 7th, 2008
>>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)
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 8
Reputation: mofoparrot is an unknown quantity at this point 
Solved Threads: 0
mofoparrot mofoparrot is offline Offline
Newbie Poster

Re: Trying to get a program to extract data from a text file

 
0
  #3
Apr 14th, 2008
Originally Posted by Ancient Dragon View Post
>>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)
Thanks,

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
  1. #include<iostream.h>
  2. #include<conio.h>
  3. #include<stdio.h>
  4. #include<fstream>
  5. #define infi 999
  6.  
  7.  
  8. int i = 0;
  9. int a;
  10. int head[16];
  11. int tail[16];
  12. int capacity[16];
  13. int cost[16];
  14. int count;
  15. ifstream inFile;
  16.  
  17. class queue
  18. {
  19. private:
  20. int q[100];
  21. int front, rear;
  22. protected:
  23. queue()
  24. {
  25. front=rear=-1;
  26. }
  27. int isempty()
  28. {
  29. if((front==-1 && rear==-1) || (front>rear))
  30. {
  31. front=rear=-1;
  32. return 1;
  33. }
  34. return 0;
  35. }
  36. void push(int a)
  37. {
  38. if(isempty())
  39. front++;
  40. q[++rear]=a;
  41. };
  42. int del()
  43. {
  44. return q[front++];
  45. }
  46. };
  47. class dj :public queue
  48. {
  49. private:
  50. int mat[10][10], dist[10], path[10];
  51. public:
  52. int n;
  53. void input()
  54. {
  55.  
  56. int i = 0;
  57. int a;
  58. int head[16];
  59. int tail[16];
  60. int capacity[16];
  61. int cost[16];
  62. int count;
  63. ifstream inFile;
  64.  
  65. // initalize the text file, and make sure it works
  66. inFile.open("fstarin.txt");
  67. if (!inFile) {
  68. cout << "Unable to open file";
  69. exit(1); // terminate with error
  70. }
  71.  
  72. for (count= 0; count< 16; count = count++)
  73. {
  74. // input so it knows what each collum stands for
  75. inFile>>tail[count]>>head[count]>>cost[count]>>capacity[count];
  76. }
  77. cout<<tail[count];
  78. for(int i=1;i<=n;i++)
  79. for(int j=1;j<=n;j++)
  80. cin>>mat[i][j];
  81. }
  82. void init(int m)
  83. {
  84. push(m);
  85. dist[m]=0;
  86. for(int i=1;i<=n;i++)
  87. {
  88. if(i!=m)
  89. {
  90. push(i);
  91. dist[i]=infi;
  92. }
  93. path[i]=0;
  94. }
  95. }
  96. void min_dist(int m)
  97. {
  98. int v, w;
  99. init(m);
  100. while(!(isempty()))
  101. {
  102. v=del();
  103. for(int i=1;i<=n;i++)
  104. {
  105. if(mat[v][i]!=0)
  106. {
  107. w=i;
  108. if((dist[v]+mat[v][w])<(dist[w]))
  109. {
  110. dist[w]=dist[v]+mat[v][w];
  111. path[w]=v;
  112. }
  113. }
  114. }
  115. }
  116. }
  117. void disp_path(int m)
  118. {
  119. int p=path[m];
  120. if(p==0)
  121. return;
  122. disp_path(p);
  123. cout<<" "<<m;
  124. }
  125. void disp_dist(int m)
  126. {
  127. cout<<"cost: "<<dist[m]<<endl<<endl;
  128. }
  129. };
  130. void main()
  131. {
  132. clrscr();
  133. int c=0;
  134. dj a;
  135. a.input();
  136. cout<<"\n\nPress any key to continue"<<endl;
  137. getch();
  138. clrscr();
  139. for(int i=1;i<=a.n;i++)
  140. {
  141. a.min_dist(i);
  142. for(int j=1;j<=a.n;j++)
  143. {
  144. if(i!=j)
  145. {
  146. if(++c==10)
  147. {
  148. cout<<"\n\nPress any key to continue"<<endl;
  149. getch();
  150. clrscr();
  151. c=0;
  152. }
  153. cout<<"From "<<i<<" to "<<j<<":"<<endl;
  154. cout<<"------------"<<endl;
  155. cout<<"Minimum distance route: ("<<i;
  156. a.disp_path(j);
  157. cout<<")"<<endl;
  158. a.disp_dist(j);
  159. }
  160. }
  161. }
  162. cout<<"\n\nPress any key to exit"<<endl;
  163. getch();
  164. }


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

  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4.  
  5.  
  6. int main()
  7. {
  8.  
  9. int i = 0;
  10. int a;
  11. int head[16];
  12. int tail[16];
  13. int capacity[16];
  14. int cost[16];
  15. int count;
  16. ifstream inFile;
  17.  
  18. // initalize the text file, and make sure it works
  19. inFile.open("fstarin.txt");
  20. if (!inFile) {
  21. cout << "Unable to open file";
  22. exit(1); // terminate with error
  23. }
  24.  
  25. for (count= 0; count< 16; count = count++)
  26. {
  27. // input so it knows what each collum stands for
  28. inFile>>tail[count]>>head[count]>>cost[count]>>capacity[count];
  29. }
  30. cout<<"enter a node"<<endl;
  31. cin>>a;
  32.  
  33. for (i=0;i<15;i++)
  34. if (tail[i]==a)
  35. {
  36. cout<<tail[i]<<head[i]<<cost[i]<<capacity[i]<<endl;
  37. }
  38.  
  39.  
  40.  
  41.  
  42. return 0;
  43. }






  1. #include<iostream.h>
  2. #include<conio.h>
  3. #include<stdio.h>
  4. #define infi 999
  5. class queue
  6. {
  7. private:
  8. int q[100];
  9. int front, rear;
  10. protected:
  11. queue()
  12. {
  13. front=rear=-1;
  14. }
  15. int isempty()
  16. {
  17. if((front==-1 && rear==-1) || (front>rear))
  18. {
  19. front=rear=-1;
  20. return 1;
  21. }
  22. return 0;
  23. }
  24. void push(int a)
  25. {
  26. if(isempty())
  27. front++;
  28. q[++rear]=a;
  29. };
  30. int del()
  31. {
  32. return q[front++];
  33. }
  34. };
  35. class dj :public queue
  36. {
  37. private:
  38. int mat[10][10], dist[10], path[10];
  39. public:
  40. int n;
  41. void input()
  42. {
  43. cout<<"Enter number of nodes:\n";
  44. cin>>n;
  45. cout<<"\n\nEnter adjacency matrix\n"<<endl;
  46. for(int i=1;i<=n;i++)
  47. for(int j=1;j<=n;j++)
  48. cin>>mat[i][j];
  49. }
  50. void init(int m)
  51. {
  52. push(m);
  53. dist[m]=0;
  54. for(int i=1;i<=n;i++)
  55. {
  56. if(i!=m)
  57. {
  58. push(i);
  59. dist[i]=infi;
  60. }
  61. path[i]=0;
  62. }
  63. }
  64. void min_dist(int m)
  65. {
  66. int v, w;
  67. init(m);
  68. while(!(isempty()))
  69. {
  70. v=del();
  71. for(int i=1;i<=n;i++)
  72. {
  73. if(mat[v][i]!=0)
  74. {
  75. w=i;
  76. if((dist[v]+mat[v][w])<(dist[w]))
  77. {
  78. dist[w]=dist[v]+mat[v][w];
  79. path[w]=v;
  80. }
  81. }
  82. }
  83. }
  84. }
  85. void disp_path(int m)
  86. {
  87. int p=path[m];
  88. if(p==0)
  89. return;
  90. disp_path(p);
  91. cout<<" "<<m;
  92. }
  93. void disp_dist(int m)
  94. {
  95. cout<<"Cost: "<<dist[m]<<endl<<endl;
  96. }
  97. };
  98. void main()
  99. {
  100. clrscr();
  101. int c=0;
  102. dj a;
  103. a.input();
  104. cout<<"\n\nPress any key to continue"<<endl;
  105. getch();
  106. clrscr();
  107. for(int i=1;i<=a.n;i++)
  108. {
  109. a.min_dist(i);
  110. for(int j=1;j<=a.n;j++)
  111. {
  112. if(i!=j)
  113. {
  114. if(++c==10)
  115. {
  116. cout<<"\n\nPress any key to continue"<<endl;
  117. getch();
  118. clrscr();
  119. c=0;
  120. }
  121. cout<<"From "<<i<<" to "<<j<<":"<<endl;
  122. cout<<"------------"<<endl;
  123. cout<<"Minimum distance route: ("<<i;
  124. a.disp_path(j);
  125. cout<<")"<<endl;
  126. a.disp_dist(j);
  127. }
  128. }
  129. }
  130. cout<<"\n\nPress any key to exit"<<endl;
  131. getch();
  132. }
Last edited by mofoparrot; Apr 14th, 2008 at 5:45 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC