C++ Input File Reading Help!

This is a discussion on C++ Input File Reading Help! within the C++ Programming forums, part of the General Programming Boards category; Hello guys, I am new in this forum and i hope im gonna get some fast help. I have this ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    1

    C++ Input File Reading Help!

    Hello guys,

    I am new in this forum and i hope im gonna get some fast help.

    I have this problem where i should input numbers from a .doc file, and the program should output how many times the number of occurences of each number.

    ie: the numbers in the .doc file are 2,3,6,8,9,2,1,1,3,1.

    The program should read the numbers from the file and outputs the following:
    Number 1 = 3
    Number 2 = 1 ... and so on.

    I started with the following but im getting stuck.
    Any help please??

    Code:
    #include<iostream>
    #include<fstream>
    #include<iomanip>
    using namespace std;
    
    int main()
    {
    
    int array[10000];
    int n;
    
    ifstream numbers( "numbers.doc", ios::in); 
    
    if(!numbers)
    {
    cerr << endl << "File could not be opened" << endl;
    system("pause");
    exit(1);
    }
    
    for (int i=0, i<10000, i++)
    array[i] = 0; //initializes array
    
    for (i=0, i<50, i++)
    {
    if (i=1)
    array[n] += array[i];
    cout << array[i];
    }
    {
    if (i=2)
    array[n] += array[i];
    cout << array[i];
    } 
    
    results.close();
    
    return 0;
    }
    ///and so on.

    My program is not working and i neeed someone please to help me fix it ASAP,

    Thanks in advance.

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    >> if (i=1)
    The = operator assigns a value to variables, in this case it sets the value of i to one. What you're wanting here is the equality operator, which is ==. Same case with your if(i=2)

  3. #3
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    891
    Twomers' response is gold.

    And, this is a canned, dry response to any question, but try to indent your code. Perhaps the forum undid whatever formatting you had, or perhaps not.

    Code:
    #include<iostream>
    #include<fstream>
    #include<iomanip>
    using namespace std;
    
    int main()
    {
    
       int array[10000];
       int n;
    
       ifstream numbers( "numbers.doc", ios::in); 
    
       if(!numbers)
       {
          cerr << endl << "File could not be opened" << endl;
          system("pause");
          exit(1);
       }
    
       for (int i=0, i<10000, i++)
          array[i] = 0; //initializes array
    
       for (i=0, i<50, i++)
       {
          if (i==1)
             array[n] += array[i];  //What have you done with n?
          cout << array[i];
       //} //I don't think you meant this
       //{ //or this
          if (i==2)
             array[n] += array[i];
          cout << array[i];
       } 
    
       results.close();
    
       return 0;
    }
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  4. #4
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    891
    Sorry to post again, but I must say you're going about this the wrong way.

    For example: you haven't actually used the input file.

    Code:
    main()
    {
       //ifstream stuff...
       //error checking stuff....
    
       //now. Create some data structure to hold your numbers and read in the data.
       //It'd be nice if the standard library had some container that could associate
       //one number with another. Oh dip.
       //std::map<int,unsigned int> modes;
       //Now, depending on how the file is layed out, you'll have to read stuff in with
       //either >>, getline(), or read()
    
       //Now you have your data. Read up on std::map and you'll see how short and easy-
       //to-read your program will be.
    
       return 0;
    }
    Last edited by CodeMonkey; 01-03-2009 at 09:13 PM. Reason: grammar
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-02-2009, 06:27 AM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21