Thread: Code works in MS Visual C++, not in Dev-C++

  1. #1
    Kill Clear Channel Kheila's Avatar
    Join Date
    Oct 2005
    Location
    St. Paul, MN
    Posts
    23

    Code works in MS Visual C++, not in Dev-C++

    I wrote some code that compiles just fine in MS Visual C++ at school, but I brought it home and Dev-C++ has a problem with it. I wrote my own header called phone.h, here's the code for it:
    (I put the error message from the compiler with the corresponding lines, each one applies to the line that is directly below it)

    Code:
    //parse error before numeric constant 
    const int FALSE=0;
    //parse error before numeric constant 
    const int TRUE=1;
    const int LISTMAX=20;
    const int NAMEMAX=30;
    
    struct phoneNumber
    {
    	char name[NAMEMAX];
    	int areaCode;
    	int exchange;
    	int number;
    };
    
    void menu();
    void editList(phoneNumber[], int);
    void printList(phoneNumber[], int);
    void printMember(phoneNumber[], int);
    int saveList(phoneNumber[], int, int, char*);
    Here are my #include statements (once again, the error messages are in the comments, they pertain to the line directly below):

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <cctype>
    #include <string>
    // In file included from phone.cpp 
    #include "phone.h"
    
    using namespace std;
    I doubt the problem is with the rest of the code, but if you think it might be, let me know and I'll post that too.

    Any ideas are greatly appreciated. Thanks.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    the problem is somewhere else. I didn't have any problem compiling what you posted with Dev-C++. Is phone.h being included more than once?
    Code:
    #ifndef PHONE_H
    #define PHONE_H
    // your header here
    
    #endif // HEADER_H

  3. #3
    Kill Clear Channel Kheila's Avatar
    Join Date
    Oct 2005
    Location
    St. Paul, MN
    Posts
    23

    Angry

    No, I double-checked that phone.h was only in there one time. I tried creating a new project and copying the header code into a new header and the include statements plus a blank main function into a new .cpp file and still got the same errors. Here's the rest of the code anyways (it's not quite done, it doesn't do what it's supposed to do, but at least it should compile so I can fix the errors!):
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <cctype>
    #include <string>
    #include "phone.h"
    
    using namespace std;
    
    int modFlag=FALSE;
    phoneNumber phoneList[LISTMAX];
    
    int main(int argc, char*argv[])
    {
    	//pass the phonelist file name in on the command line
    
    	char c;
    	ifstream infile;
    	
    	if (argc<2)
    	{
    		cout << "You must open this file from the command prompt." << endl;
    		cout << "Type the .exe file name and the name of the .dat file";
    		cout <<  "after it, separated by spaces, and press enter." << endl;
    	}
    	else
    	{
    		infile.open(argv[1], ios::in | ios::binary);
    		if (infile)
    			infile.read((char*)phoneList, LISTMAX*sizeof(phoneNumber));
    		infile.close();
    	
    	
    		printList(phoneList, LISTMAX);
    		
    		cout << "Enter the first letter of your choice and press enter: " << endl;
    		menu();
    		cin >> c;
    
    		while (tolower(c)!='q')
    		{
    		switch (c)
    		{
    			case 'e': 
    				editList(phoneList, LISTMAX);
    				modFlag=TRUE;
    				break;
    			case 'l':
    				printList(phoneList, LISTMAX);
    				break;
    			case 's':
    				if (!saveList(phoneList, sizeof(phoneNumber), LISTMAX, argv[1]))
    				cout << "Error2." << endl;
    				else
    				modFlag=FALSE;
    				break;
    		}
    		cout << "Enter the first letter of your choice and press enter: " << endl;
    		menu();
    		cin >> c;
    		}
    
    		if (modFlag==TRUE)
    		{
    			cout << "Please save the file." << endl;
    			cin >> c;
    			if (tolower(c)=='y')
    			{
    				if (!saveList(phoneList, sizeof(phoneNumber), LISTMAX, argv[1]))
    				cout << "Error3." << endl;
    				else
    				modFlag=FALSE;
    			}
    		}
    	}
    	return 0;
    }
    
    void menu()
    {
    	cout << "	(E)dit		(L)ist		(S)ave		(Q)uit		" << endl;
    }
    
    void editList(phoneNumber L[], int n)
    {
    	int i;
    	char ch;
    	
    	cout << "Enter the index number (1 through 20) of a phone list item." << endl;
    	cin >> i;
    	cin.ignore(10, '\n');		//strips whitespace to prevent problems with getline()
    	i--;						//adjust to array index(0 to n-1)
    	if (i<=n)
    	{
    		cout << "Enter the name:" << endl;
    		cin.getline(L[n].name, NAMEMAX);
    		cout << "Enter the phone number: [ex. (123) 456-7890]" << endl;
    		//uses ch variable to read and ignore the (, ), and - characters
    		cin >> ch;
    		cin >> L[n].areaCode;
    		cin >> ch;
    		cin >> L[n].exchange;
    		cin >> ch;
    		cin >> L[n].number;
    	
    	}	
    }
    
    void printList(phoneNumber L[], int n)
    {
    	int i;
    
    	for (i=1; i<=n; i++)
    		printMember(L, i);
    }
    
    void printMember(phoneNumber L[], int i)
    {
    	cout << setw(3) << i << ". " << L[i].name;
    	cout << setw(30) << "(" << L[i].areaCode << ") " << L[i].exchange << "-" << L[i].number << endl;
    }
    
    int saveList(phoneNumber L[], int recSize, int arraySize, char* fname)
    {
    	int rtrn;
    	ofstream outfile;
    
    	outfile.open(fname, ios::out|ios::trunc|ios::binary);
    	if (!outfile)
    		rtrn=FALSE;
    	
    	else if (!outfile.write((char*)L, recSize*arraySize))
    		{
    			outfile.close();
    			rtrn=FALSE;
    		}
    		else 
    		{
    		outfile.close();
    		rtrn=TRUE;
    		}
    	
    	return rtrn;
    }
    Arrgh. So frustrating.
    to Absent Toast

  4. #4
    Kill Clear Channel Kheila's Avatar
    Join Date
    Oct 2005
    Location
    St. Paul, MN
    Posts
    23
    Oh, and I did try updating my Dev-C++ software. Currently I'm using version 4.9.9.2. I'm going to try downloading some different software. Maybe it'll work then.
    to Absent Toast

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    This compile perfectly for me on Dev-C++.

    Be sure that you're starting a project and are compiling both files in the project.

    Don't compile them seperately and expect them to work.
    Sent from my iPadŽ

  6. #6
    Kill Clear Channel Kheila's Avatar
    Join Date
    Oct 2005
    Location
    St. Paul, MN
    Posts
    23
    Yup, they are in a project, I made sure.

    This is really weird.
    to Absent Toast

  7. #7
    Kill Clear Channel Kheila's Avatar
    Join Date
    Oct 2005
    Location
    St. Paul, MN
    Posts
    23
    Well, I tried uninstalling Dev-C++, including all configurations, and reinstalling it. Now it will compile. Strange, since I hadn't changed any major settings. Thanks for checking it out for me, though.
    to Absent Toast

  8. #8
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    When in doubt, blame it on the gnomes.
    Sent from my iPadŽ

  9. #9
    Kill Clear Channel Kheila's Avatar
    Join Date
    Oct 2005
    Location
    St. Paul, MN
    Posts
    23
    Haha, I'll remember that.
    to Absent Toast

  10. #10
    Super Moderator Harbinger's Avatar
    Join Date
    Nov 2004
    Posts
    74
    //parse error before numeric constant
    const int FALSE=0;

    Perhaps your dev-c++ has something like
    #define FALSE 0
    in the background, which is throwing your code off.

    Besides, since C++ has true and false (in the bool type) anyway, why are you defining your own versions?

  11. #11
    Kill Clear Channel Kheila's Avatar
    Join Date
    Oct 2005
    Location
    St. Paul, MN
    Posts
    23
    Because my professor told us to
    to Absent Toast

  12. #12
    Super Moderator Harbinger's Avatar
    Join Date
    Nov 2004
    Posts
    74
    So I guess your prof is teaching you the compiler rather than the language. Make of that what you will, but if you think learning stuff is hard, try unlearning a bunch of half-truths and compiler specific stuff which no one else supports.

    It's pretty easy to actually teach the language, such that you have a reasonable degree of success in writing portable code, and your prof is already failing at that.

    > compiles just fine in MS Visual C++ at school,
    Please don't say it's visual 6.0

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Try renaming the constants and see if the problem persists. If it does, put these two at the top of the header:
    Code:
    #undef TRUE
    #undef FALSE
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  2. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  3. Errors with including winsock 2 lib
    By gamingdl'er in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2005, 08:13 PM
  4. why wont MS Visual C++ understand my #include <stdlib.h>
    By Master_Rondal in forum C Programming
    Replies: 5
    Last Post: 10-15-2002, 04:36 PM
  5. Works on Dev C++, Doesn't on Visual C++ :(
    By marcvb in forum C++ Programming
    Replies: 6
    Last Post: 11-26-2001, 08:39 AM