943,727 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 9499
  • C++ RSS
Mar 30th, 2005
0

strings as input, substr and len

Expand Post »
i'm currently writing a program that needs one small piece in my opinion in order to work. i have an array which reads in a bunch of numbers (call them addresses, an example is "003124") and counts the amount of addresses at the moment. i need this to be passed into a switch statement that does certain things for each number in each address, i've written a mix of code/pseudocode
C++ Syntax (Toggle Plain Text)
  1. entil EOF do;
  2. string = input;
  3. len = length(string);
  4. for i=1 to len do
  5. ch = substr(string, i-1, 1)
  6. switch(ch)
  7. //case statements
i want each "string" in the input to be a single address. does anyone know how to do this? i'll provide more information if needed.
thanks in advance.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rborob is offline Offline
9 posts
since Mar 2005
Mar 30th, 2005
0

Re: strings as input, substr and len

First, I have grown to hate the "while not at end of file" stuff; I now prefer "while successfully obtaining the data I request from the file" approach. Anyways, here is a possible starting point.
C++ Syntax (Toggle Plain Text)
  1. /* file.txt
  2. 003124
  3. */
  4. #include <iostream>
  5. #include <fstream>
  6. #include <string>
  7.  
  8. int main ()
  9. {
  10. std::ifstream file("file.txt");
  11. std::string input;
  12. while ( std::getline(file, input) )
  13. {
  14. for ( std::string::size_type i = 0, len = input.size(); i < len; ++i )
  15. {
  16. switch ( input[i] )
  17. {
  18. case '0': std::cout << "zero" << '\n'; break;
  19. case '1': std::cout << "one" << '\n'; break;
  20. case '2': std::cout << "two" << '\n'; break;
  21. case '3': std::cout << "three" << '\n'; break;
  22. default: std::cout << "other" << '\n'; break;
  23. }
  24. }
  25. }
  26. return 0;
  27. }
  28.  
  29. /* my output
  30. zero
  31. zero
  32. three
  33. one
  34. two
  35. other
  36. */
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Mar 30th, 2005
0

Re: strings as input, substr and len

>I have grown to hate the "while not at end of file" stuff
Me too. Mostly because in C and C++ it's a subtle and dangerous error.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 31st, 2005
0

Re: strings as input, substr and len

ok well since i've counted all the addresses in a previous function call it EOF or "while count < Number of addresses". thats not the part thats bothering me. the purpose of the switch statements is to set up a couple of variables, say xCount YCount and zCount and the case statements will increment 1,2 or all 3 of these depending on the number found. then be able to store these in an array. like so
C++ Syntax (Toggle Plain Text)
  1. switch(addresses[i])
  2. {
  3. default: // don't do anything
  4. break;
  5.  
  6. case '0':
  7. break;
  8.  
  9. case '1': xCount++;
  10. break;
  11.  
  12. case '2': yCount++;
  13. break;
  14.  
  15. case '3': xCount++;
  16. yCount++;
  17. break;
  18.  
  19. case '4': zCount++;
  20. break;
  21.  
  22. case '5': xCount++;
  23. zCount++;
  24. break;
  25.  
  26. case '6': yCount++;
  27. zCount++;
  28. break;
  29.  
  30. case '7': xCount++;
  31. yCount++;
  32. zCount++;
  33. break;
  34. }
  35. xCount = g_pVertices[index].x;
  36. yCount = g_pVertices[index].y;
  37. zCount = g_pVertices[index].z;
  38. index++;
  39. }
  40. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rborob is offline Offline
9 posts
since Mar 2005
Mar 31st, 2005
0

Re: strings as input, substr and len

would something like this work?
C++ Syntax (Toggle Plain Text)
  1. const int MAX_ADDRESSES = g_NumberOfAdds;
  2. StrType word;
  3. ifstream fp;
  4. StrType words[MAX_ADDRESSES];
  5. int numAdds = 0;
  6.  
  7. word.MakeEmpty();
  8. word.GetStringFile(true, ALPHA_NUM, fp);
  9. while(fp && numAdds < MAX_ADDRESSES)
  10. {
  11. word.CopyString(words[numAdds]);
  12. numAdds++;
  13. word.GetStringFile(true, ALPHA_NUM, fp);
  14. }
where fp is my file pointer and g_Number Of Adds is my address count from the file
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rborob is offline Offline
9 posts
since Mar 2005
Apr 1st, 2005
0

Re: strings as input, substr and len

can anyone help please? ive got most of the other stuff working its just this thats the bridge in my program that i cant seem to suss out. can anyone shed some light on this for me please?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rborob is offline Offline
9 posts
since Mar 2005
Dec 8th, 2008
0

Re: strings as input, substr and len

I dont think you explained right. Maybe take more time to explain it
Did you solve this already?
Reputation Points: 11
Solved Threads: 1
Light Poster
namehere05 is offline Offline
25 posts
since Dec 2008
Dec 8th, 2008
0

Re: strings as input, substr and len

> 1st Apr 2005
> 2 Hours Ago
Thick end of 4 years - what do you think?
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Dec 10th, 2008
0

Re: strings as input, substr and len

Jaja You right didnt pay attention.
Reputation Points: 11
Solved Threads: 1
Light Poster
namehere05 is offline Offline
25 posts
since Dec 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: Help With Making A High Scores Table
Next Thread in C++ Forum Timeline: Linking errors: 2 unresolved externals





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


Follow us on Twitter


© 2011 DaniWeb® LLC