hey everyone, I got a weird error when I run my program. I compile and link and run the program through the terminal in Ubuntu. I have to compile or linking errors, and the program run's fine, except when the program asks:

"Would you like to see a display of the output (Y/N):"

In the code itself the function takes the y or n and uses statements to either display the output or display a default message. When I type Y I do not get the output but rather the default message that I should get when I type N (or anything other than Y but thats besides the point for my purposes). I can not find error in my logic, and I'm only a second year C++ student so my overall knowledge is well limited.

I have included my .cpp and the data I am using from my data file below. This is a class assignment so bear with it.

CODE:

//Assignment #6 due 2-8-2010
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
//global declarations
typedef char Name_t[15];
typedef fstream Infile_t;
typedef fstream Outfile_t;
typedef char Filename_t[50];
//*********************************************************************************************************
//classes
class cAccount
{
	private:
		int accountID;
		Name_t accountName;//(last,first)
		float accountBal;
	public:
		cAccount();
		int get_accountID(int &accountID)
		{ accountID=this->accountID; return accountID;};
		void read(Infile_t &Infile);
		void print(Outfile_t &Outfile);
		void display();
};//end of cAccount
class cAccountList
{
	private:
		static const int MAXARRAYSIZE=51;
		int counter;
		typedef cAccount AccountArray[MAXARRAYSIZE];
		AccountArray AccountList;
	public:
		cAccountList();
		void load_data(Infile_t &Infile);
		void print_to_outfile(Outfile_t &Outfile);
		void print_to_screen();
		void sort_by_ID();
		bool search();
};//end of cAccountList
//*********************************************************************************************************
//class cAccount functions
cAccount::cAccount()
{
	accountID=0;
	accountBal=0;
};//default constructor
void cAccount::read(Infile_t &Infile)
{ Infile>>accountID>>accountName>>accountBal; };
//end of cAccount::Read
void cAccount::print(Outfile_t &Outfile)
{ 
	Outfile<<" "<<accountID<<" "<<accountName<<" "<<accountBal<<endl;
	Outfile<<"_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_"<<endl;
};//end of print
void cAccount::display()
{
	cout<<" "<<accountID<<" "<<accountName<<" "<<accountBal<<endl;
	cout<<"***********************************************************"<<endl;
};//end of display
//*********************************************************************************************************
//class cAccountList functions  
cAccountList::cAccountList()
{ counter=0; };//default constructors
void cAccountList::load_data(Infile_t &Infile)
{
	Filename_t Filename;
	cout<<"Please enter the name of the datafile to be used:"<<endl;
	cin>>Filename;
	Infile.open(Filename,ios::in);
	AccountList[counter].read(Infile);
	while(!Infile.eof())
	{
		counter=counter+1;
		AccountList[counter].read(Infile);
	}//end while
	Infile.close();
	cout<<"Datafile read....SUCCESS!"<<endl;
};//end of read_data
void cAccountList::print_to_outfile(Outfile_t &Outfile)
{
	Filename_t Filename;
	cout<<"Please enter the name of the output file:"<<endl;
	cin>>Filename;
	Outfile.open(Filename,ios::out);
	Outfile<<"**************************************************************"<<endl;
	Outfile<<" "<<"ACCOUNT_ID#"<<" "<<"ACCOUNT_NAME"<<" "<<"ACCOUNT_BALANCE"<<endl;
	Outfile<<"**************************************************************"<<endl<<endl;
	for(int I=0; I<counter; I++)
	{
		AccountList[I].print(Outfile);
	}//end for
	Outfile.close();
	cout<<"Outfile write...SUCCESS!"<<endl<<endl;
};//end print_to_outfile
void cAccountList::print_to_screen()
{
	char choice[5];
	cout<<"Would you like to preview your output file? (Y/N)"<<endl;
	cin>>choice;
	if (choice == "Y")
	{
		cout<<"Generating preview of output....."<<endl<<endl;
		cout<<"**************************************************************"<<endl;
		cout<<" "<<"ACCOUNT_ID#"<<" "<<"ACCOUNT_NAME"<<" "<<"ACCOUNT_BALANCE"<<endl;
		cout<<"**************************************************************"<<endl<<endl;
		for(int I=0; I<counter; I++)
		{
			AccountList[I].display();
		}//end for
	}//end if
	else
	{ cout<<"Very well...MOVING TO NEXT PROCESS"<<endl;}
};//end print_to_screen
void cAccountList::sort_by_ID()
{
	int I,Pass;
	int accountID;
	AccountArray temp;
	for(Pass=1; Pass<counter-1; Pass++)
	{
		for(I=0; I<counter-1; I++)
		{
		if(AccountList[I].get_accountID(accountID) > AccountList[I+1].get_accountID(accountID))
		{
			temp[I]=AccountList[I];
			AccountList[I]=AccountList[I+1];
			AccountList[I+1]=temp[I];
		}//end if
		}//end inside for
	}//end outside for
};//end sort_by_ID
bool cAccountList::search()
{
	bool Found=false;
	int I=0;
	int accountID;
	int searchID=0;
	Name_t searchName;
	Name_t accountName;
	char choice[2];
	cout<<"Would you like to search for a record at this time? (Y/N)"<<endl;
	cin>>choice;
	while(choice=="Y")
	{
		cout<<"Please enter the 5 digit ID number:"<<endl;
		cin>>searchID;
		while((!Found)&&(I<counter))
		{
			if(AccountList[I].get_accountID(accountID)!=searchID)
			{ I=I+1;}//end if
			else if (AccountList[I].get_accountID(accountID)==searchID)
			{ Found=true;}//end else
		}//end interior while
		return Found;	
		cout<<"Would you like to search for another record? (Y/N)"<<endl;
		cin>>choice;
	}//end while
};//end search	
//*********************************************************************************************************
//main
int main()
{
	Infile_t Infile;
	Outfile_t Outfile;
	cAccountList ACCOUNT;
	ACCOUNT.load_data(Infile);
	ACCOUNT.sort_by_ID();
	ACCOUNT.print_to_outfile(Outfile);
	ACCOUNT.print_to_screen();
return 0;
}//end main
//*********************************************************************************************************

