Error undeclared identifier

C++

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 4667 Views - Last Post: 23 August 2009 - 06:38 PM Rate Topic: -----

#1 Cbick  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 28
  • Joined: 08-August 09

Error undeclared identifier

Post icon  Posted 23 August 2009 - 10:39 AM

I can't figure can't figure out why this is comming up as undeclared.
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

void menu(void);
void writeData(void);
void readData(void);
string * split(string, char);

const char FileName[] = "c:/TestAddress.txt";

int main () {
		menu();
		return 0;
} //end main
void menu(void) {
	char choice = 'S';
	while (choice!='E') {
		cout << "What would you like to do with these records: " << endl << endl;
		cout << "(A)ppend records, (S)how records, or (E)xit  "  << endl;				
		cin >> choice;
		cin.ignore(1, '\n');

		if (choice=='A' || 'a') {
				writeData();
		} else if (choice=='S' || 's') {
				readData();
		} else if (choice!='E' || 'e') {
				cout << "'" << choice << "' You have choosen to exit."  << endl;
		}else
		{
			cout << "Invalid response" << endl;
		}
	}
}
void writeData(void){
//Write the Address Info to a file
   char choice = 'Y' || 'y';
   string name = "";
   string street = "";
   string city = "";
   string state = "";
   string zipCode = 0;
   int record = 0;
   ofstream outMyStream (FileName, ios::app);
	
		do {
			cout << "\nEnter The Name: ";
			getline(cin, name);
			cout << "\nEnter The Street: ";
			getline(cin, street);
			cout << "\nEnter The City: ";
			getline(cin, city);
			cout << "\nEnter The State: ";
			getline(cin, state);
			cout << "\nEnter The Code: ";
			cin >> zipCode;

			//write to the file
			outMyStream << name << "," << street << "," << city << "," << state << "," << zipCode;
			record++;
			cout << "\nEnter another Record? (Y/N) "; 
			cin >> choice;
			//discard any newline character in keyboard buffer
			cin.ignore(1, '\n');
   }
	while (choice == 'Y');
		outMyStream.close();  
}//end write data

void readData(void){
//read data from a file
   ifstream inMyStream (FileName);

   //read the first record
   string lineBuffer;


   while (!inMyStream.eof() ){
	  getline (inMyStream, lineBuffer, '\n');
	  //divide the line into seperate fields
	  string *theFields = split(lineBuffer, ',');
	  cout << "Record #: " << record << endl;
	  cout << "Name...... " << theFields[0] << endl;
	  cout << "Street.... " << theFields[1] << endl;
	  cout << "City...... " << theFields[2] << endl;
	  cout << "State..... " << theFields[3] << endl;
	  cout << "Zip code.. " << theFields[4] << endl;
}

inMyStream.close();
}//end read data

string * split(string theLine, char theDeliminator){
		//Break the line into fields and save the fields to an array.
		//Each field will occupy one element in a character array.
		//theLine is a string with fields separated with theDeliminator character.
		//Assumes the last field in terminated with a newline.
		//Useage: string *theFields = split(lineBuffer, ',');
		//determine how many splits there will be so we can size our array

		int splitCount = 0;
		for(unsigned int i = 0; i < theLine.size(); i++){
				if (theLine[i] == ',')
				splitCount++;
		}
		splitCount++; 
	   //add one more to the count because there is not an ending comma

		//create an array to hold the fields
		string* theFieldArray;
		theFieldArray = new string[splitCount];

		//split the string into seperate fields
		string theField = "";
		int commaCount = 0;

			  for( unsigned int i = 0; i < theLine.size(); i++ ){
			  //read each character and look the deliminator
			   if (theLine[i] != theDeliminator) {
					   theField += theLine[i]; //build the field
			   }
			  else { //the deliminator was hit so save to the field to the array
					   theFieldArray[commaCount] = theField;
					   //save the field to the array
					   theField = "";
					   commaCount++;
			  }
		}
		theFieldArray[commaCount] = theField; 
		 //the last field is not marked with a comma...

		return theFieldArray;
} //end split 


it comes up with this error: cpp(85) : error C2065: 'record' : undeclared identifier
but I have already declared record in my code. I cannot figure out how to fix this and I should have to redeclare "record "again.
Thanks for the helpin advance

Is This A Good Question/Topic? 0
  • +

Replies To: Error undeclared identifier

#2 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 1011
  • View blog
  • Posts: 4,215
  • Joined: 14-February 08

