944,078 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 744
  • C++ RSS
Oct 31st, 2009
0

Putting two characters together under special circumstances

Expand Post »
Hello Daniweb,

This is my first official "help" thread. I'm currently attempting to read a file that has the following code:

C++ Syntax (Toggle Plain Text)
  1. 8 split 1 9 1 spare 7 2 10 strike 8 2 spare 6 split 2 7 3 spare 10 strike 10 strike 10 turkey 8 1

and have it read only the integers, like this:

C++ Syntax (Toggle Plain Text)
  1. 8 1 9 1 7 2 1 0 8 2 6 2 7 3 1 0 1 0 1 0 8 1 1

However, as you can see, the 10's are being broken up into 1's and 0's because I'm drawing out characters.

How would I go about making it so that if a '1' is succeeded by a '0', they are displayed together without a space between them?

My code so far...

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstdlib>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. //defining variables
  9. const int MAX = 50;
  10. char c[MAX];
  11. char file;
  12.  
  13. //giving file names
  14. ifstream original;
  15. ofstream updated;
  16.  
  17. //opening input file
  18. original.open("lane9.dat");
  19. if(original.fail())
  20. {
  21. cout << "Input file was inaccessible.";
  22. exit(1);
  23. }
  24.  
  25. //opening output file
  26. updated.open("MGlane9.txt");
  27. if(updated.fail())
  28. {
  29. cout << "Outpur file was inaccessible.";
  30. exit(1);
  31. }
  32.  
  33. for(int i = 0; i < MAX; i++)
  34. {
  35. //end of file NOT reached before reading/writing
  36. while(!original.eof())
  37. {
  38. original >> file;
  39.  
  40. if(isdigit(file))
  41. {
  42. c[MAX] = file;
  43. cout << c[MAX] << " ";
  44. updated << file << " ";
  45. }
  46. }
  47. }
  48.  
  49. //closing files
  50. original.close();
  51. updated.close();
  52.  
  53. //closing program
  54. getchar();
  55. return 0;
  56. }

Without sounding like an complete idiot in front of you guys, my original theory was to create a for loop with an if statement if the char value is equal to one, and have another if statement inside of the first one if the number after value '1' is '0'. However, before I attempt to write it, I would like to see what the pro's think. Would that be the way to go about it?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
godsgift2dagame is offline Offline
22 posts
since Oct 2009
Oct 31st, 2009
-7
Re: Putting two characters together under special circumstances
Read the file one word at a time, instead of one charcter at a time. If the word contains digits then convert the entire word to int so that "10" becomed 10. line 10 should be an array of ints, not an array of char, so that it can contain ints beyond the range normally occupied by a single char.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,957 posts
since Aug 2005
Oct 31st, 2009
1
Re: Putting two characters together under special circumstances
apart from what A.D said, to get a little technical, you may want to try the peek() function. Check if that character is an alphabet or a number. if its a number . take in the number with the >> operator using an integer . and to get rid of the word. you could just you the >> operator using a string.
Reputation Points: 673
Solved Threads: 125
Practically a Posting Shark
Sky Diploma is offline Offline
818 posts
since Mar 2008
Oct 31st, 2009
0
Re: Putting two characters together under special circumstances
apart from what A.D said, to get a little technical, you may want to try the peek() function. Check if that character is an alphabet or a number. if its a number . take in the number with the >> operator using an integer . and to get rid of the word. you could just you the >> operator using a string.
I'll definitely have to mess with the peek() feature. It looks very interesting, almost seems like an isdigit() clone in regards to what it does.

Read the file one word at a time, instead of one charcter at a time. If the word contains digits then convert the entire word to int so that "10" becomed 10. line 10 should be an array of ints, not an array of char, so that it can contain ints beyond the range normally occupied by a single char.
I'm assuming that you mean use the atoi() feature?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
godsgift2dagame is offline Offline
22 posts
since Oct 2009
Oct 31st, 2009
1
Re: Putting two characters together under special circumstances
C++ Syntax (Toggle Plain Text)
  1.  
  2. #include <vector>
  3. #include <iostream>
  4. #include <fstream>
  5. #include <sstream>
  6. #include <iterator>
  7. #include <string>
  8. #include <cctype>
  9.  
  10. int main() {
  11. std::vector<int> arr;
  12. std::string str;
  13.  
  14. std::ifstream file("d:\\test.txt");
  15. if ( !file ) {
  16. std::cerr << "Can`t open file!" << std::endl;
  17. return 1;
  18. }
  19.  
  20. while ( std::getline(file, str, ' ') ) {
  21. if ( !isdigit(str[0]) ) continue;
  22. std::stringstream ss(str);
  23. int tmp = 0;
  24. ss >> tmp;
  25. arr.push_back(tmp);
  26. }
  27.  
  28. std::copy(arr.begin(), arr.end(), std::ostream_iterator<int>(std::cout, " "));
  29.  
  30. return 0;
  31. }
