8 Replies - 1429 Views - Last Post: 19 October 2010 - 05:36 PM Rate Topic: -----

#1 C0DED_STRYKER  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 13-August 10

quit function not working

Posted 18 October 2010 - 03:24 PM

so in my program in one of the sections it lets you add records like your name and sales and press x to exit. But my problem is that when you press X or x it doesn't return you to the main but ask you for another name. what do i need to add to fix this problem.
//v5.5
#include <iostream>
#include <fstream>
#include <string>

using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::ifstream;
using std::ofstream;
using std::ios;

int displayMenu();
void addrecords();
void displayTotal();


int main()
{
	int menuchoice = 0;

	cout << "*****Welcome to Flowers Express*****";
	
	menuchoice = displayMenu();
	while (menuchoice != 3)
	{
	if (menuchoice == 1)
		addrecords();
		
	else if (menuchoice == 2)
		displayTotal();
	else 
		cout << "\nInvalid menu choice" << endl;
	
	}
	return 0;
system("PAUSE");
}

void addrecords()
{
	string name = "";
	int sales = 0;

	ofstream outfile;

	outfile.open("C:/Users/C0DED_STRYKER/Documents/sales.txt",ios::app);

	if (outfile.is_open())
	{
		cout << "\nEnter name (X to stop): ";
		getline(cin, name);

		while(name != "X" && name != "x")
		{
			cout << "\nEnter sales: ";
			cin >> sales;
			cin.ignore (100, '\n');
			outfile << name << "#" << sales << endl;
			cout << "\nEnter name (X to stop): ";
			getline(cin, name);
		}

		outfile.close();
	}
	else
		cout << "\nFile could not open." << endl;
}

int displayMenu()
{
	int choice = 0;
	cout << "Options" << endl;  
	cout << "1 Add Records"<< endl;
	cout << "2 Display Total Sales" << endl;
	cout << "3 Exit Program" << endl;
	cout << "Enter menu option: ";
	cin >> choice;
	cin.ignore(100, '\n');
	return choice;
}
void displayTotal()
{
	ifstream inFile;

	string name = "";
	int sales = 0;
	 int total = 0;

	
	
    inFile.open("sale.txt", ios::in);

	 if (inFile.is_open())
	 {
          getline(inFile, name, '#');                 
		  inFile >> sales;
		  inFile.ignore();
		 
		 while (!inFile.eof())
		 {	
			 total = total + sales;
			 getline(inFile, name, '#');
              inFile >> sales;
             inFile.ignore();
			
			
		 }

		 inFile.close();

		 cout << endl << "Total sales $" << total << endl << endl;
		
	 }
	 else
		 cout << "\nCould not open file." << endl;

}




Is This A Good Question/Topic? 0
  • +

Replies To: quit function not working

#2 elgose  Icon User is offline

  • D.I.C Head

Reputation: 102
  • View blog
  • Posts: 228
  • Joined: 03-December 09

Re: quit function not working

Posted 18 October 2010 - 03:33 PM

Look at what happens after you exit the addrecords function. Does your menu choice change? Where does the main function bring you back to in the loop?
Was This Post Helpful? 0
  • +
  • -

#3 C0DED_STRYKER  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 13-August 10

Re: quit function not working

Posted 18 October 2010 - 06:34 PM

View Postelgose, on 18 October 2010 - 02:33 PM, said:

Look at what happens after you exit the addrecords function. Does your menu choice change? Where does the main function bring you back to in the loop?


if I understand your question if I go to the display total sales menue without adding records I get an infinite loop saying file could not be found.
Was This Post Helpful? 0
  • +
  • -

#4 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: quit function not working

Posted 18 October 2010 - 06:40 PM

Did you notice that your file names are different?

One is 'sales.txt' and the other is 'sale.txt'

Another thing. You are not getting new input within the loop.

menuchoice = displayMenu();
while (menuchoice != 3)
{
  if (menuchoice == 1)
	addrecords();
		
  else if (menuchoice == 2)
	displayTotal();
  else 
	cout << "\nInvalid menu choice" << endl;
	
}


Use a do-while loop and get input in the beginning.
do
{
	menuchoice = displayMenu();
	if (menuchoice == 1)
		addrecords();
		
	else if (menuchoice == 2)
		displayTotal();
	else 
		cout << "\nInvalid menu choice" << endl;
	
}while(menuchoice != 3);

This post has been edited by eker676: 18 October 2010 - 06:44 PM

Was This Post Helpful? 1
  • +
  • -

#5 sunion  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 92
  • Joined: 24-August 08

Re: quit function not working

Posted 18 October 2010 - 06:56 PM

You use "x" and "X".
" < is for a string, so you should use single quote '
Was This Post Helpful? 0
  • +
  • -

#6 elgose  Icon User is offline

  • D.I.C Head

Reputation: 102
  • View blog
  • Posts: 228
  • Joined: 03-December 09

Re: quit function not working

Posted 18 October 2010 - 07:10 PM

View PostC0DED_STRYKER, on 18 October 2010 - 05:34 PM, said:

View Postelgose, on 18 October 2010 - 02:33 PM, said:

