Solved

MID Function in C++

Posted on 2003-12-11
11
1,042 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

733 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