Reading numbers with FOR loop solved: New problem emerges

Discussion in 'C++' started by GeekBoy, Apr 11, 2007.

  1. GeekBoy

    GeekBoy Guest

    Thanks for the help obnoxious.
    Reading in the rest of the line with "getline" worked great and now
    the averages work.

    Now for the new problem. Program on first run works flawlessly.
    Next runs enter in some data I don;t know what instead of the data in
    the file. Tried closing and opening file again, but it's not working.

    #include<iomanip>
    #include<fstream>
    #include<iostream>
    #include<string>


    using namespace std;

    int main()
    {

    ifstream inFile;
    int months = 1;
    char quest;
    int x, y, z;
    float avg = 0, data;
    string dummy_string_buffer;

    inFile.open("input4.txt");
    if (!inFile)
    {
    cerr << "Error Opening File" << endl;
    system ("pause");
    return 1;
    }


    while (months >= 1 && months <= 12)
    {



    cout << "Please enter the number of months/year to be averaged: ";
    cin >> months;

    cout << "Year/" << endl;
    cout << " /Month";

    for (x = 1; x <= months; x++) //prints out number of months header

    {
    cout << setw(4) << x;
    } // end of for loop #1

    cout << " Avg\n" << endl ;

    for (y = 1; y <= 10; y++) // loop for 10 years of rain fall data
    {
    cout << setw(2)<< y << " ";

    for (z = 1; z <= months; z++) // loop for (x)months of rain fall
    {
    inFile >> data;
    cout << setw(4) << data << setw(4);
    avg = data + avg;
    } // end of for loop #3

    getline(inFile, dummy_string_buffer); // Gets a line
    from the file and stores it in dummy_string_buffer
    // cout << dummy_string_buffer;
    cout <<": " << setw(2) << static_cast<int>(avg/
    static_cast<float>(months));
    cout << " inches\n";
    avg=0;
    } //end of for loop #2



    cout << "Do you want to do it again? (y/n:)";
    cin >> quest;

    if (quest == 'n'){
    break;
    }
    else{
    system ("cls"); //clears screen
    //inFile.close();
    // inFile.open("input4.txt");
    //data =0;
    //if (!inFile)
    //{
    //cerr << "Error Opening File" << endl;
    //system ("pause");
    //return 1;
    // }
    } //end of else
    } //end of while loop



    inFile.close();
    system ("pause");
    return 0;
    }
     
    GeekBoy, Apr 11, 2007
    #1
    1. Advertising

  2. GeekBoy

    Old Wolf Guest

    On Apr 11, 11:47 am, "GeekBoy" <> wrote:
    > Now for the new problem. Program on first run works flawlessly.
    > Next runs enter in some data I don;t know what instead of the data in
    > the file. Tried closing and opening file again, but it's not working.


    You will need to be more specific than "not working".

    Also, what do you mean by "next runs". Did you quit the program
    and run it again, or did you actually mean that you went through
    your main loop again in the same run?

    > while (months >= 1 && months <= 12)
    > {
    > cout << "Please enter the number of months/year to be averaged: ";
    > cin >> months;
    >
    > cout << "Year/" << endl;
    > cout << " /Month";
    >
    > for (x = 1; x <= months; x++) //prints out number of months header


    What will happen if they enter -1, or 15, or "Hello", for the
    number of months?

    In fact, what is the purpose of the 'while' condition, since you
    never check it after the user input?
     
    Old Wolf, Apr 11, 2007
    #2
    1. Advertising

  3. GeekBoy

    GeekBoy Guest

    "Old Wolf" <> wrote in message
    news:...
    > On Apr 11, 11:47 am, "GeekBoy" <> wrote:
    >> Now for the new problem. Program on first run works flawlessly.
    >> Next runs enter in some data I don;t know what instead of the data in
    >> the file. Tried closing and opening file again, but it's not working.

    >
    > You will need to be more specific than "not working".
    >
    > Also, what do you mean by "next runs". Did you quit the program
    > and run it again, or did you actually mean that you went through
    > your main loop again in the same run?



    The latter. Running again in the same loop.
    Running it on inital run alaways functions properly.


    >
    >> while (months >= 1 && months <= 12)
    >> {
    >> cout << "Please enter the number of months/year to be averaged:
    >> ";
    >> cin >> months;
    >>
    >> cout << "Year/" << endl;
    >> cout << " /Month";
    >>
    >> for (x = 1; x <= months; x++) //prints out number of
    >> months header

    >
    > What will happen if they enter -1, or 15, or "Hello", for the
    > number of months?
    >
    > In fact, what is the purpose of the 'while' condition, since you
    > never check it after the user input?


    Oppppss...Thanks for alerting me to that...time to do another while loop.


    >
     
    GeekBoy, Apr 11, 2007
    #3
  4. GeekBoy

    GeekBoy Guest

    Re: Reading numbers with FOR loop solved: New problem emerges - updated

    Just don't know what I am doing.

    It functions a bit better

    -----------------------------------------------------------------------

    #include<iomanip>
    #include<fstream>
    #include<iostream>
    #include<string>


    using namespace std;

    int main()
    {

    ifstream inFile;
    int months = 1;
    char quest = 'y';
    int x, y, z;
    float avg = 0, data;
    string dummy_string_buffer;

    inFile.open("input4.txt");
    if (!inFile)
    {
    cerr << "Error Opening File" << endl;
    system ("pause");
    return 1;
    }


    while (quest != 'n')
    {



    cout << "Please enter the number of months/year to be averaged: ";
    cin >> months;

    while (months >= 1 && months <= 12)
    {
    cout << "Please enter the number of months/year to be averaged: ";
    cin >> months;
    cout << "Year/" << endl;
    cout << " /Month";

    for (x = 1; x <= months; x++) //prints out number of months header

    {
    cout << setw(4) << x;
    } // end of for loop #1

    cout << " Avg\n" << endl ;

    for (y = 1; y <= 10; y++) // loop for 10 years of rain fall data
    {
    cout << setw(2)<< y << " ";

    for (z = 1; z <= months; z++) // loop for (x)months of rain fall
    {
    inFile >> data;
    cout << setw(4) << data << setw(4);
    avg = data + avg;
    } // end of for loop #3

    getline(inFile, dummy_string_buffer); // Gets a line from
    the file and stores it in dummy_string_buffer
    // cout << dummy_string_buffer;
    cout <<": " << setw(2) <<
    static_cast<int>(avg/static_cast<float>(months));
    cout << " inches\n";
    avg=0;
    } //end of for loop #2



    cout << "Do you want to do it again? (y/n:)";
    cin >> quest;

    if (quest == 'n'){
    break;
    }
    else{
    system ("cls"); //clears screen
    //inFile.close();
    // inFile.open("input4.txt");
    //data =0;
    //if (!inFile)
    //{
    //cerr << "Error Opening File" << endl;
    //system ("pause");
    //return 1;
    // }
    } //end of else
    }//end of while loop #2
    } //end of while loop #1



    inFile.close();
    system ("pause");
    return 0;
    }
     
    GeekBoy, Apr 11, 2007
    #4
  5. GeekBoy

    Old Wolf Guest

    Re: Reading numbers with FOR loop solved: New problem emerges - updated

    On Apr 11, 2:14 pm, "GeekBoy" <> wrote:
    > Just don't know what I am doing.
    > It functions a bit better


    That's not very helpful either. You should say:
    - what input you provided
    - what output you expected
    - what output actually happened

    > while (quest != 'n')
    > {
    > cout << "Please enter the number of months/year to be averaged: ";
    > cin >> months;
    >
    > while (months >= 1 && months <= 12)
    > {
    > cout << "Please enter the number of months/year to be averaged: ";
    > cin >> months;


    So you ask for the months twice in a row, and still do not bother
    to check what they typed in for the second one.

    You have to check the result of every input (user input and
    file read) to check that it was successful, and that it was
    what you were expecting. If you are getting strange behaviour
    then try to print out exactly what the inputs read; it might
    not be what you thought.
     
    Old Wolf, Apr 11, 2007
    #5
  6. GeekBoy

    GeekBoy Guest

    Re: Reading numbers with FOR loop solved: New problem emerges - updated

    "Old Wolf" <> wrote in message
    news:...
    > On Apr 11, 2:14 pm, "GeekBoy" <> wrote:
    >> Just don't know what I am doing.
    >> It functions a bit better

    >
    > That's not very helpful either. You should say:
    > - what input you provided
    > - what output you expected
    > - what output actually happened


    Same thing as before.
    The input file is not read and outputs a single number for ALL data fields


    >
    >> while (quest != 'n')
    >> {
    >> cout << "Please enter the number of months/year to be averaged: ";
    >> cin >> months;
    >>
    >> while (months >= 1 && months <= 12)
    >> {
    >> cout << "Please enter the number of months/year to be averaged: ";
    >> cin >> months;

    >
    > So you ask for the months twice in a row, and still do not bother
    > to check what they typed in for the second one.
    >
    > You have to check the result of every input (user input and
    > file read) to check that it was successful, and that it was
    > what you were expecting. If you are getting strange behaviour
    > then try to print out exactly what the inputs read; it might
    > not be what you thought.
    >
     
    GeekBoy, Apr 11, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bo
    Replies:
    2
    Views:
    334
    John Harrison
    Aug 22, 2003
  2. Ernst Berg

    Solved by reading this Group!

    Ernst Berg, Dec 6, 2003, in forum: C Programming
    Replies:
    5
    Views:
    322
    Ernst Berg
    Dec 12, 2003
  3. Michael Bacarella
    Replies:
    26
    Views:
    1,390
    harri
    Nov 20, 2007
  4. cwdjr
    Replies:
    0
    Views:
    75
    cwdjr
    Jul 14, 2003
  5. Isaac Won
    Replies:
    9
    Views:
    407
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page