If Statement with Three Strings?

Ok, I am somewhat new to C++ but I should be getting by now. I am having a syntax problem (I think). I am getting input from a file located in the same directory as the program, when I compile it gets the info fine, but I need the computer to take the letter representing the insurance code (this is a payroll program) which is one letter, either N, S, or F, and give it an amount.The area of focus is the top function called "inscalc". The program takes the information, which is just a single letter, in string type form, and I need the computer to decide whether it is a valid entry, either N,S, or F, and then assign the proper value to the variable and return it back to the main program. I am lost on how to do this. As you can see, I try if statements but am lost. Any help would be appreciated. I am trying to make this understandable and easy for you guys, let me know if you can help. Thanks. -David

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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <stdlib.h>
using namespace std;

double inscalc(string inscode){
       double ins;
       int q=0;
       cout << inscode << endl;  
       if (q<1 && inscode == "N" or "n"){
       ins = 0.00;
       q++;
       } 
       cout << q << endl;
       cin.get();
       else if(q<1 && inscode == "S" or "s"){
       ins = 9.50;
       q++;
       }
       else if(q<1 && inscode == "F" or "f"){
       ins = 27.75;
       q++;
       }
       else{
       cout << "Insurance Code Invalid";     
       }     
       cout << ins << endl;
       return ins;
       
       
       
       }

double ratecalc (double rate, double hours){
       double payment=0, ot=0, addedOT=0;
       if(hours > 40)
         {
         double ot = hours - 40;
         double addedOT = (ot * rate) * 2;      
         double payment = addedOT + (rate * (hours - ot));
        
         }
       else 
       {
       double payment = rate * hours;
      
       }
       
       return payment;
       }

int main () {
    string name;
    double rate, hours;
    string inscode;

  string line;
  ifstream myfile ("payrollinfo.txt");
  if (myfile.is_open())
  { cout << "Program Started\n\n\n" << endl;

    int i=0;
    while (getline(myfile,line))
    {
      name = line.substr(0,21);          
      rate = atof(line.substr(22,5).c_str());   
      hours = atof(line.substr(30,2).c_str());   
      inscode = line.substr(33,1);
     
      double ins = inscalc(inscode);
      double payment = ratecalc(rate, hours);
      if(i<1) {
cout << fixed << "Name" 
<< setw(22) << "Rate"
<< setw(9) << "Hours "
<< setw(5) << "INS" 
<< setw(12) << "Insurance" 
<< endl;
      i++;
      }


cout << left << setw(20) << name 
<< right << setw(5) << setprecision(2) <<  rate 
<< setw(6) <<setprecision(0) << hours 
<< setw(7) << inscode   
<< setw(11) <<setprecision(2) << ins
<< endl; 
      
            
      
    }
    myfile.close();
  }

  else cout << "Unable to open file"; 
  cout << "\n\n\nProgram Finished" << endl;
  cin.get();



          
        
}

    
Last edited on
Oh and here is the input for the program if anyone wants it.

1
2
3
4
5
6
7
Tom Anderson           7.52   45 N
Bob Conerley   	      17.50   40 S
John Potter            9.30   35 S
Terrance Appleby      31.00   42 F
Joseph Rinker         17.00   35 F
Todd Russell           5.00   30 S
Bill Ryan             18.25   45 N
Like the 'and' operator being '&&', the 'or' operator is '||'. Lines such as

if (q<1 && inscode == "N" or "n")

should be

if (q<1 && (inscode == "N" || inscode == "n"))

Note the additional parenthesis () around the N || n section, to make sure the correct logic is used and/or obvious to the reader.
Thank you Jim80y, God bless!
Topic archived. No new replies allowed.