Thread: problem in code

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    6

    problem in code

    can anyone here figure out my problem in my code

    it reads from a dat file containing this.

    Code:
    C. Moore        1000    1250
    Dot Net         2000    2075
    Hop Tuit        500     2873
    Jack B. Nimble  2000    824
    Ron Ray Gunn    1000    2000
    Ura Winter      1000    500
    Summer Place    2000    1500
    Amber Reys      500     758
    Knot Wright     500     371
    Moore Wright    1000    1234
    Seymour Moore   500     500
    when it creates the out put file it looks like this
    Code:
                Number of Minutes
    Customer    Base Plan      Used/Previous Month    Quota
    
    C. Moore        1000        1250                  4068584
    Dot Net         2000        2075                     1125
    Hop Tuit        500        2873                     2311
    Jack B. Nimble  2000         824                      931
    Ron Ray Gunn    1000        2000                     2082
    Ura Winter      1000         500                     1240
    Summer Place    2000        1500                     2000
    Amber Reys      500         758                     2180
    Knot Wright     500         371                      576
    Moore Wright    1000        1234                      500
    Seymour Moore   500         500                     1123
    the problem is that the very first quota that is printed to the outfile is some random number and the last quota that is suppose to be printed isnt there. I don't know where the number came from. Can i please get some help this is due at 10 pm tonight!!!!.

    Code:
    // Damien McKeown
    // PROGRAM 9
    // CSC 101
    
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
        
        
        ifstream infile;
        ofstream outfile;
        
        int bsplan, useprevmonth, monthaccum = 0, quota;
        
        char custmr;
        int loopcnt, max, min, bonus;
        float average ;
        
    
        
        //open input file
        infile.open("Sell_U_R.dat", ios::in);
        
        //open output file and set flags
        outfile.open("McKe_customer.out");
      
        
        outfile << setw(30)<< "Number of Minutes\n";
        outfile << "Customer" << setw(13) << "Base Plan" << setw(25) << "Used/Previous Month" << setw(11) << "Quota\n\n";
        
        
        // loop to read till end of file
        infile.get(custmr); // priming read
        while (infile)  // eof -controlled loop
        {
            outfile << custmr;
            loopcnt = 1;
            while (loopcnt <= 15)
            { infile.get(custmr);
            outfile << custmr;
            loopcnt++;   
            }
            
            infile >> bsplan >> useprevmonth;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
            outfile <<setw(2) << bsplan << setw(12) << useprevmonth << setw(25) << quota << endl;
            
            monthaccum += useprevmonth;
            
            //
            if (useprevmonth > 2000)
               {bonus = (useprevmonth * .15) + .5;
               quota = (bsplan + bonus);
               }
               else if (useprevmonth >= 1500 && useprevmonth <= 2000)
               {bonus = (useprevmonth * .12) + .5;
               quota = (bsplan + bonus);
               }
               else if (useprevmonth > 500 && useprevmonth < 1500)
               {bonus = (useprevmonth * .10) + .5;
               quota = (bsplan + bonus);
               }
               else if(useprevmonth < 500)
               {quota = bsplan;}
               else if (useprevmonth == 500)
               { bonus = 1000;
               quota = (bsplan + bonus);
               }
           
            
            infile.get(custmr);
            infile.get(custmr);
            
            
            
            }
        
        outfile << "\n\n";
        
        
       
        
        infile.close();
        outfile.close();
    
    
    system ("pause");
    
    return 0;
    }

    THANK YOU
    Last edited by Damienmc++keown; 04-20-2011 at 05:06 PM.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    I would suggest that you start by initializing your variables.
    All of the variables except montaccum are uninitialized. And it looks like you print some of them before you initialize them.

    Code:
        int bsplan, useprevmonth, monthaccum = 0, quota;
        
        char custmr;
        int loopcnt, max, min, bonus;
        float average ;

    Jim

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    6
    Ok when i did that the it changed that random first quota number(4068584) to 0. so that is affecting it in some way

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    You are printing out quota before you are calculating it.
    In the following snippet you are printing quota in the first line but you don't calculate it until about 6 lines later.
    Code:
            outfile <<setw(2) << bsplan << setw(12) << useprevmonth << setw(25) << quota << endl;
            
            monthaccum += useprevmonth;
            
            //
            if (useprevmonth > 2000)
               {bonus = (useprevmonth * .15) + .5;
               quota = (bsplan + bonus);       /////////// This is the calculation.
    Jim

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    6
    YEAH thank you so much Jim that's exactly what it was.

    what a stupid mistake.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with Code ??
    By Gaurav Singh in forum C Programming
    Replies: 10
    Last Post: 03-09-2011, 02:04 AM
  2. Help on a code problem
    By Denisius in forum C Programming
    Replies: 10
    Last Post: 01-06-2007, 10:51 AM
  3. Problem with code - Can someone take a look???
    By danielp in forum C++ Programming
    Replies: 6
    Last Post: 11-01-2003, 06:07 PM
  4. Problem with some code
    By zacpack in forum C Programming
    Replies: 5
    Last Post: 03-17-2002, 06:01 PM