Thread: don't know what the error is

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    3

    don't know what the error is

    i want to read a file of txt, and allthings in txt file is number,
    then i convert the string to int, and put it in array wanna to print the array......but don't know why it is not successful, anyone can help me?

    Code:
    //run in dev- C++
    #include <iostream>
    #include <fstream>
    #include <cstdlib>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    	string getcontent;
    	int number;
    	int array[10000];
    	
    	for(int i=0; i<10000; i++)//array intizial
    	{array[i]=0;}
    	
        int i=0;
    	ifstream openfile("data.txt");//read file
    	if (openfile.is_open())
    	{
    		while(!openfile.eof())
    		{
    			openfile >> getcontent;
           		number=atoi(getcontent.c_str());
           		array[i]=number;
           		i++;
    		}
    	}
    
       for(int i=0; i<10000; i++)//for print result
       {
           cout<<array[i]<<"  ";
       }
       system("PAUSE");
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Two possibilities.

    Firstly, the reading may have failed, depending on the layout of your input file. Looping on openfile.eof() is not a good idea - there are plenty of posts here explaining why.

    Second, you will need to flush output before calling system("PAUSE"). cout buffers output and, if the buffer is not flushed, the output is not visible. Either called cout.flush() or perform "cout << endl; " after the loop (std::endl outputs a newline, and flushes the buffer).

    Unrelated to your problem.....

    It's usually not a good idea to output all of your values to one line.

    And system("PAUSE") is not guaranteed to work unless you only work on a windows/DOS box. If you hunt through threads here, you will find better alternatives.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    a newbie :p
    Join Date
    Aug 2008
    Location
    Zurich, Switzerland, Switzerland
    Posts
    91
    I tried your code, I was curios,
    it was working.
    The problem is you are printing 10000 entries so you couldn't see the result.
    For printing result, change the code to be like this so you only print the needed result.

    Code:
    for(int j=0; j<i; j++)//for print result
       {
           cout<<array[j]<<"  ";
       }
    As grumpy said, flushing is also important, but in some cases it is ok even if we don't flush.
    I think it varies depends on the system.
    I am not sure either.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 11-15-2010, 11:14 AM
  2. Replies: 13
    Last Post: 10-08-2010, 05:35 PM
  3. Replies: 3
    Last Post: 10-02-2007, 09:12 PM
  4. Replies: 1
    Last Post: 01-11-2007, 05:22 PM
  5. Compiler error error C2065: '_beginthreadex; : undeclared identifier
    By Roaring_Tiger in forum Windows Programming
    Replies: 3
    Last Post: 04-29-2003, 01:54 AM