CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Nov 2010
    Posts
    1

    Homework help :(

    Here is the part of the problem which I'm having trouble with...

    Text file (part of it):

    Orville's Acres,
    Hoffman's Hills,
    Jiffy Quick Farm,
    Houston Bayou Texas Home Grown Popcorn Inc.,
    Jolly Good Plantation,
    Organically Grown Inc.,

    part of header file:

    void GetData(fstream& In, string& Name, double& Acres, int& Jars);
    /*Pre: The stream in is open for input.
    Post: Returns the name of less than or equal 29 characters, acres and
    number of jars.
    */

    part of the main function:

    //Open file to read
    InFile.open("e:\\BonusProj.txt",ios::in);
    //continue if Target is a character
    while (!InFile.eof())
    {
    //get data
    GetData(InFile, Name, Acres, Jars);
    //print name
    cout<<setw(29)<<Name;
    //print bar chart
    PrintStars(Jars/Acres);
    cout<<endl;
    }

    I am suppose to create function implementations/definitions corresponding to function prototypes. Such that the Name when being output will display the farm's name as shown in the .txt file WITHOUT the comma OR if the name is longer than 29 characters, print only the first 29 chars and ignore the rest.

    I tried everything but it won't work, how can I get the string to output exactly as the condition given above?

    If you got lost in my explanation and want to see the whole problem, email/IM me on yahoo nguyen0660@sbcglobal.net thanks.

    1 of the thing I tried which microsoft visual returns an error was:

    string temp;

    In>>temp;

    if(temp.length() < 29)
    {
    temp.erase(29);
    name = temp;
    }
    else
    {
    temp.erase(temp.length()-1,temp.length());
    name = temp;
    }

  2. #2
    Join Date
    Apr 2009
    Posts
    598

    Re: Homework help :(

    1. Don't say it is a homework, because it unfair to do a homework for somebody. And, as a matter of fact, you don't want other people to work for you, but you just want a little help.

    2. Don't say it does not work. Say you have an error message, or say you have a wrong result, or say you have an infinite loop, or say the program crashes. Say also when that happens.

    3. It is a very good thing to post your code. But it is not readable if it is not included in code tags.
    Write [code]if(temp.length() < 29)
    {
    temp.erase(29);
    name = temp;
    }[/code] and your code will be beautifully displayed as:
    Code:
    if(temp.length() < 29)
    {
       temp.erase(29);
       name = temp;
    }
    Last edited by olivthill2; November 26th, 2010 at 04:04 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured