Solved

MID Function in C++

Posted on 2003-12-11
11
1,032 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PDF library for Delphi 2 128
VS2015 compilation and missing DLLs 9 160
FMX enumerated colours 2 115
Eclipse IDE - Cannot copy/paste from console output 8 250
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

792 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