problem with atoi function

Jul 23, 2011 at 2:23pm
i have numbers stored in my text file and i need to sort them..each number is been stored i a different line...here's my code


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include<iostream>
#include<fstream>
#include<sstream>
#include<conio.h>
#include<string>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
void sortdata()
{
    stringstream buffer;
    string numb;
    int i,flag,arr[100],counter=0;
    ifstream fin;
    for(i=1;i<=1;i++)
    {
    buffer<<i<<".txt";
    fin.open(buffer.str().c_str(),ios::in);
    while(!fin.eof())
    {
        for(flag=0;flag<=100;flag++)
        {
        while(getline(fin,numb))
        {
            
            arr[flag]=atoi(numb);
            counter++;
        }
        }
    }
        
    fin.close();}
    for(i=0;i<counter;i++)
    cout<<arr[i]<<endl;    
    }
        
     int main()
     {
            sortdata();
            getch();
        }   
        
        
        


my error is "cannot convert `std::string' to `const char*' for argument `1' to `int atoi(const char*)' "......plz help me out
Last edited on Jul 24, 2011 at 6:54am
Jul 23, 2011 at 2:59pm
Change numb in line 26 to numb.c_str()
This should solve the problem.
Jul 23, 2011 at 4:34pm
yeah that removed the error but it is giving me unknown unexpected results.....moreover i am failing to understand the reason of your correction....means why should it be numb.c_str()
Jul 23, 2011 at 6:54pm
What are the unexpected results?

.c_str() is a function in the string class.
It changes the string to a C style string (an array to a pointer to string) that was declared as:
char *numb[]

Here is a link for further details:
http://www.cplusplus.com/reference/string/string/c_str/http://www.cplusplus.com/reference/string/string/c_str/
Jul 24, 2011 at 3:11am
numbers which are getting printed on the screen are totally diferent from the one which are there in the file......
Jul 24, 2011 at 6:30am
You should make the following changes in the code you posted above:
1. You can remove .c_str() from line 18.
2. Why have you made the for loop on line 15 with i = 1 & i <=1?
3. Instead of checking for End of File (on line 19), it will be better to use fin.good().
4. Why have you declared flag & i twice? Once on line 13 and again in the for loop in line 21 & line 15?
5. You need not have the while loop on line 23. I guess it would be better to use a if statement.
Jul 24, 2011 at 6:41am
You can remove .c_str() from line 18

it is giving me an error
Why have you made the for loop on line 15 with i = 1 & i <=1?

i have to make the program for multiple files...so right now i am just testing it for a single file...
Why have you declared flag & i twice? Once on line 13 and again in the for loop in line 21 & line 15?

i dont think i have declared it twice...i first declared it and then i am initializing it.....
You need not have the while loop on line 23. I guess it would be better to use a if statement

each number is on a seperate line...so ineed to read the complete number....


my problem is still not solved.....plz help me out
Jul 24, 2011 at 6:42am
my concept of c_str() is stillnnot clear nor of str() is clear......can some one plz tell me wat exactly it is????
Jul 24, 2011 at 6:48am
each number is on a seperate line...so ineed to read the complete number....

That is done by the for loop. By adding the while loop, you are increasing the number of iterations of the loop 100 times.
Say there are 10 lines. in the file.
If you run the program, it will repeat the process many 1000 times.

About using .c_tr():
atoi function asks for a C string, and then turns it into a number.
.c_str makes a C++ style string into a C style string.
About atoi:
http://www.cplusplus.com/reference/clibrary/cstdlib/atoi/
Jul 24, 2011 at 6:51am
yeah but my problem is still not solved......plz help me out..
Jul 24, 2011 at 6:52am
Try debugging the program. Have a look at the values during runtime using breakpoints.
Jul 24, 2011 at 6:53am
sorry but i dont know how to do that....plz give me a hint
Jul 24, 2011 at 6:54am
What compiler are you using?
Jul 24, 2011 at 6:55am
i am using wxdev c++ on win7
Jul 24, 2011 at 6:57am
Heres a detailed link about debugging in wxDev C++
http://wxdsgn.sourceforge.net/?q=node/17
Topic archived. No new replies allowed.