C++ string to C string
May 6, 2008 at 2:07am UTC
Hi I have to change my program from C++ string class to c string. I have done as my book says, but somehow it is not reading properly and I am getting an infinate loop. Here is my code if someone could give me an idea of what I am doing wrong. Thanks so much in advance.
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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
#include <iostream> // input/output declarations
#include <iomanip> // i/o manupulator declarations
#include <fstream> //output file declarations
#include <string>
using namespace std;
using std::ofstream;
using std::ios;
using std::string;
class masterInfo //create class
{
private : //private information
int id; //employee id number
char name[21]; //employee name
double payrate; //employee pay rate
int numDep; //employee number of dependents
int empType; //employee type union or management
public : //public information
void getid ();
void getname ();
void getpayrate();
void getnumDep();
void getempType();
char masterInfo::returnname();
int masterInfo::returnid ();
double masterInfo::returnpayrate ();
int masterInfo::returnnumDep ();
int masterInfo::returnempType ();
};
//declare class information
int masterInfo::returnid()
{
return id;
}
void masterInfo::getid()
{
cin >> id;
}
char masterInfo::returnname()
{
return name[21];
}
void masterInfo::getname()
{
cin.get( name, 21, '\n' );
}
double masterInfo::returnpayrate ()
{
return payrate;
}
void masterInfo::getpayrate()
{
cin >> payrate;
}
int masterInfo::returnnumDep ()
{
return numDep;
}
void masterInfo::getnumDep()
{
cin >> numDep;
}
int masterInfo::returnempType ()
{
return empType;
}
void masterInfo::getempType()
{
cin >> empType;
}
//begin main
int main ()
{
int empNum = 1;
int empHrs = empNum;
masterInfo x;
masterInfo timeSheet [5]; //timesheet array
double hrsWkd; //number of hours worked by employee
string empname;
double grossPay; //amount of pay before tax and insurance
double tax; //taxes paid by employee
double ins; //insurance paid by employee
double netPay; //amount of pay after tax and insurance
cout << setiosflags(ios::fixed | ios::showpoint) << setprecision(2); //set decimal places
//open outputfile
ofstream fout("payroll_report.txt" );
if (!fout)
{
cout << "file not open" ;
}
//begin for loop
for (int empNum = 0; empNum <5; empNum++)
{
cout << "Enter information for employee " << empNum + 1<< endl;
cout <<"Employee id: " ;
timeSheet[empNum].getid();
while (timeSheet[empNum].returnid() < 0)
{
cout << "Error-Employee id must be greater than zero. Please re-enter transaction " <<endl;
timeSheet[empNum].getid();
}
cout <<endl;
cout <<"Employee name: " ;
timeSheet[empNum].getname();
cout <<endl;
cout <<"Pay rate: " ;
timeSheet[empNum].getpayrate();
while (timeSheet[empNum].returnpayrate() < 0)
{
cout << "Error-Pay Rate must be greater than zero. Please re-enter transaction " <<endl;
timeSheet[empNum].getpayrate();
}
cout <<endl;
cout <<"Dependents: " ;
timeSheet[empNum].getnumDep();
while (timeSheet[empNum].returnnumDep() < 0)
{
cout << "Error-Number of Dependents must not be negative. Please re-enter transaction " <<endl;
timeSheet[empNum].getnumDep();
}
cout <<endl;
cout <<"Type: " ;
timeSheet[empNum].getempType();
while (timeSheet[empNum].returnempType() < 0 || timeSheet[empNum].returnempType() > 1)
{
cout << "Error-Employee Type must be either 0 for union or 1 for management. Please re-enter transaction " <<endl;
timeSheet[empNum].getempType();
}
cout <<endl;
}
//begin second for loop
cout <<"Enter timecard information for each employee:" <<endl;
for (int empHrs = 0; empHrs <5; empHrs++)
{
cout <<"Hours worked for " <<timeSheet[empHrs].returnname()<<":" <<endl;
cin >> hrsWkd;
while (hrsWkd <= 0) //nested while loop
{
cout << "Error-Number of Hours Worked must not be negative. Please re-enter transaction " <<endl;
cin >> hrsWkd;
}
if (timeSheet[empHrs].returnempType() == 1) //nested if else
{
if (hrsWkd <= 40)
{
grossPay = (hrsWkd * timeSheet[empHrs].returnpayrate());
}
else
{
grossPay = ((hrsWkd - 40) * (timeSheet[empHrs].returnpayrate() * 1.5)) + (40 * timeSheet[empHrs].returnpayrate());
}
}
else
grossPay = hrsWkd * timeSheet[empHrs].returnpayrate();
tax = (grossPay * .15);
ins = (20 * timeSheet[empHrs].returnnumDep());
netPay = grossPay - ins - tax;
//begin output to file information
fout << setiosflags(ios::fixed | ios::showpoint) << setprecision(2);
fout <<setw(15)<<left<<"Employee ID: " <<setw(8) <<timeSheet[empHrs].returnid() <<endl;;
fout <<setw(15)<<left<< "Employee Name: " <<setw(15)<<timeSheet[empHrs].returnname()<<endl;
fout <<setw(15)<<left<< "Tax: " << setw(8)<<right<<tax << endl;
fout <<setw(15)<<left<< "Insurance: " <<setw(8)<<right<<ins << endl;
fout <<setw(15)<<left<<"Gross Pay: " <<setw(8)<<right<<grossPay <<endl;
fout <<setw(15)<<left<< "Net Pay: " <<setw(8)<<right<<netPay << endl;
fout <<setw(15)<<"" <<endl;
}
system ("PAUSE" );
return 0;
}
May 6, 2008 at 6:34am UTC
Hi cannsyl,
U need to fflush the standard input before input from user.
i.e. prior to using "cin>>" use
fflush(stdin);
in all the places, where cin is being used.
May 6, 2008 at 8:08am UTC
>> I have to change my program from C++ string class to c string.
Why??
1 2 3 4 5
char masterInfo::returnname()
{
return name[21];
}
This doesn't return the name, but it returns one character read from a location outside the array. Probably the first byte of double payrate.
May 6, 2008 at 7:02pm UTC
1 2 3 4
char masterInfo::returnname()
{
return name[21]; // This returns the last char + 1. Invalid memory
}
Should be:
1 2 3
const char * masterInfo::returnname() {
// etc
}
May 7, 2008 at 12:00am UTC
ok for whatever reason I can't get anything to work properly.
All the tutors at my university have apparently left for summer already and I have two programs to resolve by friday. I am desperate. I need help please. here is my problem. I need c strings for 20 characters instead of c++ strings (my professor wants it that way) and i need to be able to read in a file instead of user input. I am not getting either to work. WHY??? Please help me.
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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
#include <iostream> // input/output declarations
#include <iomanip> // i/o manupulator declarations
#include <fstream> //output file declarations
#include <string>
using namespace std;
using std::ofstream;
using std::ios;
using std::string;
class masterInfo //create class
{
private : //private information
int id; //employee id number
char name[21]; //employee name
double payrate; //employee pay rate
int numDep; //employee number of dependents
int empType; //employee type union or management
public : //public information
void getid ();
void getname ();
void getpayrate();
void getnumDep();
void getempType();
const char * masterInfo::returnname();
int masterInfo::returnid ();
double masterInfo::returnpayrate ();
int masterInfo::returnnumDep ();
int masterInfo::returnempType ();
};
//declare class information
int masterInfo::returnid()
{
return id;
}
void masterInfo::getid()
{
cin >> id;
}
const char * masterInfo::returnname()
{
return name[21];
}
void masterInfo::getname()
{
cin.getline( name, 21, '\n' );
}
double masterInfo::returnpayrate ()
{
return payrate;
}
void masterInfo::getpayrate()
{
cin >> payrate;
}
int masterInfo::returnnumDep ()
{
return numDep;
}
void masterInfo::getnumDep()
{
cin >> numDep;
}
int masterInfo::returnempType ()
{
return empType;
}
void masterInfo::getempType()
{
cin >> empType;
}
//begin main
int main ()
{
int empNum = 1;
int empHrs = empNum;
masterInfo x;
masterInfo timeSheet [5]; //timesheet array
double hrsWkd; //number of hours worked by employee
string empname;
double grossPay; //amount of pay before tax and insurance
double tax; //taxes paid by employee
double ins; //insurance paid by employee
double netPay; //amount of pay after tax and insurance
int y = 0;
int sum = 0;
cout << setiosflags(ios::fixed | ios::showpoint) << setprecision(2); //set decimal places
//open outputfile
ofstream fout("payroll_report.txt" );
if (!fout)
{
cout << "file not open" ;
}
//begin for loop
for (int empNum = 0; empNum <5; empNum++)
{
cout << "Enter information for employee " << empNum + 1<< endl;
cout <<"Employee id: " ;
timeSheet[empNum].getid();
while (timeSheet[empNum].returnid() < 0)
{
cout << "Error-Employee id must be greater than zero. Please re-enter transaction " <<endl;
timeSheet[empNum].getid();
}
cout <<endl;
cout <<"Employee name: " ;
timeSheet[empNum].getname();
cout <<endl;
cout <<"Pay rate: " ;
timeSheet[empNum].getpayrate();
while (timeSheet[empNum].returnpayrate() < 0)
{
cout << "Error-Pay Rate must be greater than zero. Please re-enter transaction " <<endl;
timeSheet[empNum].getpayrate();
}
cout <<endl;
cout <<"Dependents: " ;
timeSheet[empNum].getnumDep();
while (timeSheet[empNum].returnnumDep() < 0)
{
cout << "Error-Number of Dependents must not be negative. Please re-enter transaction " <<endl;
timeSheet[empNum].getnumDep();
}
cout <<endl;
cout <<"Type: " ;
timeSheet[empNum].getempType();
while (timeSheet[empNum].returnempType() < 0 || timeSheet[empNum].returnempType() > 1)
{
cout << "Error-Employee Type must be either 0 for union or 1 for management. Please re-enter transaction " <<endl;
timeSheet[empNum].getempType();
}
cout <<endl;
}
//begin second for loop
cout <<"Enter timecard information for each employee:" <<endl;
for (int empHrs = 0; empHrs <5; empHrs++)
{
cout <<"Hours worked for " <<timeSheet[empHrs].returnname()<<":" <<endl;
cin >> hrsWkd;
while (hrsWkd <= 0) //nested while loop
{
cout << "Error-Number of Hours Worked must not be negative. Please re-enter transaction " <<endl;
cin >> hrsWkd;
}
if (timeSheet[empHrs].returnempType() == 1) //nested if else
{
if (hrsWkd <= 40)
{
grossPay = (hrsWkd * timeSheet[empHrs].returnpayrate());
}
else
{
grossPay = ((hrsWkd - 40) * (timeSheet[empHrs].returnpayrate() * 1.5)) + (40 * timeSheet[empHrs].returnpayrate());
}
}
else
grossPay = hrsWkd * timeSheet[empHrs].returnpayrate();
tax = (grossPay * .15);
ins = (20 * timeSheet[empHrs].returnnumDep());
netPay = grossPay - ins - tax;
//begin output to file information
fout << setiosflags(ios::fixed | ios::showpoint) << setprecision(2);
fout <<setw(15)<<left<<"Employee ID: " <<setw(8) <<timeSheet[empHrs].returnid() <<endl;;
fout <<setw(15)<<left<< "Employee Name: " <<setw(15)<<timeSheet[empHrs].returnname()<<endl;
fout <<setw(15)<<left<< "Tax: " << setw(8)<<right<<tax << endl;
fout <<setw(15)<<left<< "Insurance: " <<setw(8)<<right<<ins << endl;
fout <<setw(15)<<left<<"Gross Pay: " <<setw(8)<<right<<grossPay <<endl;
fout <<setw(15)<<left<< "Net Pay: " <<setw(8)<<right<<netPay << endl;
fout <<setw(15)<<"" <<endl;
}
system ("PAUSE" );
return 0;
}
May 7, 2008 at 4:25am UTC
I think after the inputs U should be able to get the program done.
If still U r facing problem, Please state what kind of problem U r facing?
So that one can help U to sort out that problem...
Topic archived. No new replies allowed.