Jump to content
Sign in to follow this  
Negat1ve

String from file to int

Recommended Posts

Negat1ve

Well i have little improved my skills in C++ but i need help =\

Is there any way to convert String to int ?

Or just read line from file as int

Share this post


Link to post
Share on other sites
Negat1ve

This is for autoit .. i need to know how to do it in C++ ( MS Visual C++ 2008 )

Share this post


Link to post
Share on other sites
monoceres

It's all about streams.

#include <iostream>
#include <sstream>
#include <fstream>
using namespace std;

int main()
{
    // Converting using stringstream
    stringstream ss("123");
    int num;
    ss>>num;
    cout << num << endl;

    // Getting line as int directly
    fstream file("somefile.txt",ios::in);
    int num2;
    file >> num2;
    cout << num2 << endl;
    

}

Edit: Beautiful, isn't it? ;)

Edited by monoceres

Broken link? PM me and I'll send you the file!

Share this post


Link to post
Share on other sites
Negat1ve

heh never used std functions =D but i will try it and let you know if it works =)

Share this post


Link to post
Share on other sites
Negat1ve

Doesnt work =\

Output :

------ Build started: Project: DropCalcDropImport, Configuration: Release Win32 ------

Compiling...

DropCalcDropImport.cpp

d:\project\dropcalcdropimport\dropcalcdropimport\Form1.h(191) : error C2039: 'ss' : is not a member of 'std'

d:\project\dropcalcdropimport\dropcalcdropimport\Form1.h(192) : error C2065: 'endl' : undeclared identifier

d:\project\dropcalcdropimport\dropcalcdropimport\Form1.h(195) : error C2653: 'ios' : is not a class or namespace name

d:\project\dropcalcdropimport\dropcalcdropimport\Form1.h(195) : error C2065: 'in' : undeclared identifier

d:\project\dropcalcdropimport\dropcalcdropimport\Form1.h(195) : error C2079: 'file' uses undefined class 'std::basic_fstream<_Elem,_Traits>'

with

[

_Elem=char,

_Traits=std::char_traits<char>

]

d:\project\dropcalcdropimport\dropcalcdropimport\Form1.h(195) : error C2078: too many initializers

d:\project\dropcalcdropimport\dropcalcdropimport\Form1.h(197) : error C2039: 'file' : is not a member of 'std'

d:\project\dropcalcdropimport\dropcalcdropimport\Form1.h(197) : warning C4552: '>>' : operator has no effect; expected operator with side-effect

d:\project\dropcalcdropimport\dropcalcdropimport\Form1.h(198) : error C2065: 'endl' : undeclared identifier

Build log was saved at "file://d:\Project\DropCalcDropImport\DropCalcDropImport\Release\BuildLog.htm"

DropCalcDropImport - 8 error(s), 1 warning(s)

========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Share this post


Link to post
Share on other sites
monoceres

You're doing something wrong.

Just the fact that the errors are coming from a .h definitely tells me you're doing something wrong. (generally .h files should contain declarations, not definitions).

Edit: Really!? STL is an incredible powerful and useful addition to C++. It's usually the first thing people learn when they reading about C++.

Edited by monoceres

Broken link? PM me and I'll send you the file!

Share this post


Link to post
Share on other sites
Negat1ve

array<Char>^ test1;

String^ test = "10";

test1 = test->ToCharArray();

int test2 = test1[0];

it WORKS =D i did it my self . no google or something =D

Share this post


Link to post
Share on other sites
monoceres

What. The. Hell. Was. That?


Broken link? PM me and I'll send you the file!

Share this post


Link to post
Share on other sites
Valik

It looks like Managed C++. Yet another person who doesn't know what language they are using, apparently.

Share this post


Link to post
Share on other sites
Negat1ve

I know that it is managed C++ .. and finaly found simple solution for conversion

Single::Parse()

Share this post


Link to post
Share on other sites
Valik

I know that it is managed C++ .. and finaly found simple solution for conversion

It would have been helpful if you had mentioned that earlier. Managed C++ != C++. They are very different. You said and I quote:

his is for autoit .. i need to know how to do it in C++ ( MS Visual C++ 2008 )

That is wrong, you need to know how to do it in Managed C++. A very important detail.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.