Im working on homework for a class and have come across an issue with sscanf and C++.
Code:
#include <stdlib.h>
#include <stdio.h>
#include <fstream>
#include <string>
#include <iostream>
#include <iomanip>
using namespace std;
#define ARGSZ 2
#define COMMANDARG 2
#define MAX_EMPLOYEES 5
#define MAX_SALARIES 2
#define ONE 1
#define ZEROD 0.0
#define ZEROI 0;
class Employee;
void cmdLineTest(int, char *[]);
void fileTest(ifstream &);
void hire(Employee *[MAX_EMPLOYEES], ifstream &, string &);
void pay(Employee *[MAX_EMPLOYEES], ifstream &, string &);
void print(Employee *[MAX_EMPLOYEES]);
void promotions(Employee *[MAX_EMPLOYEES], ifstream &, string &);
void transfer(Employee *[MAX_EMPLOYEES], ifstream &, string &);
Code:
#include "hw4employeemf.cpp"
int main(int argc, char *argv[])
{
char selection;
string lineOfData;
int numEmployees = ZEROI;
Employee *ePtrs[MAX_EMPLOYEES] = {NULL};
cmdLineTest(argc, argv);
ifstream inFile(argv[1]);
fileTest(inFile);
getline(inFile, lineOfData);
while (!inFile.eof())
{
if ( (lineOfData.c_str())[0] == '*')
{
selection = (lineOfData.c_str())[2];
switch (selection)
{
case 'H': hire(ePtrs, inFile, lineOfData);
break;
case 'I': pay(ePtrs, inFile, lineOfData);
break;
case 'P': promotions(ePtrs, inFile, lineOfData);
break;
case 'T': transfer(ePtrs, inFile, lineOfData);
break;
default: break;
}
}
}
return 0;
}
void cmdLineTest(int argc, char *argv[])
{
if (argc == ARGSZ)
cout << "The argument supplied is " << argv[ARGSZ-ONE] << endl;
else
{
if (argc > COMMANDARG)
{
cout << "Too many arguments supplied. Exiting Program." << endl;
exit(1);
}
else
{
cout << "One argument expected. Exiting Program." << endl;
exit(1);
}
}
return;
}
void fileTest(ifstream &inFile)
{
if (!inFile)
{
cout << "File could not be opened. Exiting Program.\n";
exit(1);
}
else
{
cout << "File opened succesfully.\n";
}
}
void hire(Employee *ePtrs[], ifstream &inFile, string &lineOfData)
{
cout << "Hire Function Called.\n";
while(!inFile.eof())
{
char firstName[20], lastName[20], classification[20];
int id;
getline(inFile, lineOfData);
cout << "C++ STYLE STRING:" << lineOfData << endl;
if ( (lineOfData.c_str())[0] == '*')
{
cout << "RETURN\n";
return;
}
sscanf( (lineOfData.c_str()) , “%s” , lastName);
}
cout << "While Terminated\n";
}
void pay(Employee *[], ifstream &inFile, string &lineOfData)
{
cout << "Pay Function Called. \n";
while(!inFile.eof())
while(!inFile.eof())
{
char firstName[20], lastName[20], classification[20];
int id;
getline(inFile, lineOfData);
cout << "C++ STYLE STRING:" << lineOfData << endl;
if ( (lineOfData.c_str())[0] == '*')
{
cout << "RETURN\n";
return;
}
}
}
void print(Employee *[])
{
}
void promotions(Employee *[], ifstream &inFile, string &lineOfData)
{
cout << "Promotions Function Called. \n";
while(!inFile.eof())
{
char firstName[20], lastName[20], classification[20];
int id;
getline(inFile, lineOfData);
cout << "C++ STYLE STRING:" << lineOfData << endl;
if ( (lineOfData.c_str())[0] == '*')
{
cout << "RETURN\n";
return;
}
}
}
void transfer(Employee *[], ifstream &inFile, string &lineOfData)
{
cout << "Transfer Function Called. \n";
while(!inFile.eof())
{
char firstName[20], lastName[20], classification[20];
int id;
getline(inFile, lineOfData);
cout << "C++ STYLE STRING:" << lineOfData << endl;
if ( (lineOfData.c_str())[0] == '*')
{
cout << "RETURN\n";
return;
}
}
}
And now I get the following error from the GNU G++ Compiler:
Code:
[dgk6636@omega ~]$ g++ hw4driver.cpp
hw4driver.cpp:86: error: stray â\223â in program
hw4driver.cpp:86: error: stray â\224â in program
hw4driver.cpp: In function âvoid hire(Employee**, std::ifstream&, std::string&)â:
hw4driver.cpp:86: error: expected primary-expression before â%â token
hw4driver.cpp:86: error: âsâ was not declared in this scope
[dgk6636@omega ~]$
Anybody lend a helping hand? I know its the sscanf statement, but im not sure how i should correct it.