Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 13th, 2006, 9:09 PM   #1
Mcoy
Newbie
 
Mcoy's Avatar
 
Join Date: Mar 2006
Posts: 20
Rep Power: 0 Mcoy is on a distinguished road
About atoi function and const chars...

string temp;
    char temp2[3];
    int num_of_values;
    
    getline (myfile,temp);
    
    for(int i(0); i < 3; i++)
    {
            temp2[i] = temp[i];
    }
    
    num_of_values = atoi(temp2);

The goal of the above code was to read a line from a file (with the getline function) and convert it from string type to integer type with atoi(). When supplying the atoi function with the string, the compiler gave me an error saying that atoi did not accept strings, only const char*s. So I had to use the for loop that you see in my code to change each letter from the string to a char array. -Seems very redundant to me considering a string is simply a character array, or am I mistaken?
What is a better/more efficient/intuitive way to read integers into my program from a file?
Mcoy is offline   Reply With Quote
Old Nov 13th, 2006, 9:53 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Your terminology is too fuzzy. An array of char, terminated by a '\0', is often referred to as a string, and atoi would accept that. The C++ string class is something else, but you can access its array of char with ElStringo.c_str (). Atoi isn't a great choice, since an error will return the same thing as the value, zero, so you can't tell the difference. There are other things like strtol, etc., that work better. You might also wish to investigate string streams.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Nov 14th, 2006, 8:16 AM   #3
Narue
Professional Programmer
 
Narue's Avatar
 
Join Date: Sep 2005
Posts: 419
Rep Power: 4 Narue is on a distinguished road
>since an error will return the same thing as the value, zero, so you can't tell the difference
Not to mention undefined behavior if the value can't be represented by an int. atoi is only about two steps above gets on my eew-o-meter.
__________________
Even if the voices aren't real, they have some pretty good ideas.
Narue is offline   Reply With Quote
Old Nov 14th, 2006, 8:34 AM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
A whole two? ewwwwwwwwwwwww.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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

Similar Threads
Thread Thread Starter Forum Replies Last Post
GOTO Sys. Tray Src. (code Snippet) Cipher Show Off Your Open Source Projects 1 Oct 17th, 2006 2:38 PM
BlitzPlus antros Other Programming Languages 2 Mar 3rd, 2006 1:24 AM
Jackpot game zorin Visual Basic 3 Jun 10th, 2005 2:19 PM
How can i take const char* in function... hanjun C++ 5 May 23rd, 2005 4:49 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:01 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC