Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C++'s equivalent of Java's Integer.parseInt()?

Status
Not open for further replies.

Cobra220

Programmer
Dec 17, 2006
8
US
Sadly, I've been searching a couple hours for a one line way to parse strings into integers. It is such a ridiculously simple task that I'm almost hesitant to post this. Please keep the terminology at a low level as I am extremely new to this language.

Thanks guys!
-David
 
Try a stringstream.
Code:
#include <sstream>
#include <iostream>

using namespace std;

int main()
{
   stringstream str;
   str << "666";   // Put the number into the stringstream.
   cout << "The number as a string: " << str.str() << endl;
   int num;
   str >> num;   // Move the number into an int.
   cout << "The number as an int: " << num << endl;
   str << "3.14";
   float pi;
   str >> pi;
   cout << "How about some pi? " << pi << endl;

   return 0;
}
 
In my program, I can't use stringstream or the >>/<< without getting errors. Do I have to be working in main to use these? Right now, I'm working on a form application. Here's what should happen: When a button is clicked, I read in text from a field, take a substring of it, and then parse that substring to an int, which I use later in the program.
Does this clarification help?
 
Most of the Windows API uses WCHAR based strings, so maybe you need to use wstringstream instead of stringstream?

If you like living dangerously, you could also use a C function like sscanf() (or swscanf() for wide strings).

What compiler are you using and what are the error messages?
 
> In my program, I can't use stringstream or the >>/<< without getting errors.
Post
- your program
- your errors
- your OS/Compiler



--
 
I'm using Visual C++ '05 on Windows XP. Here is the code:
Code:
private: System::Void submitButton_Click(System::Object^  sender, System::EventArgs^  e)
		 {
			 String^a = serialField->Text; //from textfield
			 int c = 0;
			 String^b = a->Substring(1,8);
			 //parse String b into int c...how?
		 }
What I've got here works. I just get errors when I try different ways of parsing. I don't know too much about namespaces or included 'things' so I'll post what I do have:
Code:
	using namespace std;
	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;
They're were all added automatically by Vis. C++. I didn't include any other stuff at the top.

Sorry for the lack of proper terminology and thanks for the help!
-David
 
Aha! No wonder you're having problems. You're using Microsoft's bastardized version of "Managed" C++. I know nothing about that since it's an unholy sub-language of C++, but after looking on the MSDN site I think you want to use one of the Convert functions:
 
Thanks for that link. I've searched that page for some of my other questions/problems but never thought to lookup how to parse under convert.

In case anyone is interested, the following code is what worked for me:
Code:
try{
					 c = Convert::ToInt32(b);
				 }
				 catch(OverflowException^g){
					 Console::WriteLine("Overflow in Double to Int32 conversion");
				 }
				 catch(ArgumentNullException^g){
					 Console::WriteLine("String is null");
				 }
				 catch(FormatException^g){
					 Console::WriteLine("String does not consist of an optional sign followed by a series of digits");
				 }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top