Infinite loop while using cin

Hi all,

I am taking a intro to c++ class and I am stuck. My code keeps looping to this:

Choose (o O) for overtime pay, (g G) for gross pay,(n N) for net pay,(a A) for all,(q Q) to Quit
quit

Here is my code:
//
// main.cpp
// M3 part b
//
// Created by synack23 on 6/20/16.
// Copyright © 2016 synack23. All rights reserved.
//



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string.h>
#include <cstdlib>
#include <ctype.h>

using namespace std;

int main()
{
    int employeeid[100],i=0,n=0;
    int hoursworked[100];
    int hourrate[100];
    float taxrate[100],overtime[100],overtimepay[100],regularpay[100];
    double grosspay[100],taxamount[100],netpay[100];
    char firstname[10][100],lastname[15][100],stat[1][100];
 ifstream data;
    data.open("employee.in",ios::in);
            cin>>firstname[i]>>lastname[i]>>stat[i]>>employeeid[i]>>hoursworked[i]>>hourrate[i];
            while(cin>>firstname[i]>>lastname[i]>>stat[i]>>employeeid[i]>>hoursworked[i]>>hourrate[i])loop to read data from employee.in
     {
        overtime[i]=0;
        overtimepay[i]=0;
        taxrate[i]=0;
        {   grosspay[i]=hourrate[i]*hoursworked[i];}//gross pay calculation
            {if (grosspay[i]>1000) taxrate[i]=.30;
            else if (grosspay[i]>500) taxrate[i]=.20;
            else taxrate[i]=.10;
            if (*stat[i]==(/* DISABLES CODE */ (83) ))taxrate[i]=taxrate[i]+.05;
            else if (*stat[i]==(/* DISABLES CODE */ (115) ))taxrate[i]=taxrate[i]+.05;
            else if (*stat[i]==(/* DISABLES CODE */ (72) )) taxrate[i]=taxrate[i]-.05;
            else if (*stat[i]==(/* DISABLES CODE */ (104) )) taxrate[i]=taxrate[i]-.05;
            else taxrate[i]=taxrate[i];
                taxamount[i]=grosspay[i]*taxrate[i];    }//tax rate calculations
        
        {   if (hoursworked[i]>40) overtime[i]=hoursworked[i]-40;
                if (overtime[i]==0) overtimepay[i]=0;
                else if (overtime[i]>0) overtimepay[i]=overtime[i]*hourrate[i]*1.5;
                regularpay[i]=grosspay[i]-overtimepay[i];}//overtime calculations
        
        {   netpay[i]=grosspay[i]-taxamount[i]+overtimepay[i];}//net pay calculations        
        char option;
        
        do{
            cout<<"Choose (o O) for overtime pay, (g G) for gross pay,(n N) for net pay,(a A) for all,(q Q) to Quit"<<endl;
         cin>>(option);
            switch(option){
                case 'o':case'O':
                    for (int i=0;i<n;i++)
                        cout<<"Enter the Last Name:"<<endl;
                    cin>>lastname[n];
                    cout<<"Overtime Pay:"<<overtimepay[n]<<endl;
                    n++;
                    break;
                case'g':case'G':
                    for (int i=0;i<n;i++)
                        cout<<"Enter the Last Name:"<<endl;
                    cin>>lastname[n];
                    cout<<"Gross Pay:"<<grosspay[n]<<endl;
                    n++;
                    break;
                case'n':case'N':
                    for (int i=0;i<n;i++)
                        cout<<"Enter the Last Name:"<<endl;
                    cin>>lastname[n];
                    cout<<"Net Pay:"<<netpay[n]<<endl;
                    n++;
                    break;
                case'a':case'A':
                    for (int i=0;i<n;i++)
                        cout<<"DrEbrahimi.com Payroll system"<<endl;
                    cout<<"C++ class work Michael Lamb M3 Assignment Part 2"<<endl;
                    cout<<"First Name Last Name Stat SSN   HW HR OTH  OTP   REGP  Gross Tax Net"<<endl;
                    cout<<"========== ========= ==== ====  == == ===  ===   ====  ===== === ==="<<endl;
                       cout<<std::setw(12)<<std::left<<firstname[i]<<std::setw(10)<<lastname[i]<<std::setw(4)<<stat[i]<<std::setw(6)<<employeeid[i]<<std::setw(3)<<hoursworked[i]<<std::setw(4)<< std::left<<hourrate[i]<<std::setw(4)<<overtime[i]<<std::setw(6)<< std::left<<overtimepay[i]<<std::setw(6)<< std::left<<regularpay[i]<<std::setw(6)<<grosspay[i]<<std::setw(4)<< std::left<<taxrate[i]*100<<std::setw(4)<< std::left<<netpay[i]<<endl;
                case'q':case'Q':;
                    //for (int i=0;i<n;i++)
                        cout<<"quit "<<endl;
                    break;
            }//switch
        }while((option !='q')&& (option='Q')) ;//do while
        return 0;
    }
//}//loop
   
}


I am using xcode. When I put a breakpoint in to the cin statemant option has a value of J so it is reading from my employee.in file. Help
Last edited on
line 32: cin is a really bad name for your input file stream. cin is defined in the standard library as the terminal input file. Yes, your local definition takes precedence, but it is very confusing to the reader.

line 34: You appear to be discarding the first record. from the file. For what reason is not clear.
You don;t check that this read was successful. Whatever you read is going to be overlaid by the next line. If the first record has column names, this read will probably set the fail bit for the file since you be attempting to read a column heading (alpha) into a numeric field . e.g. emplyeeid.

Line 35: If line 34 set the fail bit, this read will never succeed.

line 68-69: This for loop will output "Enter last name" n times, where n is the number of records read from the file. Many more for loops with this problem.

Lots more problems, but without code tags, I'm not going to respond further.

Please show us the format of your input file.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.



Last edited on
1
2
3
4
ifstream cin;
//...
cout<<"Choose (o O) for overtime pay, (g G) for gross pay,(n N) for net pay,(a A) for all,(q Q) to Quit"<<endl;
cin>>(option);
that part looks interactive, ¿why are you reading from a file?
AbstractionAnon

Thanks for the info, I am completely new to C++ programming, In the employee.in record I have the first record as 000000000 00000 0 0000 0000 0000 that is why I drop the first record. The code keeps looping at the

cout<<"Choose (o O) for overtime pay, (g G) for gross pay,(n N) for net pay,(a A) for all,(q Q) to Quit"<<endl;

line, it does not wait for input. When I set a breakpoint for cin>>option I get J as a value (I think it is reading from my employee.in file) how do I stop that?

Any advice would be greatly appeciated.

ne555

I don't want to read from a file, I just want the user to enter input, what do I change cin to?

I think it is reading from my employee.in file) how do I stop that?

Stop using cin as the name of your ifstream.

I already pointed out that was a very bad choice. You're trying to use cin to represent your ifstream, and your terminal input. You can't use it for both. How is the compiler supposed to know which one you mean?


PLEASE APPLY CODE TAGS TO YOUR POST AS PREVIOUSLY REQUESTED.


Hello AbstractionAnon,

I think I put the tags in you were asking about. Hopefully this will clarify what I was tring to do with my code.

Thanks for the help
It looks like you added multiple code blocks.
Since you have only one source file, you only need one set of code tags.

Put
[code]
before your first line of code and
[/code]
after your last line of code.
Last edited on
Topic archived. No new replies allowed.