Please help me.

This is a discussion on Please help me. within the C++ Programming forums, part of the General Programming Boards category; This is the assignment that I need help with Assignment 5 and here is the file that needs to go ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    9

    Please help me.

    This is the assignment that I need help with
    Assignment 5

    and here is the file that needs to go into the program
    .txt file

    here is what i have so far.... any suggestions and hints would be greatly appreciated.
    Code:
    #include <iostream>
    
    using namespace std;
    
    struct sales
    {
      int customerid;
      string firstname;
      string lastname;
      int items;
      float amount;
    };
    
    int main()
    
    {
    int i;							//int i
    int sales[1000]; 
    for(i=0; i<1000; i++);
    cin>>sales[i];
     
    return 0;

  2. #2
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    If you're talking about inputting and outputting with text files, they have a tutorial on it at this site.

    Are you a part of the class that assignment is from? If not, I may be able to 'whip up' a functioning program (it does what it's supposed to do) for you to look at and learn from. I'm a little rusty myself and I need the practice. Should have it sometime tommorow, unless you don't want me to.
    Last edited by homeyg; 03-25-2005 at 09:03 PM.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    9
    No, please whip one up. I need something to go by, it would be a great help and thank you.

  4. #4
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    I won't be able to have one by the end of tonight though.

  5. #5
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,803
    Well first off you should pay attention in class. Well I shouldn't say this because there are crappy teachers but I am just assuming.
    Read This http://www.cprogramming.com/tutorial/lesson10.html for File I/O. It should show you all you need to know.
    And homeyg showing examples on how to do stuff is ok, but doing a "Full" example is not good. Let people do their own homework.
    Woop?

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    I'm a little rusty myself and I need the practice. Should have it sometime tommorow, unless you don't want me to.
    Then, feel free to write the program for your own advancement, but don't post solutions to people's homework. And, guess what? Just because someone says it's not homework doesn't mean that is in fact the case. Cheaters have no qualms about lying in order to get you to provide the answers they want.
    Last edited by 7stud; 03-25-2005 at 11:44 PM.

  7. #7
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    here, this program reads in the first column and prints it to the screen. it should be enough for you to go off of for the rest of the assignment.
    Code:
    #include<iostream>
    #include<fstream>   //the C++ library used for file I/O
    
    int main()
    {
        unsigned int no;
        
        std::fstream file("Untitled1.dat",std::ios::in); //open a file stream for reading
        while(file) //while the stream has no terminal flags set
        {
            file>>no;                   //take in unsigned int no from the stream
            file.ignore(32000,'\n');    //ignore the rest of the line in the stream
            std::cout<<no<<std::endl;   //output unsigned int no to the console
        }
        file.close();   //use this to explicitly close the file
        
        std::cin.get();
        return 0;
    }
    as you can see, a file stream is (in your case) used the same way as the standard input stream (cin). all the different uses of cin can be used with the file stream, for example, cin.get(), cin.getline(), cin.ignore(), etc. can all be used with a file stream.
    Last edited by major_small; 03-26-2005 at 01:42 AM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    > for(i=0; i<1000; i++);
    Well the ; at the end is bad news for you.

    First step is to read all the data in, then print it all out again unmodified.
    That at least proves you've read it in correctly.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    Feb 2005
    Posts
    9
    This is what I did last night before anyone posted bu i am getting an error, I went by our notes and the textbook. Can you help me from this part on?
    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    struct sales
    {
      int customerid;
      string firstname;
      string lastname;
      int items;
      float amount;
    };
    
    int main()
    {
     int count=0;
     
     ifstream infile (sales.txt);
    
      sales sales [1000];
      for (count=0; count<1000; count++)
      	{
          infile >> sales[count].customerid
          		 >> sales[count].firstname
                 >> sales[count].lastname
                 >> sales[count].items
                 >> sales[count].amount 
        }
        
    for (count=0; count<1000; count++)
    {
      cout << sales[count].firstname <<" "
      	   << sales[count].lastname << " ";
    }
    
    return 0;

  10. #10
    Registered User
    Join Date
    Dec 2004
    Posts
    32
    The first thing you are missing is,
    Code:
    #include <string>
    Then you will also need to put "sales.txt" between double quotes,
    Code:
    ifstream infile ("sales.txt");
    You also are missing a semicolon here,
    Code:
                >> sales[count].items
                >> sales[count].amount;
        }
    You are also missing the closing curly brace after return 0,
    Code:
    			<< sales[count].lastname << " ";
    	}
    
    	return 0;
    }
    You should also try to include the error messages you are getting from the compiler when you post.

  11. #11
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    Quote Originally Posted by 7stud
    Then, feel free to write the program for your own advancement, but don't post solutions to people's homework. And, guess what? Just because someone says it's not homework doesn't mean that is in fact the case. Cheaters have no qualms about lying in order to get you to provide the answers they want.
    Well, I don't think it is his class because the assignment was dated from a year ago.

  12. #12
    Registered User
    Join Date
    Feb 2005
    Posts
    9
    Quote Originally Posted by Anubis
    The first thing you are missing is,
    Code:
    #include <string>
    Then you will also need to put "sales.txt" between double quotes,
    Code:
    ifstream infile ("sales.txt");
    You also are missing a semicolon here,
    Code:
                >> sales[count].items
                >> sales[count].amount;
        }
    You are also missing the closing curly brace after return 0,
    Code:
    			<< sales[count].lastname << " ";
    	}
    
    	return 0;
    }
    You should also try to include the error messages you are getting from the compiler when you post.
    Thank you very much. I wish I could repay you for your help.

    Here is my code so far, I have gotten no errors but how do I read information into the program?
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    struct sales
    {
      int customerid;
      string firstname;
      string lastname;
      int items;
      float amount;
    };
    
    int main()
    {
     int count=0;
     
     ifstream infile ("sales.txt");
    
      sales sales [1000];
      for (count=0; count<1000; count++)
      	{
          infile >> sales[count].customerid
          		 >> sales[count].firstname
                 >> sales[count].lastname
                 >> sales[count].items
                 >> sales[count].amount; 
        }
        
    for (count=0; count<1000; count++)
    {
      cout << sales[count].firstname <<" "
      	   << sales[count].lastname << " ";
    }
    
    return 0;

  13. #13
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Well, I don't think it is his class because the assignment was dated from a year ago.
    Did you consider the professor might not go to his web page to udate the dates every semester?
    I went by our notes and the textbook.
    What do you think now?
    Last edited by 7stud; 03-26-2005 at 09:15 PM.

  14. #14
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    sales sales [1000];
    I suggest you not name your variable the same name as the type. Would you do this:

    int int=10;

    A struct creates a type, like an int type. To declare a variable of your struct type, you would do this:

    sales x;


    Here is my code so far, I have gotten no errors but how do I read information into the program?
    In your opinion, what does this do:
    Code:
    ifstream infile ("sales.txt");
    
      sales sales [1000];
      for (count=0; count<1000; count++)
      	{
          infile >> sales[count].customerid
          		 >> sales[count].firstname
                 >> sales[count].lastname
                 >> sales[count].items
                 >> sales[count].amount; 
        }
    Last edited by 7stud; 03-26-2005 at 09:24 PM.

  15. #15
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    Quote Originally Posted by 7stud
    Did you consider the professor might not go to his web page to udate the dates every semester?
    What do you think now?
    Okay, my bad.

Popular pages Recent additions subscribe to a feed

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