Re: Error undeclared identifier

Posted 23 August 2009 - 10:47 AM

*edit*

Missed the declaration, so never mind, iphoneorange is right.

This post has been edited by stayscrisp: 23 August 2009 - 10:49 AM

Was This Post Helpful? 0
  • +
  • -

#3 athlon32  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 117
  • View blog
  • Posts: 363
  • Joined: 20-August 08

Re: Error undeclared identifier

Posted 23 August 2009 - 10:47 AM

View PostCbick, on 23 Aug, 2009 - 09:39 AM, said:

I can't figure can't figure out why this is comming up as undeclared.
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

void menu(void);
void writeData(void);
void readData(void);
string * split(string, char);

const char FileName[] = "c:/TestAddress.txt";

int main () {
		menu();
		return 0;
} //end main
void menu(void) {
	char choice = 'S';
	while (choice!='E') {
		cout << "What would you like to do with these records: " << endl << endl;
		cout << "(A)ppend records, (S)how records, or (E)xit  "  << endl;				
		cin >> choice;
		cin.ignore(1, '\n');

		if (choice=='A' || 'a') {
				writeData();
		} else if (choice=='S' || 's') {
				readData();
		} else if (choice!='E' || 'e') {
				cout << "'" << choice << "' You have choosen to exit."  << endl;
		}else
		{
			cout << "Invalid response" << endl;
		}
	}
}
void writeData(void){
//Write the Address Info to a file
   char choice = 'Y' || 'y';
   string name = "";
   string street = "";
   string city = "";
   string state = "";
   string zipCode = 0;
   int record = 0;
   ofstream outMyStream (FileName, ios::app);
	
		do {
			cout << "\nEnter The Name: ";
			getline(cin, name);
			cout << "\nEnter The Street: ";
			getline(cin, street);
			cout << "\nEnter The City: ";
			getline(cin, city);
			cout << "\nEnter The State: ";
			getline(cin, state);
			cout << "\nEnter The Code: ";
			cin >> zipCode;

			//write to the file
			outMyStream << name << "," << street << "," << city << "," << state << "," << zipCode;
			record++;
			cout << "\nEnter another Record? (Y/N) "; 
			cin >> choice;
			//discard any newline character in keyboard buffer
			cin.ignore(1, '\n');
   }
	while (choice == 'Y');
		outMyStream.close();  
}//end write data

void readData(void){
//read data from a file
   ifstream inMyStream (FileName);

   //read the first record
   string lineBuffer;


   while (!inMyStream.eof() ){
	  getline (inMyStream, lineBuffer, '\n');
	  //divide the line into seperate fields
	  string *theFields = split(lineBuffer, ',');
	  cout << "Record #: " << record << endl;
	  cout << "Name...... " << theFields[0] << endl;
	  cout << "Street.... " << theFields[1] << endl;
	  cout << "City...... " << theFields[2] << endl;
	  cout << "State..... " << theFields[3] << endl;
	  cout << "Zip code.. " << theFields[4] << endl;
}

inMyStream.close();
}//end read data

string * split(string theLine, char theDeliminator){
		//Break the line into fields and save the fields to an array.
		//Each field will occupy one element in a character array.
		//theLine is a string with fields separated with theDeliminator character.
		//Assumes the last field in terminated with a newline.
		//Useage: string *theFields = split(lineBuffer, ',');
		//determine how many splits there will be so we can size our array

		int splitCount = 0;
		for(unsigned int i = 0; i < theLine.size(); i++){
				if (theLine[i] == ',')
				splitCount++;
		}
		splitCount++; 
	   //add one more to the count because there is not an ending comma

		//create an array to hold the fields
		string* theFieldArray;
		theFieldArray = new string[splitCount];

		//split the string into seperate fields
		string theField = "";
		int commaCount = 0;

			  for( unsigned int i = 0; i < theLine.size(); i++ ){
			  //read each character and look the deliminator
			   if (theLine[i] != theDeliminator) {
					   theField += theLine[i]; //build the field
			   }
			  else { //the deliminator was hit so save to the field to the array
					   theFieldArray[commaCount] = theField;
					   //save the field to the array
					   theField = "";
					   commaCount++;
			  }
		}
		theFieldArray[commaCount] = theField; 
		 //the last field is not marked with a comma...

		return theFieldArray;
} //end split 


it comes up with this error: cpp(85) : error C2065: 'record' : undeclared identifier
but I have already declared record in my code. I cannot figure out how to fix this and I should have to redeclare "record "again.
Thanks for the helpin advance


