![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Mar 2006
Posts: 20
Rep Power: 0
![]() |
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? |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#3 |
|
Professional Programmer
![]() Join Date: Sep 2005
Posts: 419
Rep Power: 4
![]() |
>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. |
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |