Hello,

I have a program with the following variable:

char dateCode[13];

and the this type of data is held by the variable -> 2011006+0000

Now if i want to print only 2011006, and ignore the + and everything after, is there a way to do that in c++? Would i need to read the data in as a different variable type?

Thanks in advance!

First of, can you use std::string?

i dont understand, do you mean to declare it as a string over a char

Yes, can you use std::string instead of char arrays? Is there some restriction that says you can't? If you can use std::string, then it will be easy as this :

std::string str = "2011006+0000";
std::string::size_type pos = str.find_first_of("+");
if(pos == std::string::npos) { cout << "Not found\n"; return -1; }
else{ cout << str.substr(0,pos) << endl; }

oh yes there are no restrictions for this project, more of a just get it to do this type situation

i will try that out, thanks!

ok so i used firstPerson's suggestion on a test case using the same data as in my original project, and I was able to get it to work successfully. however when i apply it to my code, I get compile errors as follow:

dnjlab1.cxx: In function âint main()â:
dnjlab1.cxx:93: error: âstrâ was not declared in this scope
dnjlab1.cxx:93: error: âposâ was not declared in this scope
dnjlab1.cxx:108: error: âstrâ was not declared in this scope
dnjlab1.cxx:108: error: âposâ was not declared in this scope

now does this mean that I am just not using a needed include file??

here is the code:

/***************************************
Devang N. Joshi
Homework One
CSCI 325 - Averaging in C++
January 19th, 2011

The purpose of this program is to serve
as basic review on simple file I/O in
C++ using a well structured data file
and performing simple math on it.
***************************************/
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
using namespace std;

int main()
{
	//declare all variables
	ifstream Infile;
	ofstream Outfile;
	int stationNum = 0; //variable for the station number 
	char dateCode[13];  //variable for the date code
	char dataType[4];   /*variable that shows what "type" the data is
			    example...pwl stands for "primary water level"*/

	double d1=0;
	double d2=0; 
	double d3=0;
	double d4=0;
	double d5=0;
	double d6=0;
	double d7=0;
	double d8=0;
	double d9=0;
	double d10=0;
			    //ten variables for water levels over an hour

	double daySum=0;    //sum of all the water levels over 24hrs
	double dayAvg=0;    //average water level for a given day

	double denominator=240;
			    //240 data points per day, used to cal dayAvg

	int rowCount=0;     //counter used to keep track of day change

	//open and test files for input & output
	Infile.open("lab1.dat",ios::in);
	if(!Infile)
	{
		cerr<<"Error with input file....terminating program";
		exit(1);
	}
	Outfile.open("lab1.out",ios::out);
	if(Outfile.fail())
	{
		cerr<<"Error with output file....terminating program";
		exit(1);
	}

	//initial write to output file...write headings to output file
	Outfile<<"Data"<<endl<<endl;
	Outfile<<"Date"<<"	"<<"Average Water Height"<<endl;
	Outfile<<"-----------------------------"<<endl<<endl;

	/*
	Logic to calculate average daily water levels and
	print them to the output files                   
	*/

	
	while(!Infile.eof()) //test against end of file marker
	{
		if(rowCount<24) //at 24 one day has elapsed
		{
			
			Infile>>stationNum>>dateCode>>dataType>>d1>>d2>>d3>>d4>>d5>>d6>>d7>>d8>>d9>>d10;
			daySum=daySum+d1+d2+d3+d4+d5+d6+d7+d8+d9+d10;
			string str = dateCode;
			string::size_type pos = str.find_first_of("+");
			if(pos == string::npos)
			{
				cerr<<"string concatenation error"<<endl;
			}
			rowCount++;
			

		}
		else //picked up when one days worth of data is read in, can be averaged now
		{
			dayAvg=daySum/denominator; //calculate the average
			Outfile<<str.substr(0,pos)<<"	"<<dayAvg<<endl; //print data to the output file
			daySum=0;    //reset daysum
			dayAvg=0;    //reset dayavg
			rowCount=0;  //reset rowcount
		
		}//if-else
	
	}//while

	/*when the end of file is picked up, the loop terminates
	  however this will cause the final set of averages to fail
	  that is why the final average is calculated below and the
	  the final write to the output file is done */

	dayAvg=daySum/denominator; //calculate the average
	Outfile<<str.substr(0,pos)<<"	"<<dayAvg<<endl; //print data to the output file

	//final write to output file to mark end of data
	Outfile<<endl<<endl;
	Outfile<<"End of data"<<endl;

	//close input & output files
	Infile.close();
	Outfile.close();

	return 0;
}//main

You need to #include <string> but more importantly, you must pull line 80 out of the scope of the if -- since it's declared within the braces, it goes out of scope when that block ends. Move it to line 72 or before.

just tried that still the same errors :/

Yeah, I made an edit, sorry.

oh ic works like a charm thanks so much, i really need to practice the string manipulation more often.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.