strings as input, substr and len

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2005
Posts: 9
Reputation: rborob is an unknown quantity at this point 
Solved Threads: 0
rborob rborob is offline Offline
Newbie Poster

strings as input, substr and len

 
0
  #1
Mar 30th, 2005
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
  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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,335
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 236
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: strings as input, substr and len

 
0
  #2
Mar 30th, 2005
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.
  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. */
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,587
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 709
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: strings as input, substr and len

 
0
  #3
Mar 30th, 2005
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 9
Reputation: rborob is an unknown quantity at this point 
Solved Threads: 0
rborob rborob is offline Offline
Newbie Poster

Re: strings as input, substr and len

 
0
  #4
Mar 31st, 2005
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
  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. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 9
Reputation: rborob is an unknown quantity at this point 
Solved Threads: 0
rborob rborob is offline Offline
Newbie Poster

Re: strings as input, substr and len

 
0
  #5
Mar 31st, 2005
would something like this work?
  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
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 9
Reputation: rborob is an unknown quantity at this point 
Solved Threads: 0
rborob rborob is offline Offline
Newbie Poster

Re: strings as input, substr and len

 
0
  #6
Apr 1st, 2005
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?
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 21
Reputation: namehere05 is an unknown quantity at this point 
Solved Threads: 1
namehere05 namehere05 is offline Offline
Newbie Poster

Re: strings as input, substr and len

 
0
  #7
Dec 8th, 2008
I dont think you explained right. Maybe take more time to explain it
Did you solve this already?
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: strings as input, substr and len

 
0
  #8
Dec 8th, 2008
> 1st Apr 2005
> 2 Hours Ago
Thick end of 4 years - what do you think?
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 21
Reputation: namehere05 is an unknown quantity at this point 
Solved Threads: 1
namehere05 namehere05 is offline Offline
Newbie Poster

Re: strings as input, substr and len

 
0
  #9
Dec 10th, 2008
Jaja You right didnt pay attention.
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