problem with atoi function

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
Change numb in line 26 to numb.c_str()
This should solve the problem.
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()
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/
numbers which are getting printed on the screen are totally diferent from the one which are there in the file......
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.
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
my concept of c_str() is stillnnot clear nor of str() is clear......can some one plz tell me wat exactly it is????
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/
yeah but my problem is still not solved......plz help me out..
Try debugging the program. Have a look at the values during runtime using breakpoints.
sorry but i dont know how to do that....plz give me a hint
What compiler are you using?
i am using wxdev c++ on win7
Heres a detailed link about debugging in wxDev C++
http://wxdsgn.sourceforge.net/?q=node/17
Topic archived. No new replies allowed.