Problem with character arrays.

This is a discussion on Problem with character arrays. within the C++ Programming forums, part of the General Programming Boards category; Code: //////////////////// //Daniel Pritchett// // COP 1220 // // 11-2-02 // //////////////////// /* This program will first ask the user ...

  1. #1
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901

    Problem with character arrays.

    Code:
        ////////////////////
       //Daniel Pritchett//
      //   COP 1220     //
     //   11-2-02      //
    ////////////////////
    /*
    This program will first ask the user for the term, then it will present 3 choices
    to pick from.  Either imput the information to a file, output the information
    from the first file to a report, or quit.  This all is looped so that when the user is
    finished with one choice, they will be able to re-chose again.
    */
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <iomanip>
    
    using namespace std;
    
    void OutputInfo(ofstream & outFile1, string & SSN, string & LastName, string & FirstName, string & semester,
    				string & Email, int & choice, int & students, char cRefNumber [], 
    				char cRefNumber1[], char cRefNumber2[], char cRefNumber3[]);
    void ReportOutput(ifstream & InFile, ofstream & outFile2, string SSN, string LastName, string FirstName, 
    				  string & semester, string & Email, int choice, char cRefNumber []);
    enum Selections{OutputData = 1, OutputReport, Quit};
    
    
    int main ()
    {
    
    	string SSN, LastName, FirstName, semester, Email ;
    	int choice, students;
    	char cRefNumber[10];
    	char cRefNumber1[10] = "111111";
    	char cRefNumber2[10] = "222222";
    	char cRefNumber3[10] = "333333";
    	
    
    	ofstream outFile1;
    	ofstream outFile2;
    	ifstream inFile;
    
    	outFile1.open("a:sdntnfo.dat", ios::out);
    	inFile.open("a:sdntnfo.dat", ios::in);
    	outFile2.open("a:Record.txt", ios::out);
    
    	cout << "Please enter the Class term: ";
    	cin >> semester;
    	//heading
    	outFile2 << setw(6) << "Introduction to C"
    			<< setw(5) <<"\nTerm:" << semester;
    
    	//loops so that the user can re-chose what operation they would like to do.
    	do
    	{
    		cout << "\nWould you like to\n1: Enter Student information\n"
    		<<"2: Read from file and create report"
    		<< "\n3: Quit\n";
    	cin >> choice;
    	switch(choice)
    	{
    		//outputs the first file, student database
    	case OutputData:
    		OutputInfo(outFile1, SSN, LastName, FirstName, semester, 
    			Email, choice, students, cRefNumber, cRefNumber1, cRefNumber2, cRefNumber3);
    		break;
    
    		//generates reference number organized file
    	case OutputReport:
    		
    		ReportOutput(inFile, outFile2,  SSN,  LastName,  FirstName, 
    				   semester,  Email, choice, cRefNumber);
    	
        break;
    
    	//quits
    	case Quit:
    		exit(0);
    
    	//default, exits switch and displays switch options 
    	default:
    		cout << "\nYou did not enter a valid choise, please enter correct choice!";
    		break;
    	}
    	}
    	while(choice != 3);   //end of do...while statemet
    
    	outFile1.close();
    	inFile.close();
    	outFile2.close();
    
    	return 0;
    }
    
    void OutputInfo(ofstream & outFile1, string & SSN, string & LastName, string & FirstName, string & semester,
    				string & Email, int & choice, int & students, char cRefNumber [], 
    				char cRefNumber1[], char cRefNumber2[], char cRefNumber3[])
    {
    
    	cout << "\nHow many students are you entering?";
    		cin >> students;
    		while(students != 0)
    		{
    
    		cout << "\nEnter the student's nine digit ID number: ";
    		cin >> SSN;
    		outFile1 << SSN;
    		cout << "\nEnter the student's First and Last Name: ";
    		cin >> FirstName >> LastName;
    		outFile1 << " " << LastName << " " << FirstName;
    		cout << "\nEnter the student's e-mail@mdcc.edu: ";
    			cin >> Email;
    			outFile1 << " " << Email;
    		cout << "\nEnter the student's Reference number: "
    			 <<"(note, only 111111, 222222, and 333333 are valid.\n)";
    		cin >> cRefNumber;
    
    		outFile1 << " " << cRefNumber << endl;
    		
    		students--;
    		}
    		
    
    }
    
    
    void ReportOutput(ifstream & inFile, ofstream & outFile2, string SSN, string LastName, string FirstName, 
    				  string & semester, string & Email, int choice, char cRefNumber [])
    {	
    	 if (!inFile)
    		{
    			cerr << "File could not be opened" << endl;
    			exit(1);
    		}
    
    	   if (inFile==NULL)
    	   {
    		   cout<<"The file does not exist";
    		   exit(0);
    	   }
    
    	outFile2 << "\n\n\nSSN" << right << setw(20) << right << setw(20) << "Last"
    			<< right << setw(20) << "First  " << right << setw(25) << "E-mail@mdcc.edu" << setw(20)
    			<< "Reference Number\n";
    	
    	while (!inFile.eof())
    		{
    		inFile >> SSN >> LastName >> FirstName >> Email >> cRefNumber;
    
    		outFile2 << right << SSN << right << setw(17) << LastName << right 
    			<< setw(16) << FirstName << right << setw(25) << Email << setw(20) << 
    			cRefNumber[8]<< cRefNumber[7] << cRefNumber[6] << cRefNumber[5]<< endl;
    		}
    }
    Last edited by indigo0086; 11-20-2002 at 07:12 PM.

  2. #2
    ...
    ... is offline
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    try this:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	char SSN[10];
    	cout << "Enter your Social Security number: ";
    	cin >> SSN;
    	cout << "\nYour ssn is " << SSN;
    	cout << "\nthe last four digits of your ssn are " << SSN[8] << SSN[7] << SSN[6] << SSN[5] << endl;
    
     int x;
     cin >> x;
    	
    	return 0;
    }
    you are only putting in 9 variables in a 10 variable array. your array goes from

    SSN[0] to SSN[9], NOT SSN[1] to SSN[10]

    10 is the size of the array, but the highest element in the array is 9, since the array starts at 0 and not 1.
    I came up with a cool phrase to put down here, but i forgot it...

  3. #3
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    Oh I forgot I put 9 digits in.

  4. #4
    ...
    ... is offline
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    *nod*
    I came up with a cool phrase to put down here, but i forgot it...

  5. #5
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    so the 9th element is the null character right?

  6. #6
    ...
    ... is offline
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    yes
    I came up with a cool phrase to put down here, but i forgot it...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Character Arrays
    By Smiley27 in forum C++ Programming
    Replies: 15
    Last Post: 04-13-2004, 05:19 PM
  2. Character pointer problem.
    By kiss_psycho in forum C++ Programming
    Replies: 1
    Last Post: 02-28-2003, 02:49 AM
  3. Comparing Character Arrays
    By LostNotFound in forum C++ Programming
    Replies: 2
    Last Post: 02-23-2003, 12:42 PM
  4. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 04:23 PM
  5. Manipulation of character arrays
    By Han in forum C Programming
    Replies: 4
    Last Post: 04-17-2002, 02:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21