DATA:
31951
Walker,John
1000.98
69537
Patton,George
876.07
28542
Colt,Samuel
1234.56
28325
Bond,James
6798.00
68128
Hamilton,Alexander
9000.00
44206
Lincoln,Abe
00.01
13108
Armstrong,Louis
750.00
41727
Gotti,John
5500.60
21158
Lennon,John
6969.69
52019
McCarthy,Joseph
742.02
37823
Stewart,Jon
29.98
70109
Schrute,Dwight
00.00
74746
Bob,Billy
50.50
18211
Pitt,Brad
2056.89
19724
Blitzer,Wolf
3009.10
49926
Dillinger,John
862.43

hey everyone, I got a weird error when I run my program. I compile and link and run the program through the terminal in Ubuntu. I have to compile or linking errors, and the program run's fine, except when the program asks:

"Would you like to see a display of the output (Y/N):"

I don't see this message anywhere in the code...

Regardless, the only response that seems to work will be Y. y will not work.

cout<<"Would you like to preview your output file? (Y/N)"<<endl;
cin>>choice;
if (choice == "Y")  // only an upper case Y can be entered
{
  cout<<"Generating preview of output....."<<endl<<endl;
}//end if
else
{ cout<<"Very well...MOVING TO NEXT PROCESS"<<endl;}
};//end print_to_screen

I don't see this message anywhere in the code...

Regardless, the only response that seems to work will be Y. y will not work.

cout<<"Would you like to preview your output file? (Y/N)"<<endl;
cin>>choice;
if (choice == "Y")  // only an upper case Y can be entered
{
  cout<<"Generating preview of output....."<<endl<<endl;
}//end if
else
{ cout<<"Very well...MOVING TO NEXT PROCESS"<<endl;}
};//end print_to_screen

cout<<"Would you like to preview your output file? (Y/N)"<<endl;
This is the line I am talking about, sorry for misquoting.
But yea I tried inputing 'Y', I will try 'y' but I dont know it it will work

cout<<"Would you like to preview your output file? (Y/N)"<<endl;
This is the line I am talking about, sorry for misquoting.
But yea I tried inputing 'Y', I will try 'y' but I dont know it it will work

Wait a minute....

You've defined char choice[2]; you're inputting cin>>choice; So what is in choice at this point? Both bytes?
And is if (choice == "Y") the proper comparison -- with Y as a string?

Either make choice a single character and acquire it by cin.get(choice); (you will need a cin.ignore() after to mop up the '\n'
or use if(strcmp(choice,"Y") ==0) instead of your (choice == "Y") EDIT: D'OH WaltP beat me to it

Edited 6 Years Ago by jonsca: n/a

Either make choice a single character and acquire it by cin.get(choice); (you will need a cin.ignore() after to mop up the '\n'
or use if(strcmp(choice,"Y") ==0) instead of your (choice == "Y") EDIT: D'OH WaltP beat me to it

Or if(choice == 'Y') with choice as a single character.

Wonder why we always have to edit a post to acknowledge being beaten... :-/

Don't have to, just giving you the proper credit and verifying that I didn't lie there in wait for you to post and immediately copy what you said. Or it's a bad habit I picked up. Your choice :D

Comments
I'll take it as a bad habit :-)

thanks everyone, I changed the char size to 1 and everything seems to work fine now, thanks again for all of the quick replys!

This question has already been answered. Start a new discussion instead.