//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;
}
quit function not working
Page 1 of 18 Replies - 1214 Views - Last Post: 19 October 2010 - 05:36 PM
#1
quit function not working
Posted 18 October 2010 - 03:24 PM
Replies To: quit function not working
#2
Re: quit function not working
Posted 18 October 2010 - 03:33 PM
#3
Re: quit function not working
Posted 18 October 2010 - 06:34 PM
elgose, on 18 October 2010 - 02:33 PM, said:
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.
#4
Re: quit function not working
Posted 18 October 2010 - 06:40 PM
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
#5
Re: quit function not working
Posted 18 October 2010 - 06:56 PM
" < is for a string, so you should use single quote '
#6
Re: quit function not working
Posted 18 October 2010 - 07:10 PM
C0DED_STRYKER, on 18 October 2010 - 05:34 PM, said:
elgose, on 18 October 2010 - 02:33 PM, said:
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.
sunion, on 18 October 2010 - 05:56 PM, said:
" < 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
This post has been edited by elgose: 18 October 2010 - 07:12 PM
#7
Re: quit function not working
Posted 18 October 2010 - 07:26 PM
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
#8
Re: quit function not working
Posted 19 October 2010 - 05:18 PM
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;
}
#9
Re: quit function not working
Posted 19 October 2010 - 05:36 PM
Read more here
http://www.drpaulcar...-errors.php#4.2
and here
http://www.dreaminco...g-text-files-c/
|
|

New Topic/Question
Reply




MultiQuote






|