| | |
Putting two characters together under special circumstances
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Oct 2009
Posts: 12
Reputation:
Solved Threads: 0
Hello Daniweb,
This is my first official "help" thread. I'm currently attempting to read a file that has the following code:
and have it read only the integers, like this:
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...
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?
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)
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)
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)
#include <iostream> #include <fstream> #include <cstdlib> using namespace std; int main() { //defining variables const int MAX = 50; char c[MAX]; char file; //giving file names ifstream original; ofstream updated; //opening input file original.open("lane9.dat"); if(original.fail()) { cout << "Input file was inaccessible."; exit(1); } //opening output file updated.open("MGlane9.txt"); if(updated.fail()) { cout << "Outpur file was inaccessible."; exit(1); } for(int i = 0; i < MAX; i++) { //end of file NOT reached before reading/writing while(!original.eof()) { original >> file; if(isdigit(file)) { c[MAX] = file; cout << c[MAX] << " "; updated << file << " "; } } } //closing files original.close(); updated.close(); //closing program getchar(); return 0; }
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?
-7
#2 26 Days Ago
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.
1
#3 26 Days Ago
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.
•
•
Join Date: Oct 2009
Posts: 12
Reputation:
Solved Threads: 0
0
#4 26 Days Ago
•
•
•
•
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.
•
•
•
•
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.
1
#5 26 Days Ago
C++ Syntax (Toggle Plain Text)
#include <vector> #include <iostream> #include <fstream> #include <sstream> #include <iterator> #include <string> #include <cctype> int main() { std::vector<int> arr; std::string str; std::ifstream file("d:\\test.txt"); if ( !file ) { std::cerr << "Can`t open file!" << std::endl; return 1; } while ( std::getline(file, str, ' ') ) { if ( !isdigit(str[0]) ) continue; std::stringstream ss(str); int tmp = 0; ss >> tmp; arr.push_back(tmp); } std::copy(arr.begin(), arr.end(), std::ostream_iterator<int>(std::cout, " ")); return 0; }
•
•
Join Date: Oct 2009
Posts: 12
Reputation:
Solved Threads: 0
0
#6 26 Days Ago
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"?
Thanks Ancient Dragon, Sky Diploma, and niXman!
Hope to be part of this community for a long time.
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)
#include <iostream> #include <fstream> #include <cstdlib> using namespace std; int main() { //defining variables string str; const int MAX = 50; char c[MAX]; int file; //giving file names ifstream original; ofstream updated; //opening input file original.open("lane9.dat"); if(original.fail()) { cout << "Input file was inaccessible."; exit(1); } //opening output file updated.open("MGlane9.txt"); if(updated.fail()) { cout << "Outpur file was inaccessible."; exit(1); } for(int i = 0; i < MAX; i++) { //end of file NOT reached before reading/writing while(!original.eof()) { original >> str; if(isdigit(str[0])) { cout << str << " "; updated << str << " "; } } } //closing files original.close(); updated.close(); //closing program getchar(); return 0; }
Thanks Ancient Dragon, Sky Diploma, and niXman!
Hope to be part of this community for a long time.
Last edited by godsgift2dagame; 26 Days Ago at 2:11 pm.
0
#7 26 Days Ago
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)
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)
•
•
Join Date: Oct 2009
Posts: 12
Reputation:
Solved Threads: 0
Is the code below any better?
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> #include <cstdlib> using namespace std; int main() { //defining variables string str; int file; //giving file names ifstream original; ofstream updated; //opening input file original.open("lane4.dat"); if(original.fail()) { cout << "Input file was inaccessible."; exit(1); } //opening output file updated.open("MGlane9.txt"); if(updated.fail()) { cout << "Output file was inaccessible."; exit(1); } //end of file NOT reached before reading/writing while(!original.eof()) { original >> str; if(isdigit(str[0])) { updated << str << " "; } } //closing files original.close(); updated.close(); //closing program return 0; }
![]() |
Other Threads in the C++ Forum
- Previous Thread: Borland 5.5
- Next Thread: Arrays - Need help displaying an array.
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