Look at what happens after you exit the addrecords function. Does your menu choice change? Where does the main function bring you back to in the loop?


if I understand your question if I go to the display total sales menue without adding records I get an infinite loop saying file could not be found.

That's part of it. As eker676 has shown, it's an infinite loop no matter what, which is why whenever you press "x" or "X" it brings you back to the beginning. You successfully exit the addrecord function, but main loops you right back into it.

Another suggestion I'd like to include: other than the incorrect names eker676 pointed out, try to make all of your functions and variables follow the same naming convention. i.e. displayMenu and displayTotal have the first letter of the second word capitalized, yet instead of addRecord you have addrecord. You have an fstream inFile at one point, but a string menuchoice instead of menuChoice. If you get into the habit of consistently using the same scheme, you'll save yourself from big headaches later on.

View Postsunion, on 18 October 2010 - 05:56 PM, said:

You use "x" and "X".
" < is for a string, so you should use single quote '

While I agree a char is probably better suited for that loop, the variable name is a string, so I think "x" and "X" is correct in while(name != "X" && name != "x").

Edit: You may also want to look into case-switch for your menu options :D

This post has been edited by elgose: 18 October 2010 - 07:12 PM

Was This Post Helpful? 0
  • +
  • -

#7 elgose  Icon User is offline

  • D.I.C Head

Reputation: 102
  • View blog
  • Posts: 228
  • Joined: 03-December 09

Re: quit function not working

Posted 18 October 2010 - 07:26 PM

Something else I just noticed, the last few lines of your main function:
    return 0;
system("PAUSE");
}
Just so you know, the return 0; gives control back to the computer and anything after that will not run. So, I could have:
int main()
{
     return 0;
     destroyTheWorld();
}
..and you can rest assured the world will still be here. You're also bound to hear someone tell you to avoid system("PAUSE");, and I'd pay attention to them. Use something friendlier like cin.get(); like so:
int main()
{
//...
     cout << "Press return to continue...";
     cin.get();
     return 0;
}

This post has been edited by elgose: 18 October 2010 - 07:27 PM

Was This Post Helpful? 1
  • +
  • -

#8 C0DED_STRYKER  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 13-August 10

Re: quit function not working

Posted 19 October 2010 - 05:18 PM

Okay So I made my changes and the only problem I have Now is that the sales total needs to add up to $15150 after you enter everyone's name and sales (john 3000, carol 2000, sean 1000, drew 4500, jake 4650) the problem is that it wont add up jake's sales so I end up with 10500 instead.

sales.txt:
John Hammil#3000
Carol Wroberg#2000
Sean Nunez#1000
Drew Merriweather#4500
Jake Treadle#4650

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

using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::ifstream;
using std::ofstream;
using std::ios;

int displayMenu();
void addrecords();
void displayTotal();


int main()
{
	int menuchoice = 0;

	cout << "*****Welcome to Flowers Express*****";

	menuchoice = displayMenu();
	while (menuchoice != 3)
	{
	if (menuchoice == 1)
		addrecords();
		
	else if (menuchoice == 2)
		displayTotal();
	else 
		cout << "Invalid menu choice" << endl;
	menuchoice = displayMenu();
	}
	
	return 0;

}
int displayMenu()
{
	int choice = 0;
	cout << "\nOptions\n" << "1 Add Records\n" << "2 Display Total Sales\n" << "3 Exit Program\n";
	cout << "Enter menu option: ";
	cin >> choice;
	cin.ignore(100, '\n');
	return choice;
}

void addrecords()
{
	string name = "";
	int sales = 0;

	ofstream outfile;

	outfile.open("C:/Users/C0DED_STRYKER/Documents/sales.txt",ios::app);

	if (outfile.is_open())
	{
		cout << "Enter name (X to stop): ";
		getline(cin, name);
		while(name != "X" && name != "x")
		{
			cout << "Enter sales: ";
			cin >> sales;
			cin.ignore (100, '\n');
			cout << "Enter name (X to stop): ";
		getline(cin, name);
		}

		outfile.close();
	}
	else
		cout << "File could not open." << endl;
}



void displayTotal()
{

	string name = "";
	int sales = 0;
	 int total = 0;

	
	ifstream inFile;
    inFile.open("C:/Users/C0DED_STRYKER/Documents/sales.txt", ios::in);

	 if (inFile.is_open())
	 {
          getline(inFile, name, '#');                 
		  inFile >> sales;
		  inFile.ignore();
		 
		 while (!inFile.eof())
		 {	
			 total = total + sales;
			 getline(inFile, name, '#');
              inFile >> sales;
             inFile.ignore();
			
			
		 }

		 inFile.close();

		 cout << endl << "Total sales $" << total << endl << endl;
		
	 }
	 else
		 cout << "Could not open file." << endl;

}



Was This Post Helpful? 0
  • +
  • -

#9 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: quit function not working

Posted 19 October 2010 - 05:36 PM

Not sure if this relates to your problem (because your bug description is light on for useful facts) but it is worth knowing that EOF is a problematic tool.
Read more here
http://www.drpaulcar...-errors.php#4.2
and here
http://www.dreaminco...g-text-files-c/
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1