Looks like you're facing problems with scope. Record was declared inside a functions, so you can't access it from the other one. Either make record global, or find another way :P

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

void menu(void);
void writeData(void);
void readData(void);
string * split(string, char);

const char FileName[] = "c:/TestAddress.txt";
int record = 0;

int main () {
        menu();
        return 0;
} //end main
void menu(void) {
    char choice = 'S';
    while (choice!='E') {
        cout << "What would you like to do with these records: " << endl << endl;
        cout << "(A)ppend records, (S)how records, or (E)xit  "  << endl;
        cin >> choice;
        cin.ignore(1, '\n');

        if (choice=='A' || 'a') {
                writeData();
        } else if (choice=='S' || 's') {
                readData();
        } else if (choice!='E' || 'e') {
                cout << "'" << choice << "' You have choosen to exit."  << endl;
        }else
        {
            cout << "Invalid response" << endl;
        }
    }
}
void writeData(void){
//Write the Address Info to a file
   char choice = 'Y' || 'y';
   string name = "";
   string street = "";
   string city = "";
   string state = "";
   string zipCode = 0;
   ofstream outMyStream (FileName, ios::app);

        do {
            cout << "\nEnter The Name: ";
            getline(cin, name);
            cout << "\nEnter The Street: ";
            getline(cin, street);
            cout << "\nEnter The City: ";
            getline(cin, city);
            cout << "\nEnter The State: ";
            getline(cin, state);
            cout << "\nEnter The Code: ";
            cin >> zipCode;

            //write to the file
            outMyStream << name << "," << street << "," << city << "," << state << "," << zipCode;
            record++;
            cout << "\nEnter another Record? (Y/N) ";
            cin >> choice;
            //discard any newline character in keyboard buffer
            cin.ignore(1, '\n');
   }
    while (choice == 'Y');
        outMyStream.close();
}//end write data

void readData(void){
//read data from a file
   ifstream inMyStream (FileName);

   //read the first record
   string lineBuffer;


   while (!inMyStream.eof() ){
      getline (inMyStream, lineBuffer, '\n');
      //divide the line into seperate fields
      string *theFields = split(lineBuffer, ',');
      cout << "Record #: " << record << endl;
      cout << "Name...... " << theFields[0] << endl;
      cout << "Street.... " << theFields[1] << endl;
      cout << "City...... " << theFields[2] << endl;
      cout << "State..... " << theFields[3] << endl;
      cout << "Zip code.. " << theFields[4] << endl;
}

inMyStream.close();
}//end read data

string * split(string theLine, char theDeliminator){
        //Break the line into fields and save the fields to an array.
        //Each field will occupy one element in a character array.
        //theLine is a string with fields separated with theDeliminator character.
        //Assumes the last field in terminated with a newline.
        //Useage: string *theFields = split(lineBuffer, ',');
        //determine how many splits there will be so we can size our array

        int splitCount = 0;
        for(unsigned int i = 0; i < theLine.size(); i++){
                if (theLine[i] == ',')
                splitCount++;
        }
        splitCount++;
       //add one more to the count because there is not an ending comma

        //create an array to hold the fields
        string* theFieldArray;
        theFieldArray = new string[splitCount];

        //split the string into seperate fields
        string theField = "";
        int commaCount = 0;

              for( unsigned int i = 0; i < theLine.size(); i++ ){
              //read each character and look the deliminator
               if (theLine[i] != theDeliminator) {
                       theField += theLine[i]; //build the field
               }
              else { //the deliminator was hit so save to the field to the array
                       theFieldArray[commaCount] = theField;
                       //save the field to the array
                       theField = "";
                       commaCount++;
              }
        }
        theFieldArray[commaCount] = theField;
         //the last field is not marked with a comma...

        return theFieldArray;
} //end split


Was This Post Helpful? 0
  • +
  • -

#4 Cbick  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 28
  • Joined: 08-August 09

Re: Error undeclared identifier

Posted 23 August 2009 - 10:57 AM

Wow I'm such a noob sometimes. I got it to compile but now when I run my code it comes up with invalid null pointer.
Was This Post Helpful? 0
  • +
  • -

#5 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3124
  • View blog
  • Posts: 19,168
  • Joined: 14-September 07

Re: Error undeclared identifier

Posted 23 August 2009 - 10:59 AM

