Thread: Help with this program!

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    2

    Help with this program!

    I have a program that requires me to read from a file looking like this:
    Last First 78 70 76 72 82 84
    Last First 72 76 77 90 76 45
    Last First 64 93 95 83 66 56
    Last First 67 78 68 75 64 46
    Last First 67 75 97 90 57 89

    the program is to read line by line and print out the (last, First, and store the six numbers in an array for each line. How do I put the numbers in an array when reading from the file?
    I would really appreciate it if someone can help me. Thanks

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Use a for loop. Then post your attempt.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Try this code with your file:
    Code:
    #include <iostream>
    #include <fstream.h>
    #include <cstring>
    
    using namespace std;
    
    int main()
    {
        ifstream afile("file.txt");  //name of file holding the data you posted
        string alldata[40];
        for(int i=0;i<40;i++)
        afile>>alldata[i];
        
        afile.close();
        
        for(int b=0;b<40;b++)
        cout<<alldata[b]<<endl;
        
        system("PAUSE");
        return 0;
    }
    See, the above reads all the pieces(strings) into an array, then prints it out. Is that what you're looking for or no?
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    2
    the numbers stand for grades and I need to manipulate them later in the program. The program is to print a students last name, First name, and then use the grades for some comparison and letter assignment. I can get the last name and the first name, but my problem is that I don't know how to store the grades into an array. I just learning C++

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    jmd15,

    It's not appropriate for you to post answers to someone's homework. In addition, there are enough syntax errors, bad programming practices, and poor design to make it something no one should try and emulate.

    vuudu,

    If arrays are giving you trouble, forget about reading from a file for the moment--that just complicates things. An array is just a series of variables. If I want to store 100 numbers for use in my program, why should I have to think up 100 different names for those variables and declare them with 100 lines of code, e.g.
    i
    Code:
    nt myfirstNum = 0;
    int mysecondNum = 0;
    int mythirdNum = 0;
    ...
    ...
    int myonehundredthNum = 0;
    All programming languages allow you to declare an array instead:
    Code:
    int myNums[100];
    That means there are 100 variables with the names:

    myNums[0], myNums[1], ...myNums[99]

    Note how the variable names run from 0 - 99 instead of 1-100. Each of those variables is called an element of the array.

    I was able to declare those 100 variables in one line. I can also set everyone of them equal to 0 in the same line when I declare the array:
    Code:
    int myNums[100] = {0};
    Since you should always initialize your variables, that is good practice. Now, every one of those 100 variables is equal to 0.

    Try these exercises:

    Exercise 1:
    1) Create an array of size 5.
    2) Assign each element of the array the numbers 1-5 using five assignment statements.

    Exercise 2:
    The same as 1) except use a for-loop to assign the numbers 1-5 to the array using only one assignment statement.

    Exercise 3:
    The same as 2) except use a while loop instead.
    Last edited by 7stud; 11-19-2005 at 07:52 PM.

  6. #6
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    It wasn't doing his homework, THAT WAS AN EXAMPLE OF READING THE DATA INTO AN ARRAY! What are my "syntax errors", "poor design" and all this bs you're talking about, point it out to me. HOW MUCH DESIGN DOES IT TAKE FOR AN EXAMPLE!? IT WAS AN EXAMPLE!! E-X-A-M-P-L-E. NOT HIS HOMEWORK. Man, I can't take arrogance.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  7. #7
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    Code:
    #include <fstream.h>
    should be:-

    Code:
    #include <fstream>
    When you compile and execute it with this you should notice that your compiler returns no errors.

    ...and why string alldata[40]?

    What happens if there are more than 40 pieces of data in your file? Ok so the OP has shown his file containing exactly 40 pieces of data. But maybe you should have shown him how to handle a file containing any amount of data?

    He also said he wanted to read the file in one line at a time.
    So why not do something like...

    Code:
    while(getline(afile, alldata, '\n'))
    {
      //do stuff 
    }

    Perhaps?

    Oh and system("pause") should be swapped with a few
    cin.gets. System isn't portable just to name but one of its many pitfalls.

    I understand you are probably just an enthusiastic programmer. But making suggestions after the person has had a chance to write something is probably better in the long run.

    Last edited by treenef; 11-20-2005 at 11:25 AM.

  8. #8
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Of course I would read a file like you have shown treenef, but I thought he wanted each string in the line in the array. Didn't he want each piece in an array, or does he just want to read the whole thing? Well either way, I thought he was going to use a SET file like he posted. If he did want to read a file that was bigger than that, you would need to use something bigger than a 40 slot array. Yeah I guess I just got mad in my last post when someone comes in, tells me that I don't know what I'm doing, and then says that only he knows how to do it right. Well I'm past that. Oh, I would normally use a cin.get() in place of a system call but for some reason on the copy of Dev-C++ installed on my WinXP box doesn't pause at the call of cin.get(). It does on my Win98 laptop but not on the XP one.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  9. #9
    !anExpert
    Join Date
    Mar 2005
    Location
    pa
    Posts
    155
    Code:
    for(int i=0; i<100; ++i)
        cout << "lol!" << endl;
    kick his ass seebass

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM