Thread: sscanf issue

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    5

    sscanf issue

    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.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Look at your post and which way the " are leaning.

    You get this when you copy from people who've used MS-Word to write code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    What ON EARTH would lead you to think that this has something to do with sscanf()?

    As Salem said your code is peppered with garbage characters. If it was written in Word (which is unfathomable!) then save it as text and try to fix up the oddities which remain. Failing that, rewrite it, and don't write code using a word processor again.

    EDIT: Also, you are including a .cpp file? What sort of abomination is that

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I like scanf() myself, but in some C++ circles, when you use it over cin you may find yourself the enemy. As far as coding in Word goes, I remember when I was taking word processing in High School. Since Word was the only word processor accessible on our machines I would commonly use it to write code... not that I would ever encourage programming over other school work.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I don't know... This looks like one fabolous mess of mixed C and C++ code.
    The employee pointers array doesn't seem to be used at all, for another. And thank goodness for that, I'd bet we'd see for access violations somewhere.
    And never ever include .cpp files. ONLY headers. ONLY.
    I suggest you start re-writing this to C++ code instead.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. float calculation issue
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-26-2008, 04:56 AM
  2. Problems reading formatted input with sscanf
    By Nazgulled in forum C Programming
    Replies: 17
    Last Post: 05-10-2006, 12:46 AM
  3. annoying validation issue with sscanf
    By Axel in forum C Programming
    Replies: 15
    Last Post: 10-24-2005, 09:08 AM
  4. sscanf()
    By task in forum C Programming
    Replies: 4
    Last Post: 11-22-2003, 04:43 PM
  5. sscanf (I think)
    By RyeDunn in forum C Programming
    Replies: 7
    Last Post: 07-31-2002, 08:46 AM