Updated code listing would be helpful.
Was This Post Helpful? 0
  • +
  • -

#6 Cbick  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 28
  • Joined: 08-August 09

Re: Error undeclared identifier

Posted 23 August 2009 - 11:03 AM

View PostKYA, on 23 Aug, 2009 - 09:59 AM, said:

Updated code listing would be helpful.

Here is my updated code
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

void menu(void);
void writeData(void);
void readData(void);
string * split(string, char);

const char FileName[] = "c:/TestAddress.txt";

int main () {
		menu();
		return 0;
} //end main
void menu(void) {
	char choice = 'S';
	while (choice!='E') {
		cout << "What would you like to do with these records: " << endl << endl;
		cout << "(A)ppend records, (S)how records, or (E)xit  "  << endl;				
		cin >> choice;
		cin.ignore(1, '\n');

		if (choice=='A' || 'a') {
				writeData();
		} else if (choice=='S' || 's') {
				readData();
		} else if (choice!='E' || 'e') {
				cout << "'" << choice << "' You have choosen to exit."  << endl;
		}else
		{
			cout << "Invalid response" << endl;
		}
	}
}
void writeData(void){
//Write the Address Info to a file
   char choice = 'Y' || 'y';
   string name = "";
   string street = "";
   string city = "";
   string state = "";
   string zipCode = 0;
   int record = 0;
   ofstream outMyStream (FileName, ios::app);
	
		do {
			cout << "\nEnter The Name: ";
			getline(cin, name);
			cout << "\nEnter The Street: ";
			getline(cin, street);
			cout << "\nEnter The City: ";
			getline(cin, city);
			cout << "\nEnter The State: ";
			getline(cin, state);
			cout << "\nEnter The Code: ";
			cin >> zipCode;

			//write to the file
			outMyStream << name << "," << street << "," << city << "," << state << "," << zipCode;
			record++;
			cout << "\nEnter another Record? (Y/N) "; 
			cin >> choice;
			//discard any newline character in keyboard buffer
			cin.ignore(1, '\n');
   }
	while (choice == 'Y');
		outMyStream.close();  
}//end write data

void readData(void){
//read data from a file
   ifstream inMyStream (FileName);

   //read the first record
   string lineBuffer;


   while (!inMyStream.eof() ){
	  getline (inMyStream, lineBuffer, '\n');
	  //divide the line into seperate fields
	  string *theFields = split(lineBuffer, ',');
	  int record = 0;
	  cout << "Record #: " << record << endl;
	  cout << "Name...... " << theFields[0] << endl;
	  cout << "Street.... " << theFields[1] << endl;
	  cout << "City...... " << theFields[2] << endl;
	  cout << "State..... " << theFields[3] << endl;
	  cout << "Zip code.. " << theFields[4] << endl;
}

inMyStream.close();
}//end read data

string * split(string theLine, char theDeliminator){
		//Break the line into fields and save the fields to an array.
		//Each field will occupy one element in a character array.
		//theLine is a string with fields separated with theDeliminator character.
		//Assumes the last field in terminated with a newline.
		//Useage: string *theFields = split(lineBuffer, ',');
		//determine how many splits there will be so we can size our array

		int splitCount = 0;
		for(unsigned int i = 0; i < theLine.size(); i++){
				if (theLine[i] == ',')
				splitCount++;
		}
		splitCount++; 
	   //add one more to the count because there is not an ending comma

		//create an array to hold the fields
		string* theFieldArray;
		theFieldArray = new string[splitCount];

		//split the string into seperate fields
		string theField = "";
		int commaCount = 0;

			  for( unsigned int i = 0; i < theLine.size(); i++ ){
			  //read each character and look the deliminator
			   if (theLine[i] != theDeliminator) {
					   theField += theLine[i]; //build the field
			   }
			  else { //the deliminator was hit so save to the field to the array
					   theFieldArray[commaCount] = theField;
					   //save the field to the array
					   theField = "";
					   commaCount++;
			  }
		}
		theFieldArray[commaCount] = theField; 
		 //the last field is not marked with a comma...

		return theFieldArray;
} //end split 


When I compile and run this is when I get the invalid null pointer error.
Was This Post Helpful? 0
  • +
  • -

#7 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3124
  • View blog
  • Posts: 19,168
  • Joined: 14-September 07

Re: Error undeclared identifier

Posted 23 August 2009 - 11:05 AM

Ignoring everything else for a moment:

char choice = 'Y' || 'y';



