Solved

MID Function in C++

Posted on 2003-12-11
11
1,010 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
 

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

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. …
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 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 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.

743 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