Guru3D.com Forums

Go Back   Guru3D.com Forums > General > Programming/Html
Programming/Html C++,Visual Basic,HTML,Flash,OpenGL,Modelling,Java.


Reply
 
Thread Tools Display Modes
C++ how do i put a sting into a integer
Old
  (#1)
darknight909
Ancient Guru
 
darknight909's Avatar
 
Videocard: EVGA GX 260 Core 216
Processor: Intel Core 2 Duo 3ghz
Mainboard: ASUS P5N-D 750i
Memory: 2 x 2GB DDR2 800
Soundcard: Creative X-Fi
PSU: Tough Power 700w
Default C++ how do i put a sting into a integer - 11-10-2006, 06:31 | posts: 4,845 | Location: San Diego, CA

i have this string that has a number value and i want to put it into an integer so how would i go about this?
   
Reply With Quote
 
Old
  (#2)
Vinnie
Ancient Guru
 
Vinnie's Avatar
 
Videocard: 2x AMD HD6970 2GB
Processor: Intel Core i7 2600K
Mainboard: Gigabyte P67A-UD4-B3
Memory: 8GB Corsair DDR3-1600
Soundcard: X-fi Titanium Fatal1ty
PSU: Enermax Revolution 1020W
Default 11-10-2006, 14:18 | posts: 7,290 | Location: The Netherlands

Maybe with a static class ?

Code:
static_cast<int>(variablename)
   
Reply With Quote
Old
  (#3)
Sieras
Ancient Guru
 
Sieras's Avatar
 
Videocard: Gigabyte 450 GTS OC2
Processor: Intel Core i7 2600k
Mainboard: Gigabyte P67X-UD3-B3
Memory: Corsair DDR3-1600 8 GB
Soundcard: Xonar D2X+Z-5500+SH HD448
PSU: Corsair TX650W
Default 11-10-2006, 15:45 | posts: 3,857 | Location: Lithuania, Kaunas

Huh? There's and easier way though.. or just a simpler to understand:

Code:
integer = StrToInt(String);
for example:

Code:
int number;
String str;

.......

number = IntToStr(str);

.......
The same thing applies to double variables, but you have to use StrToFloat then
   
Reply With Quote
Old
  (#4)
darknight909
Ancient Guru
 
darknight909's Avatar
 
Videocard: EVGA GX 260 Core 216
Processor: Intel Core 2 Duo 3ghz
Mainboard: ASUS P5N-D 750i
Memory: 2 x 2GB DDR2 800
Soundcard: Creative X-Fi
PSU: Tough Power 700w
Default 11-14-2006, 05:44 | posts: 4,845 | Location: San Diego, CA

error C3861: 'inttostr': identifier not found, even with argument-dependent lookup

ifstream myfile ("example.txt");
myfile.open ("example.txt");
getline (myfile,string);
int = inttostr(string);
myfile.close();

Last edited by darknight909; 11-14-2006 at 05:47.
   
Reply With Quote
 
Old
  (#5)
Sieras
Ancient Guru
 
Sieras's Avatar
 
Videocard: Gigabyte 450 GTS OC2
Processor: Intel Core i7 2600k
Mainboard: Gigabyte P67X-UD3-B3
Memory: Corsair DDR3-1600 8 GB
Soundcard: Xonar D2X+Z-5500+SH HD448
PSU: Corsair TX650W
Default 11-14-2006, 19:36 | posts: 3,857 | Location: Lithuania, Kaunas

You have to write IntToStr() with all the capital letters
And as I see from your '' error C3861: 'inttostr': '' , you just wrote inttostr
C++ doesn't have any formatting which would correct this on it's own.
   
Reply With Quote
Old
  (#6)
darknight909
Ancient Guru
 
darknight909's Avatar
 
Videocard: EVGA GX 260 Core 216
Processor: Intel Core 2 Duo 3ghz
Mainboard: ASUS P5N-D 750i
Memory: 2 x 2GB DDR2 800
Soundcard: Creative X-Fi
PSU: Tough Power 700w
Default 11-15-2006, 05:31 | posts: 4,845 | Location: San Diego, CA

i fixed it to capital letters and I still get the same error

is there a header im missing?

or is there a way to use getline to grab an integer or a similar command

Last edited by darknight909; 11-15-2006 at 05:37.
   
Reply With Quote
Old
  (#7)
Sieras
Ancient Guru
 
Sieras's Avatar
 
Videocard: Gigabyte 450 GTS OC2
Processor: Intel Core i7 2600k
Mainboard: Gigabyte P67X-UD3-B3
Memory: Corsair DDR3-1600 8 GB
Soundcard: Xonar D2X+Z-5500+SH HD448
PSU: Corsair TX650W
Default 11-15-2006, 15:58 | posts: 3,857 | Location: Lithuania, Kaunas

Hm, strange.. I've always used that and it worked without any errors.
Oh well, try this then:

Code:
string s = "50";
int i = Int32.Parse(s);
or

Code:
string s = "50";
int i = Convert.ToInt32(s);
This should work.

Last edited by Sieras; 11-15-2006 at 17:12.
   
Reply With Quote
Old
  (#8)
thecake90
Ancient Guru
 
thecake90's Avatar
 
Videocard: ATi HD3850 1GB/Nvidia 230
Processor: Intel C2D E6400/i7 720QM
Mainboard: Gigabyte GA-965G-DS3/PM55
Memory: 2Gb DDR2 533/4Gb DDR3
Soundcard: Integrated Sound Card
PSU: 450W/ 19V 6.5A
Default 11-15-2006, 16:29 | posts: 4,065 | Location: Earth

&lt;
&gt:
   
Reply With Quote
Old
  (#9)
darknight909
Ancient Guru
 
darknight909's Avatar
 
Videocard: EVGA GX 260 Core 216
Processor: Intel Core 2 Duo 3ghz
Mainboard: ASUS P5N-D 750i
Memory: 2 x 2GB DDR2 800
Soundcard: Creative X-Fi
PSU: Tough Power 700w
Default 11-16-2006, 19:41 | posts: 4,845 | Location: San Diego, CA

error C2228: left of '.Parse' must have class/struct/union type
type is ''unknown-type''

am I missing a header?
   
Reply With Quote
Old
  (#10)
Cornelius
Master Guru
 
Cornelius's Avatar
 
Videocard: Geforce 560Ti
Processor: Core i7 2600K @ stock
Mainboard:
Memory: 16 GB DDR3
Soundcard: Logitech G930 Wireless
PSU: 550W PSU
Default 11-17-2006, 16:03 | posts: 624 | Location: Orlando, FL

The C standard library already has a function built in:

Code:
#include <stdlib.h>
...
string s = "255";
int n = atoi( s.c_str() );
http://www.cppreference.com/stdstring/atoi.html
   
Reply With Quote
 
Old
  (#11)
darknight909
Ancient Guru
 
darknight909's Avatar
 
Videocard: EVGA GX 260 Core 216
Processor: Intel Core 2 Duo 3ghz
Mainboard: ASUS P5N-D 750i
Memory: 2 x 2GB DDR2 800
Soundcard: Creative X-Fi
PSU: Tough Power 700w
Default 11-23-2006, 08:58 | posts: 4,845 | Location: San Diego, CA

hmm well that seems to work but for some reason now
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <stdlib.h>
#include <stdio.h>

using namespace std;

int x;
int y;
string a;

int main() {
x = 2;
a = x;
cin >> y;
if (y == 1) {
ofstream myfile;
myfile.open ("example.txt");
myfile << a;
myfile.close();
return 0;
}else if(y == 2) {
ifstream myfile ("example.txt");
myfile.open ("example.txt");
getline (myfile,a);
x = atoi(a.c_str());
myfile.close();
cout << x;

return 0;
}

x doesnt output anything

Last edited by darknight909; 11-23-2006 at 09:01.
   
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
vBulletin Skin developed by: vBStyles.com
Copyright (c) 1995-2012, All Rights Reserved. The Guru of 3D, the Hardware Guru, and 3D Guru are trademarks owned by Hilbert Hagedoorn.