• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1078
  • Last Modified:

MID Function in C++

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
tpiazza
Asked:
tpiazza
  • 6
  • 4
1 Solution
 
Sys_ProgCommented:
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
 
tpiazzaAuthor Commented:
do you have some example code on how to transverse thru each character
0
 
bcladdCommented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
tpiazzaAuthor Commented:
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
 
bcladdCommented:
What error? I am guessing that you are using some other string class in your code already and there is a conflict.

-bcl
0
 
tpiazzaAuthor Commented:
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
 
bcladdCommented:
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
 
tpiazzaAuthor Commented:
#include <fstream.h>

is what is causing the problem  but i need that to read the file
0
 
tpiazzaAuthor Commented:
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
 
bcladdCommented:
#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
 
tpiazzaAuthor Commented:
had two variables named the same thing -- thanks for your help
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now