is incorrect. In fact, that shouldn't let you compile. It must be one or the other, cannot be assigned OR both.

What line is the null pointer on?
Was This Post Helpful? 0
  • +
  • -

#8 Cbick  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 28
  • Joined: 08-August 09

Re: Error undeclared identifier

Posted 23 August 2009 - 11:09 AM

View PostKYA, on 23 Aug, 2009 - 10:05 AM, said:

Ignoring everything else for a moment:

char choice = 'Y' || 'y';



is incorrect. In fact, that shouldn't let you compile. It must be one or the other, cannot be assigned OR both.

What line is the null pointer on?

The error is on line 1094 And I guess I should drop the 'y' or could I use toupper somehow?
Was This Post Helpful? 0
  • +
  • -

#9 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3124
  • View blog
  • Posts: 19,168
  • Joined: 14-September 07

Re: Error undeclared identifier

Posted 23 August 2009 - 11:29 AM

Copy paste the line please and the exact error. There isn't over a thousand lines of code in the above box.

and I would use toupper().

This post has been edited by KYA: 23 August 2009 - 11:29 AM

Was This Post Helpful? 0
  • +
  • -

#10 Cbick  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 28
  • Joined: 08-August 09

Re: Error undeclared identifier

Posted 23 August 2009 - 11:33 AM

View PostKYA, on 23 Aug, 2009 - 10:29 AM, said:

Copy paste the line please and the exact error. There isn't over a thousand lines of code in the above box.

and I would use toupper().

I cannot past the exact line because this comes up after it compiles and the cmd prompt opens. Then When I select A for append this is when it gives me this error and closees my program
How can I post a screenshot it won't let me that way I can show you the error that comes up

This post has been edited by Cbick: 23 August 2009 - 11:35 AM

Was This Post Helpful? 0
  • +
  • -

#11 Cbick  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 28
  • Joined: 08-August 09

Re: Error undeclared identifier

Posted 23 August 2009 - 11:40 AM

Ill just type out what comes up
Debug Assertion Failed
Program......
project.exe
File: f:\dd\vctools\crt_bld\x86\crt\src\xstring
Line: 1094
Expression: invalid null pointer
Was This Post Helpful? 0
  • +
  • -

#12 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3124
  • View blog
  • Posts: 19,168
  • Joined: 14-September 07

Re: Error undeclared identifier

Posted 23 August 2009 - 12:02 PM

You're setting the string zipCode to zero for some reason. The string doesn't like that, hence the null pointer error.


edit: Also, for file systems in Windows you need to use backslash and escape it:

"C:\\TestAddress.txt";


This post has been edited by KYA: 23 August 2009 - 12:02 PM

Was This Post Helpful? 0
  • +
  • -

#13 Cbick  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 28
  • Joined: 08-August 09

Re: Error undeclared identifier

Posted 23 August 2009 - 12:07 PM

View PostKYA, on 23 Aug, 2009 - 11:02 AM, said:

You're setting the string zipCode to zero for some reason. The string doesn't like that, hence the null pointer error.


edit: Also, for file systems in Windows you need to use backslash and escape it:

"C:\\TestAddress.txt";


Thanks KYA That was the problem. Something so little can throw everthing out of wack. Now I will continue testing the program.
Was This Post Helpful? 0
  • +
  • -

#14 Cbick  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 28
  • Joined: 08-August 09

Re: Error undeclared identifier

Posted 23 August 2009 - 12:13 PM

Now my program will not break from the Append function. When I enter the records and then enter N It still wants me to enter another record. Its like it is not breaking from Append function?

This post has been edited by Cbick: 23 August 2009 - 12:17 PM

Was This Post Helpful? 0
  • +
  • -

#15 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3124
  • View blog
  • Posts: 19,168
  • Joined: 14-September 07

Re: Error undeclared identifier

Posted 23 August 2009 - 12:16 PM

What does that loop look like? I did some altering and it exits on any non 'Y' character:

   char choice;  //did not set this equal to anything initially 
   string name ="";
   string street ="";
   string city ="";
   string state ="";
   string zipCode ="";
   int record = 0;
   ofstream outMyStream (FileName, ios::app);
   
		do {
		   //code omitted
			cout << "\nEnter another Record? (Y/N) ";
			cin >> choice;
			//discard any newline character in keyboard buffer
			cin.ignore(1, '\n');
	  }while (choice == 'Y');


This post has been edited by KYA: 23 August 2009 - 12:16 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2