Writting data to a file.
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Writting data to a file.

  1. #1
    Join Date
    Dec 2008
    Posts
    1

    Post Writting data to a file.

    I'm having some trouble some parts of a progarm. I have to write a progarm which allows a user to enter inventory data for item in a store. This data should be stored in a text file on disk, with each items data on a single line. The data for each item must be entered on separate lines
    with separate prompts is this order:

    Stock Number (at most 5 characters long)
    Price
    Quantity in stock

    I can assume the user will enter numeric values for price and quantity.

    The progarm should validate the price to require it to be between 0.00 and 10000.00. Repeatedly prompt for the price until it is within that range. However, if the user enters -999.00, escape from the validation loop and abandon that record. In other words, if the user enters an illegal value, ask for it again and agin until the user enters a valid number or enters -999.00, then abandon that record and start prompting for a new record by asking for its stock number.

    I dont need to validate the other data for this progarm.

    Store the data on a disk with one record on each line of the file with the fields in the order shown above.

    This program should prompt the user for a name of the flie to be created. If the user just presses <enter>, then the progarm should use the default name of "stock.dat" If the user does not include an extention on the file name, the progarm should append ".dat" to the user's file name.


    This is what I have so far:
    Most my problems are in the validation. I cant seem to validate the data correctly. If anyone can help at all, I thank you for it.


    //p3b

    #include <fstream.h>
    #include <iostream.h>
    #include <string.h>
    #include <stdio.h>
    #include <ctype.h>


    void getPrice(float &price);
    void getQuantity(int &quantity);
    void getStockNum(char stockNum[]);
    void getFileName(char FileName[]);

    void main()
    {
    char stockNum[6];
    float price ;
    int quantity ;
    char FileName[20];


    getStockNum(stockNum);

    getPrice(price);


    //===============================
    // loop for price being -.999
    //===============================

    getQuantity(quantity);

    getFileName(FileName);

    strcat(FileName,".dat");
    ofstream project3b(FileName, ios:ut);
    project3b << "Stock Number: "<<stockNum << "\n" << "Price: "<<price << "\n" << "Quantity: "<<quantity;
    cout << "\n\n";


    }


    //========================
    //get stockNum
    //========================


    void getStockNum(char stockNum[])
    {
    cout << "Stock Number: ";
    cin >> stockNum;

    if(strlen(stockNum, '\0'))
    exit(1);

    }


    //==========================
    //get the price
    //==========================


    void getPrice(float &price)
    {
    cout << "Price: ";
    cin >> price;

    if (price > 10000.00 && price <0.00 )
    {
    cout<<"Enter a valid Price: ";
    getPrice(price);
    }
    if (price == -.999)
    {
    cout<<"exiting progarm...";
    exit(1);
    }
    }


    //===========================
    //get the quantity
    //===========================


    void getQuantity(int &quantity)
    {
    cout << "Quantity: ";
    cin >> quantity;
    }

    //===========================
    //get file name
    //===========================


    void getFileName(char FileName[])
    {
    cout << "\nEnter the name of the file you want to create with extention: ";
    cin >> FileName;
    }
    Last edited by bmcginnis; December 7th, 2008 at 11:57 AM.

  2. #2
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: Writting data to a file.

    Please edit your post and put CODE tags around your code. Read the FAQ on that.
    Quote Originally Posted by bmcginnis View Post
    #include <fstream.h>
    #include <iostream.h>
    #include <string.h>
    #include <stdio.h>
    #include <fstream.h>
    #include <ctype.h>
    Most of these headers do not exist in standard C++ (according to the 10 year old standard). Considering the exercise, it is also unclear why you include things like stdio.h or ctype.h. Try starting anew with
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    Quote Originally Posted by bmcginnis View Post
    void main()
    The return type of main is int, has always been int and there is no plan to change that.
    Quote Originally Posted by bmcginnis View Post
    char FileName[20];
    Why not declare filename as std::string? Then appending ".dat" would simply be a += operation.

    No more comments before you make your code readable.
    More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf

    Premature optimization is the root of all evil --Donald E. Knuth


    Please read Information on posting before posting, especially the info on using [code] tags.

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,097

    Re: Writting data to a file.

    if (price > 10000.00 && price <0.00 )

    Think about any possible value where that could evaluate true. A number could never be both greater than 10,000 and less than 0 at the same time. It could be greater than 10,000 or less than 0 though.

Tags for this Thread

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center