Reputation Points: 10
Solved Threads: 10
Light Poster
niXman is offline Offline
39 posts
since Oct 2009
Oct 31st, 2009
0
Re: Putting two characters together under special circumstances
niXman,

May I ask as to why you're not using "using namespace std;" below the library declarations instead of writing "std::" before everything?

EDIT: Is there a reason not to do it? My prof. told me you'll always use it.

EDIT2: I've got it to "WORK" but I'm not sure if this is a good, principle way of doing things. Can someone just look over the code and tell me if I did the code "efficiently"?

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstdlib>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. //defining variables
  9. string str;
  10. const int MAX = 50;
  11. char c[MAX];
  12. int file;
  13.  
  14. //giving file names
  15. ifstream original;
  16. ofstream updated;
  17.  
  18. //opening input file
  19. original.open("lane9.dat");
  20. if(original.fail())
  21. {
  22. cout << "Input file was inaccessible.";
  23. exit(1);
  24. }
  25.  
  26. //opening output file
  27. updated.open("MGlane9.txt");
  28. if(updated.fail())
  29. {
  30. cout << "Outpur file was inaccessible.";
  31. exit(1);
  32. }
  33.  
  34. for(int i = 0; i < MAX; i++)
  35. {
  36. //end of file NOT reached before reading/writing
  37. while(!original.eof())
  38. {
  39. original >> str;
  40.  
  41. if(isdigit(str[0]))
  42. {
  43. cout << str << " ";
  44. updated << str << " ";
  45. }
  46. }
  47. }
  48.  
  49. //closing files
  50. original.close();
  51. updated.close();
  52.  
  53. //closing program
  54. getchar();
  55. return 0;
  56. }

Thanks Ancient Dragon, Sky Diploma, and niXman!

Hope to be part of this community for a long time.
Last edited by godsgift2dagame; Oct 31st, 2009 at 2:11 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
godsgift2dagame is offline Offline
22 posts
since Oct 2009
Oct 31st, 2009
0
Re: Putting two characters together under special circumstances
Disclose the name space, not a good idea.
The first reason:
Suppose you use some third-party libraries, with some of them may be objects with matching names and signatures, disclosing their namespace, the compiler will give an error message.
The second reason:
Specifying a namespace, it is easier to navigate. Since you know exactly to which namespace object / function belongs.

PS
I apologize for my English, I'm from Moldova)
Reputation Points: 10
Solved Threads: 10
Light Poster
niXman is offline Offline
39 posts
since Oct 2009
Oct 31st, 2009
0
Re: Putting two characters together under special circumstances
Your English is perfect! Should I be worried about that as a first year student or do you think they'll teach us that down the road? The only thing is, I haven't seen anyone use std:: in my class yet and nothing is remotely as hard as what you people (programmer's) do.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
godsgift2dagame is offline Offline
22 posts
since Oct 2009
Oct 31st, 2009
0
Re: Putting two characters together under special circumstances
Quote ...
Can someone just look over the code and tell me if I did the code "efficiently"?
Too many unnecessary actions.
Reputation Points: 10
Solved Threads: 10
Light Poster
niXman is offline Offline
39 posts
since Oct 2009
Oct 31st, 2009
0

More efficient code for project

Is the code below any better?

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstdlib>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. //defining variables
  9. string str;
  10. int file;
  11.  
  12. //giving file names
  13. ifstream original;
  14. ofstream updated;
  15.  
  16. //opening input file
  17. original.open("lane4.dat");
  18. if(original.fail())
  19. {
  20. cout << "Input file was inaccessible.";
  21. exit(1);
  22. }
  23.  
  24. //opening output file
  25. updated.open("MGlane9.txt");
  26. if(updated.fail())
  27. {
  28. cout << "Output file was inaccessible.";
  29. exit(1);
  30. }
  31.  
  32. //end of file NOT reached before reading/writing
  33. while(!original.eof())
  34. {
  35. original >> str;
  36.  
  37. if(isdigit(str[0]))
  38. {
  39. updated << str << " ";
  40. }
  41. }
  42.  
  43. //closing files
  44. original.close();
  45. updated.close();
  46.  
  47. //closing program
  48. return 0;
  49. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
godsgift2dagame is offline Offline
22 posts
since Oct 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: Borland 5.5
Next Thread in C++ Forum Timeline: Arrays - Need help displaying an array.





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


Follow us on Twitter


© 2011 DaniWeb® LLC