Hello Daniweb,

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

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:

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

#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?

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.

Comments
Hey AC, thanks a lot for your help!

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.

Comments
Hey SD, I didn't get to use your idea, but it looks very "snazzy". :)

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.

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?

#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;
}
Comments
Hey niXman, thanks a bunch for the idea! Worked like a charm.

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"?

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

Edited 7 Years Ago by godsgift2dagame: n/a

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)

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.

Can someone just look over the code and tell me if I did the code "efficiently"?

Too many unnecessary actions.

Is the code below any better?

#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;
}
This question has already been answered. Start a new discussion instead.