Putting two characters together under special circumstances

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2009
Posts: 17
Reputation: godsgift2dagame is an unknown quantity at this point 
Solved Threads: 0
godsgift2dagame godsgift2dagame is offline Offline
Newbie Poster

Putting two characters together under special circumstances

 
0
  #1
Oct 31st, 2009
Hello Daniweb,

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

  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:

  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...

  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?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,679
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: 1504
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning
 
-7
  #2
Oct 31st, 2009
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.
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: 678
Reputation: Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold 
Solved Threads: 101
Sky Diploma's Avatar
Sky Diploma Sky Diploma is offline Offline
Practically a Master Poster
 
1
  #3
Oct 31st, 2009
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.
1. Please Mark Your Thread as Solved After Getting Your Answers.
2. Please Use CODE TAGS .
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 17
Reputation: godsgift2dagame is an unknown quantity at this point 
Solved Threads: 0
godsgift2dagame godsgift2dagame is offline Offline
Newbie Poster
 
0
  #4
Oct 31st, 2009
Originally Posted by Sky Diploma View Post
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.

Originally Posted by Ancient Dragon View Post
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?
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 39
Reputation: niXman is an unknown quantity at this point 
Solved Threads: 10
niXman's Avatar
niXman niXman is offline Offline
Light Poster
 
1
  #5
Oct 31st, 2009
  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. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 17
Reputation: godsgift2dagame is an unknown quantity at this point 
Solved Threads: 0
godsgift2dagame godsgift2dagame is offline Offline
Newbie Poster
 
0
  #6
Oct 31st, 2009
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"?

  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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 39
Reputation: niXman is an unknown quantity at this point 
Solved Threads: 10
niXman's Avatar
niXman niXman is offline Offline
Light Poster
 
0
  #7
Oct 31st, 2009
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)
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 17
Reputation: godsgift2dagame is an unknown quantity at this point 
Solved Threads: 0
godsgift2dagame godsgift2dagame is offline Offline
Newbie Poster
 
0
  #8
Oct 31st, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 39
Reputation: niXman is an unknown quantity at this point 
Solved Threads: 10
niXman's Avatar
niXman niXman is offline Offline
Light Poster
 
0
  #9
Oct 31st, 2009
Can someone just look over the code and tell me if I did the code "efficiently"?
Too many unnecessary actions.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 17
Reputation: godsgift2dagame is an unknown quantity at this point 
Solved Threads: 0
godsgift2dagame godsgift2dagame is offline Offline
Newbie Poster

More efficient code for project

 
0
  #10
Oct 31st, 2009
Is the code below any better?

  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. }
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum


Views: 379 | Replies: 9
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC