Solved

MID Function in C++

Posted on 2003-12-11
11
1,021 Views
Last Modified: 2012-08-13
New to C++ Coming from VB

Is there a mid function in C++, If not how do you parse a string?

12012003  -- need to set month to 12 day to 01 etc etc
0
Comment
Question by:tpiazza
  • 6
  • 4
11 Comments
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 9920344
You will have to traverse thru each charater [Assuming that u are having a char array i.e. char *]

OR You can can strstr (have a look at help for this if it is usefu to u )

Amit
0
 

Author Comment

by:tpiazza
ID: 9920424
do you have some example code on how to transverse thru each character
0
 
LVL 11

Expert Comment

by:bcladd
ID: 9920436
Depends on what type of string you are using:

std::string supports a substr function:

#include <iostream>
#include <string>
using std::string;
using std::cout;
using std::endl;

int main()
{
  string date = "20031209";

  string year = date.substr(0,4);
  string month = date.substr(4, 2);
  string day = date.substr(6, 2);

  cout << month << "/" << day << "/" << year << endl;

  return 0;
}

Hope this helps, -bcl
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:tpiazza
ID: 9920511
that looks like what i want however mine errors out when i put hte "using" lines in

i basically have an element in an array that is returned

date[9]  the value of this is "20031209" -- that element is what i trying to parse

0
 
LVL 11

Expert Comment

by:bcladd
ID: 9920540
What error? I am guessing that you are using some other string class in your code already and there is a conflict.

-bcl
0
 

Author Comment

by:tpiazza
ID: 9920582
these are the includes

#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
#include <string.h>

and these are the errors

C:\Program Files\Microsoft Visual Studio\MyProjects\parse\parse.cpp(6) : error C2653: 'std' : is not a class or namespace name
C:\Program Files\Microsoft Visual Studio\MyProjects\parse\parse.cpp(6) : error C2873: 'string' : symbol cannot be used in a using-declaration
C:\Program Files\Microsoft Visual Studio\MyProjects\parse\parse.cpp(7) : error C2653: 'std' : is not a class or namespace name
C:\Program Files\Microsoft Visual Studio\MyProjects\parse\parse.cpp(7) : error C2873: 'cout' : symbol cannot be used in a using-declaration
C:\Program Files\Microsoft Visual Studio\MyProjects\parse\parse.cpp(8) : error C2653: 'std' : is not a class or namespace name
C:\Program Files\Microsoft Visual Studio\MyProjects\parse\parse.cpp(8) : error C2873: 'endl' : symbol cannot be used in a using-declaration

0
 
LVL 11

Accepted Solution

by:
bcladd earned 50 total points
ID: 9920632
Get rid of the .h at the end of iostream, fstream, and string. change the name of stdlib to cstdlib.

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>

using namespace std;

The "using namespace std;" moves all of the functions in that namespace into the global namespace. The .h versions of the files are old. The standard now specifies the ones without the .h and that wraps the funcitons in the std namespace.

-bcl
0
 

Author Comment

by:tpiazza
ID: 9920677
#include <fstream.h>

is what is causing the problem  but i need that to read the file
0
 

Author Comment

by:tpiazza
ID: 9920715
this is what i have and it errors all kinds of stuff -- the fstream is the one causing the problems

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>

using std::string;
using std::cout;
using std::endl;


0
 
LVL 11

Expert Comment

by:bcladd
ID: 9920897
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
  ifstream test;
  ofstream someothertest;
 
  string date = "20031209";

  string year = date.substr(0,4);
  string month = date.substr(4, 2);
  string day = date.substr(6, 2);

  cout << month << "/" << day << "/" << year << endl;

  return 0;
}

Notice I changed the using lines to one using line. That line should include all the stuff from fstream into your global namespace.

-bcl
0
 

Author Comment

by:tpiazza
ID: 9920916
had two variables named the same thing -- thanks for your help
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now