943,694 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 860
  • C++ RSS
Mar 7th, 2008
0

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

Expand Post »
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

c++ Syntax (Toggle Plain Text)
  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!!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mofoparrot is offline Offline
8 posts
since Mar 2008
Mar 7th, 2008
0

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

>>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)
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,950 posts
since Aug 2005
Apr 14th, 2008
0

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

>>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
C++ Syntax (Toggle Plain Text)
  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

C++ Syntax (Toggle Plain Text)
  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. }






C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mofoparrot is offline Offline
8 posts
since Mar 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Segmentation Core Dump
Next Thread in C++ Forum Timeline: character arrays...help please!!!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC