| | |
strings as input, substr and len
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2005
Posts: 9
Reputation:
Solved Threads: 0
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
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.
C++ Syntax (Toggle Plain Text)
entil EOF do; string = input; len = length(string); for i=1 to len do ch = substr(string, i-1, 1) switch(ch) //case statements
thanks in advance.
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)
/* file.txt 003124 */ #include <iostream> #include <fstream> #include <string> int main () { std::ifstream file("file.txt"); std::string input; while ( std::getline(file, input) ) { for ( std::string::size_type i = 0, len = input.size(); i < len; ++i ) { switch ( input[i] ) { case '0': std::cout << "zero" << '\n'; break; case '1': std::cout << "one" << '\n'; break; case '2': std::cout << "two" << '\n'; break; case '3': std::cout << "three" << '\n'; break; default: std::cout << "other" << '\n'; break; } } } return 0; } /* my output zero zero three one two other */
"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
•
•
Join Date: Mar 2005
Posts: 9
Reputation:
Solved Threads: 0
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)
switch(addresses[i]) { default: // don't do anything break; case '0': break; case '1': xCount++; break; case '2': yCount++; break; case '3': xCount++; yCount++; break; case '4': zCount++; break; case '5': xCount++; zCount++; break; case '6': yCount++; zCount++; break; case '7': xCount++; yCount++; zCount++; break; } xCount = g_pVertices[index].x; yCount = g_pVertices[index].y; zCount = g_pVertices[index].z; index++; } }
•
•
Join Date: Mar 2005
Posts: 9
Reputation:
Solved Threads: 0
would something like this work?
where fp is my file pointer and g_Number Of Adds is my address count from the file
C++ Syntax (Toggle Plain Text)
const int MAX_ADDRESSES = g_NumberOfAdds; StrType word; ifstream fp; StrType words[MAX_ADDRESSES]; int numAdds = 0; word.MakeEmpty(); word.GetStringFile(true, ALPHA_NUM, fp); while(fp && numAdds < MAX_ADDRESSES) { word.CopyString(words[numAdds]); numAdds++; word.GetStringFile(true, ALPHA_NUM, fp); }
![]() |
Similar Threads
- Starting Python (Python)
- Variable pointers or References for .conf parser? (C++)
- Strings (C)
- Help with programming assignment (Python)
Other Threads in the C++ Forum
- Previous Thread: Help With Making A High Scores Table
- Next Thread: Linking errors: 2 unresolved externals
| Thread Tools | Search this Thread |
api array based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






