I have validated cin input by using std::numeric_limits and thus I need to include <limits>.
Will this effect the functionality of my Program?
I have been reading so many comments on different forums that using <limits> is not a good practice, but nobody cleared stated why it is not a good practice. If it is really a bad practice, what is a good practice to validate cin input for known errors, such as invalid inputs and long inputs?
#include <iostream>
#include <limits>
using namespace std;
void initMenu ();
void initDecision(int);
double areaCircle (double);
double areaRectangle (double, double);
const double PI = 4.14;
bool isValid (string);
int main()
{
int choice;
char cont;
do
{
system ("cls");
initMenu ();
while (!(cin >> choice))
{
cin.clear();
system ("cls");
initMenu();
cout << "---------------------------------------------------------------------------" << '\n';
cout << "Invalid Input cleared and turns to [" << cin.rdstate() << "] you can try it again or go to school" << '\n';
cout << "---------------------------------------------------------------------------" << '\n';
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
initDecision (choice);
do
{
cin.clear();
cout << "" << '\n';
cout << "------------------------------------------------" << '\n';
cout << "Do you want to continue using the Program? [Y/n]" << '\n';
cout << "------------------------------------------------" << '\n';
cin >> cont;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
while (cont != 'Y' && cont != 'y' && cont != 'N' && cont != 'n' );
system ("cls");
cout << "-------------------------------" << '\n';
cout << "Thank you for using our Program" << '\n';
cout << "-------------------------------" << '\n';
}
while (cont == 'Y' || cont == 'y');
return 0;
}
void initMenu()
{
cout << "-------------------------------------------------" << '\n';
cout << "Choose any one option from above! [1] [2] [3] [4]" << '\n';
cout << "-------------------------------------------------" << '\n';
cout << "1 Circle" << '\n';
cout << "2 Rectangle" << '\n';
}
void initDecision(int choice)
{
double r, a, b, h;
switch (choice)
{
case 1:
cout << "Enter the Radius of a Circle: " << '\n';
do {cin >> r;} while (!isValid("Wrong Input detected, Try again!"));
areaCircle(r);
break;
case 2:
cout << "Enter Base and Height of a Rectangle: " << '\n';
do {cin >> b >> h;} while (!isValid("Wrong Input detected, Try again!"));
areaRectangle(b ,h);
break;
default:
cout << "----------------------------------------------------------------------" << '\n';
cout << "The option is out of bound, Check and try again with available options" << '\n';
cout << "----------------------------------------------------------------------" << '\n';
break;
}
}
double areaCircle (double r)
{
double result = PI * r * r;
cout << "The area of a circle that has value: [" << r << "] = [" << result << "]" << '\n';
return result;
}
double areaRectangle (double b, double h)
{
double result = b * h;
cout << "Rectangle with Base value: [" << b << "] and value of Height: [" << h << "] = [" << result << "]" << '\n';
return result;
}
bool isValid (string error_msg)
{
if(cin.rdstate())
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max() , '\n');
system("cls");
initMenu();
cout << error_msg << '\n';
return false;
}
return true;
}
Related
How do you provide a condition in the if statement, if the variable is an integer data type then it will be displayed, and if the variable is any other data type then something else will be displayed?
#include <iostream>
#include <stdlib.h>
#include <windows.h>
#include <conio.h>
using namespace std;
int main() {
char pilih,pilih2;
int p, l, a, t, r,ulang = 4;
float luas1, luas2, luas3, keliling1, keliling2, keliling3, phi = 3.14;
while (ulang > 0) {
pilihUlang:
cout << "Pilih jenis bangun datar berikut :\n 1. Persegi panjang\n 2. Segitiga sama sisi\n 3. Lingkaran" << endl;
cout << "Masukan pilihan anda [1/2/3] : ";
cin >> pilih;
system("cls");
switch (pilih) {
case '1':
system("color 07");
cout << "Luas Dan Keliling Persegi Panjang" << endl;
cout << "Masukan Panjang = ";
cin >> p;
if (?) { // <-- here
cout << "Masukan Lebar = ";
cin >> l;
system("cls");
cout << "Luas dan keliling Persegi Panjang dengan panjang " << p << " dan lebar " << l << ", yaitu :" << endl;
luas1 = p * l;
keliling1 = 2 * (p + l);
cout << "Luas = " << luas1 << endl;
cout << "Keliling = " << keliling1 << endl;
cout << "Sisa bisa memilih ulang " << ulang - 1 << " kali." << endl;
break;
}
else {
//...
}
From the istream::operator>> man page:
If extraction fails (e.g. if a letter was entered where a digit is
expected), zero is written to value and failbit is set.
So, your function could test the cin.good() method to see if the >> operation was successful, like this:
cin >> p;
if (cin.good()) {
cout << "The integer you typed was " << p << endl;
} else {
cout << "Hey, that wasn't an integer!" << endl;
}
I recently developed a calculator, this is the code:
/*
*All 4 operations + percentage finder + Raise to power and more....
* by Ulisse
* ulissebenedennti#outlook.com
* Feel free to take some parts of this code an put them
* in yours, but do not take all the code and change/delete
* the comments to take the credit, trust me, it doesn't
* gives the satisfaction you expect.
*/
#include <iostream> //For cin and cout
#include <iomanip> //For setprecision()
#include <windows.h> //For SetconsoleTitle()
#include <stdlib.h> //For system()
#include <cmath> //For pow()
#include <cctype> //For isdigit()
using namespace std;
int main(){
reset:
system("cls"); //Screen cleaner
system("color 0f");
SetConsoleTitle("Calculator by Ulisse");//Setting window title
char op; //Start of the variables declaration
double a, b, ra;
string p, ms, d, me, e;
p = " + ";
ms = " - ";
d = " : ";
me = " x ";
e = " = "; //End of the variable declaration
cout << "Type now '?' for help page, or another character to continue." << endl;
cin >> op;
if (op == '?'){
help:
system("cls");
cout << "Write the whole operation.\nEXAMPLE: 2 ^ 3 \n OUTPUT: 2 ^ 3 = 8"<< endl;
cout << "(+) Sum between a and b\n(-) Subtraction between a and b" << endl;
cout << "(^) Raise to power\n(%)finds the a% of b\n(x or *)Multiplicate a by b" << endl;
cout << "(: or /) Divide a by b" << endl;
system("pause");
system("cls");
goto start;
}
else{
system("cls");
while(1){
start:
cout << "CALC> ";
cin >> a;
cin >> op;
cin >> b;
//The four operations
if (op == '+'){
cout << "RESULT" << endl;
cout << setprecision(999) << a << p << b << e << a + b << endl;
cout << "________________________________________________________________________________" << endl;
}
if (op == '-'){
cout << "RESULT" << endl;
cout << setprecision(999) << a << ms << b << e << a - b << endl;
cout << "________________________________________________________________________________" << endl;
}
if (op == '*' || op == 'x'){
cout << "RESULT" << endl;
cout << setprecision(999) << a << me << b << e << a * b << endl;
cout << "________________________________________________________________________________" << endl;
}
if (op == '/' || op == ':'){
cout << "RESULT" << endl;
cout << setprecision(999) << a << d << b << e << a / b << endl;
cout << "________________________________________________________________________________" << endl;
}
if (op == '%'){
cout << "RESULT" << endl;
cout << setprecision(999) << "The " << a << "% of " << b << " is " << b / 100 * a << endl;
cout << "________________________________________________________________________________" << endl;
}
if (op == '^'){
cout << "RESULT" << endl;
cout << setprecision(999) << a << " ^ " << b << " = " << pow (a, b) << endl;
cout << "________________________________________________________________________________" << endl;
}
//Some useful functions
if (op == 'c'){
system("cls");
}
if (op == '?'){
system("cls");
goto help;
}
if (op == 'r'){
goto reset;
}
if (op == 'b'){
system("color 0c");
Beep(400,500);
cout << "CLOSING, ARE YOU SURE?(y/n)";
system("color 0c");
cin >> op;
if(op == 'y'){
cout << "Closing..." << endl;
system("cls");
system("color 0f");
system("pause");
break;
}
if(op == 'n'){
goto start;
}
}
if (op == '<'){
if (a < b){
cout << "RESULT" << endl;
cout << setprecision(999) << a << " < " << b << e << " TRUE " << endl;
cout << "________________________________________________________________________________" << endl;
}
else{
cout << "RESULT" << endl;
cout << setprecision(999) << a << " < " << b << e << " FALSE " << endl;
cout << "________________________________________________________________________________" << endl;
}
}
if (op == '>'){
if (a > b){
cout << "RESULT" << endl;
cout << setprecision(999) << a << " > " << b << e << "TRUE" << endl;
cout << "________________________________________________________________________________" << endl;
}
else{
cout << "RESULT" << endl;
cout << setprecision(999) << a << " > " << b << e << "FALSE" << endl;
cout << "________________________________________________________________________________" << endl;
}
}
if (op == '='){
if (a == b){
cout << "RESULT" << endl;
cout << setprecision(999) << a << " = " << b << " is TRUE" << endl;
cout << "________________________________________________________________________________" << endl;
}
else{
cout << "RESULT" << endl;
cout << setprecision(999) << a << " = " << b << " is FALSE" << endl;
cout << "________________________________________________________________________________" << endl;
}
}
}
}
}
This is how it works:
You write a number, then an operator(like +, - plus other functions...) and it makes the operation between the two numbers you typed depending o what is the typed operato, so if you type 4 + 3 it will output 4 + 3 = 7.
Now that you understand how it works, let us go to the qyestion...
Is there an indentifier for a number or a character? When you type a sttring or a character when you cin >> (not a number variable) the application will start printing out characters that you did never inserted:
Input
I think like this(console output) will be printed out(until you dont close the process.
So i would like to prevent the applicatin for failing when you type an invalid input for the variable and making it executes another instruction, here's what i mean:
if(anumbervariable != number || anumbervariable == string){
cout << "invalid input" << endl;
}
This isn't a real working code, it's just a representation of what i mean, or i wouldn't came hre to make you waste you lives :)
Thanks in advance.
You can do something like follows
int getNumber(){
int x;
cin >> x;
while(cin.fail()){
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
cout << "invalid input"<<endl;
cin >> x;
}
return x;
}
If you want to do a character by character thing, C++ has an isalpha() function, so you can use !isalpha(). The numeric limits thing is the max buffer that can be taken before a new line. If you print it out, it's just some large number so that it can ignore that amount of input.
So, I have several fields to fill in with numerals. And if I try to fill in the input with letters (ex. 'noway' or 'gg1337' - it makes and error and asks for a valid number (without letters for ex. '13' or '1500000').
BUT there is one problem, if I start to fill the input with numbers and then I add some letters (for ex. '12nowshithappens'), this jumps to the next field of input, thinking it is a valid number, but showing an error in the next input field.
Here is the function code:
int appled()
{
cin >> appleds;
while(cin.fail())
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
cout << "An arror occured!\nPlease, enter a valid number: ";
cin >> appleds;
}
return appleds;
}
If I described something wrong - here is the full code of my test program :)
// exZerry presents
#include <iostream>
#include <conio.h>
int apples;
int fruit;
int oranges;
int x;
int appleds;
int orangeds;
using std::cout;
using std::cin;
using std::endl;
using std::numeric_limits;
using std::streamsize;
char newline = '\n';
bool ok;
bool ok2;
bool ok3;
int appled() //Function to receive 'apples' input
{
cin >> appleds;
while(cin.fail())
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
cout << "An arror occured!\nPlease, enter a valid number: ";
cin >> appleds;
}
return appleds;
}
int oranged() //Function to receive 'oranges' input
{
cin >> orangeds;
while(cin.fail())
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
cout << "An arror occured!\nPlease, enter a valid number: ";
cin >> orangeds;
}
return orangeds;
}
int main()
{
ok = ok2 = ok3 = false; //Some testing
while(!ok2) //Actual program loop
{
x = 0; // Dropping program restart.
//cout << "-----------------------" << endl;
//cout << "DEBUG MODE: " << x << endl;
//cout << "-----------------------" << endl;
cout << "=====================================================" << endl;
cout << "Enter apples: ";
apples = appled();
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n'); //Now we have apples
cout << "Enter oranges: ";
apples = oranged();
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n'); //And now we have oranges
cout << "\n=====================================================" << endl;
cout << "Calculating..." << endl;
cout << "=====================================================" << endl;
fruit = apples + oranges;
cout << "In total we have " << fruit << " fruit" << endl;
cout << "=====================================================" << endl;
cout << newline;
//Option to go out or continue the loop
//If you enter 1 - resets the program, any other char - exit
cout << "Go out? (1 - 'No', Others - 'Yeap'):" << " ";
cin >> x;
cout << newline;
// ok2 = true;
if (x == 1)
{
cout << "Continue the program..." << endl;
//cout << "-----------------------" << endl;
//cout << "DEBUG MODE: " << x << endl;
//cout << "-----------------------" << endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
ok = false;
ok3 = false;
continue;
}
if (x == 0)
{
cout << "Shutting down the app..." << x << endl;
//cout << "-----------------------" << endl;
//cout << "DEBUG MODE: " << x << endl;
//cout << "-----------------------" << endl;
break;
}
else
{
cout << "Shutting down the app..." << x << endl;
//cout << "-----------------------" << endl;
//cout << "DEBUG MODE: " << x << endl;
//cout << "-----------------------" << endl;
break;
}
}
cout << "Press any key to exit :D" << endl;
getch();
return 0;
}
You can make your own facet that parses character sequence like that add invalid. Like this:
class num_get : public std::num_get<char>
{
public:
iter_type do_get( iter_type it, iter_type end, std::ios_base& str,
std::ios_base::iostate& err, long& v) const
{
auto& ctype = std::use_facet<std::ctype<char>>(str.getloc());
it = std::num_get<char>::do_get(it, end, str, err, v);
if (it != end && !(err & std::ios_base::failbit)
&& ctype.is(ctype.alpha, *it))
err |= std::ios_base::failbit;
return it;
}
};
Now you can install it into your stream:
std::locale orig(std::cin.getloc());
std::cin.imbue(std::locale(orig, new num_get));
while (!(std::cin >> appleds)) {
// input was not entirely numeric
}
// input was entirely numeric
So I tried and it worked, thanks, Batmaaaan :D
Adding full working code. What's it doing? Simple: When you wish to count something and you obviously do not want to count letters in your program, this might help you to do it, while styding C++ or whatever. Some basics, I guess, for everyone =)
Made and tested in Visual Studio 2012. Took some time to gather every peace of code in one place.
// exZerry presents
// Apples & Oranges V2.1
#include <iostream>
#include <conio.h>
int apples;
int juce;
int oranges;
int x;
int appleds;
int orangeds;
using std::cout;
using std::cin;
using std::endl;
using std::numeric_limits;
using std::streamsize;
using std::locale;
char newline = '\n';
bool ok;
bool ok2;
bool ok3;
class num_get : public std::num_get<char>
{
public:
iter_type do_get( iter_type it, iter_type end, std::ios_base& str,
std::ios_base::iostate& err, long& v) const
{
auto& ctype = std::use_facet<std::ctype<char>>(str.getloc());
it = std::num_get<char>::do_get(it, end, str, err, v);
if (it != end && !(err & std::ios_base::failbit)
&& ctype.is(ctype.alpha, *it))
err |= std::ios_base::failbit;
return it;
}
};
/*
int appled()
{
cin >> appleds;
while(cin.fail())
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
cout << "An arror occured!\nPlease, enter a valid number: ";
cin >> appleds;
}
return appleds;
}
int oranged()
{
cin >> orangeds;
while(cin.fail())
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
cout << "An arror occured!\nPlease, enter a valid number: ";
cin >> orangeds;
}
return orangeds;
}
*/
int main()
{
cout << "=====================================================" << endl;
cout << "Welcome to exZerry's 'Apples & Oranges V2'" << endl;
cout << "=====================================================" << endl;
cout << newline;
cout << newline;
ok = ok2 = ok3 = false;
while(!ok2)
{
x = 0;
//cout << "-----------------------" << endl;
//cout << "DEBUG MODE: " << x << endl;
//cout << "-----------------------" << endl;
cout << "=====================================================" << endl;
cout << "Enter apples: ";
// apples = appled();
locale orig(cin.getloc());
cin.imbue(locale(orig, new num_get));
while (!(cin >> apples)) {
cout << "An arror occured!\nPlease, enter a valid number: ";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
}
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
cout << "Enter oranges: ";
// oranges = oranged();
//std::locale orig(std::cin.getloc());
cin.imbue(locale(orig, new num_get));
while (!(cin >> oranges)) {
cout << "An arror occured!\nPlease, enter a valid number: ";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
}
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
cout << "\n=====================================================" << endl;
cout << "Calculating..." << endl;
cout << "=====================================================" << endl;
juce = apples + oranges;
cout << "In total we have " << juce << " fruit" << endl;
cout << "=====================================================" << endl;
cout << newline;
cout << "Go out? (1 - 'No', Others - 'Yeap'):" << " ";
cin >> x;
cout << newline;
// ok2 = true;
if (x == 1)
{
cout << "Continue the program..." << endl;
//cout << "-----------------------" << endl;
//cout << "DEBUG MODE: " << x << endl;
//cout << "-----------------------" << endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
ok = false;
ok3 = false;
continue;
}
if (x == 0)
{
cout << "Shutting down the app..." << x << endl;
//cout << "-----------------------" << endl;
//cout << "DEBUG MODE: " << x << endl;
//cout << "-----------------------" << endl;
break;
}
else
{
cout << "Shutting down the app..." << x << endl;
//cout << "-----------------------" << endl;
//cout << "DEBUG MODE: " << x << endl;
//cout << "-----------------------" << endl;
break;
}
}
cout << "Press any key to exit :D" << endl;
getch();
return 0;
}
I'm solving some C++ problems from ebooks. I made this C++ program but it isn't working properly. I've 2 problems:
Even after applying the forumla (votePercentage = firstAnswer/totalVotes*100;) it isn't showing the output, but only 0.
The program should display the bar chart, how am I suppose to do that? Any hints, reference or solution will be appreciated.
Here is my code:
/*
* Write a program that provides the option of tallying up the
* results of a poll with 3 possible values.
* The first input to the program is the poll question;
* the next three inputs are the possible answers.
* The first answer is indicated by 1, the second by 2, the third by 3.
* The answers are tallied until a 0 is entered.
* The program should then show the results of the poll—try making
* a bar graph that shows the results properly scaled to fit on
* your screen no matter how many results were entered.
*/
#include <iostream>
#include <string>
void startPoll (void);
void showPoll (void);
void pollCheck (void);
std::string pollQuestion, answer1, answer2, answer3;
int pollChoice, firstAnswer, secondAnswer, thirdAnswer;
int main (void)
{
int totalVotes = 1;
float votePercentage;
startPoll();
showPoll();
for(;;totalVotes++)
{
if (pollChoice == 1)
{
firstAnswer = firstAnswer + 1;
}
else if (pollChoice == 2)
{
secondAnswer++;
}
else if (pollChoice == 3)
{
thirdAnswer++;
}
else
{
std::cout << "==============*======*======*==============\n"
<< " RESULT \n"
<< "==============*======*======*==============\n"
<< "Question: " << pollQuestion << "\n"
<< "Total Votes: " << totalVotes << "\n";
votePercentage = (firstAnswer/totalVotes)*100;
std::cout << answer1 << ": " << firstAnswer << " votes. | " << votePercentage << "\n";
votePercentage = secondAnswer/totalVotes*100;
std::cout << answer2 << ": " << secondAnswer << " votes. | " << votePercentage << "\n";
votePercentage = thirdAnswer/totalVotes*100;
std::cout << answer3 << ": " << thirdAnswer << " votes. | " << votePercentage << "\n";
return 0;
}
std::cout << "\nEnter your vote again\nOR\nuse 0 to show the results.\n";
std::cin >> pollChoice;
}
std::cout << "Error: Something went wrong!\n";
}
void startPoll (void)
{
std::cout << "Enter your poll question:\n";
getline (std::cin, pollQuestion, '\n');
std::cout << "Enter answer 1:\n";
getline (std::cin, answer1, '\n');
std::cout << "Enter answer 2:\n";
getline (std::cin, answer2, '\n');
std::cout << "Enter answer 3:\n";
getline (std::cin, answer3, '\n');
}
void showPoll (void)
{
std::cout << "==============|======|======|==============\n"
<< " POLL \n"
<< "==============|======|======|==============\n"
<< pollQuestion << "\n"
<< "1. " << answer1 << "\n"
<< "2. " << answer2 << "\n"
<< "3. " << answer3 << "\n\n"
<< "Enter 1,2 or 3:\n\n";
std::cin >> pollChoice;
pollCheck();
}
void pollCheck (void)
{
if (pollChoice != 1 && pollChoice != 2 && pollChoice != 3)
{
std::cout << "Wrong choice entered! Please try again.\n\n";
return showPoll();
}
}
You need to take care that integer/integer = integer. In your case, changing
(firstAnswer/totalVotes)*100
to
(1.0*firstAnswer/totalVotes)*100
or
(firstAnswer*100.0/totalVotes)
should work. They all give a floating point result.
Well, the solution for the Bar Chart could be the following:(Not written by me) I think thats very self explaining because its really basic
void line (int n, char c)
{
// this is the loop for n
for (int i = 0; i < n; i++)
cout << c << endl;
}
Here is my solution, you can see how I made the bars work by reading the comments.
#include <iostream>
using namespace std;
int main()
{
int a = 0;
int b = 0;
int c = 0;
cout << "What is your favorite animal? 1 Cat, ";
cout <<"2 Dog, 3 Fish, 0 Count votes" << endl;
//Choice counter
while (true)
{
int choice;
cout << "Choice: ";
cin >> choice;
if(choice == 1)
a++;
else if(choice == 2)
b++;
else if(choice == 3)
c++;
else if(choice == 0)
break;
else
continue;
}
cout << endl << " 1: " << a << endl;
cout << " 2: " << b << endl;
cout << " 3: " << c << endl;
cout << endl << "1\t" << "2\t" << "3\t" << endl;
//Finds the max voted option
int max = 0;
if(a > b && a > c)
max = a;
else if(b > c && b > a)
max = b;
else if(c > a && c > b)
max = c;
/* If the max voted option is bigger than 10, find by how much
we have to divide to scale the graph, also making 10 bar
units the max a bar can reach before scaling the others too */
int div =2;
if(max > 10)
{
do
{
max = max/div;
if(max < 10)
break;
div++;
}while(true);
}else
div = 1;
//Sets the final number for the bars
a=a/div;
b=b/div;
c=c/div;
if(a==0)
a++;
if(b==0)
b++;
if(c==0)
c++;
//Creates the bars
while(true)
{
if(a>0)
{
cout << "[]" << "\t";
a--;
}else
cout << " ";
if(b>0)
{
cout << "[]" << "\t";
b--;
}else
cout << " ";
if(c>0)
{
cout << "[]" << "\t";
c--;
}else
cout << " ";
cout << endl;
if(a==0 && b==0 && c==0)
break;
}
}
Every time I do anything, and my while(1) gets called in my main function, my file gets cleared. It's driving me crazy. I've tried EVERYTHING. I try to put my ofstream out("data.dat"); outside the while(1) statement so it isn't called everytime but then nothing is written to the file like the ofstream isn't even working!
I've tried to make the ofstream static, so it isn't called over and over again like:
static ofstream open("data.dat");
That doesn't work.
And like I said, when I put the ofstream OUTSIDE the while statement, nothing is written to the file. Like:
ofstream out("data.dat");
while (1)
{
string line = "";
cout << "There are currently " << structList.size() << " items in memory.";
cout << endl << endl;
cout << "Commands: " << endl;
cout << "1: Add a new record " << endl;
cout << "2: Display a record " << endl;
cout << "3: Edit a current record " << endl;
cout << "4: Delete a record " << endl;
cout << "5: Save current information " << endl;
cout << "6: Exit the program " << endl;
cout << endl;
cout << "Enter a command 1-6: ";
getline(cin , line);
int rValue = atoi(line.c_str());
system("cls");
switch (rValue)
{
case 1:
structList = addItem(structList);
break;
case 2:
displayRecord(structList);
break;
case 3:
structList = editRecord(structList);
break;
case 4:
deleteRecord(structList);
break;
case 5:
if (!structList.size()) { cout << "There are no items to save! Enter one first!" << endl << endl; system("pause"); system("cls"); break; }
writeVector(out , structList);
break;
case 6:
return 0;
default:
cout << "Command invalid. You can only enter a command number 1 - 6. Try again. " << endl;
}
out.close();
}
And can someone tell me why my check to prevent reading of a empty file won't work?
My Check:
bool checkFileEmpty()
{
ifstream in("data.dat");
if (in.peek() == in.eofbit)
{
return true;
}
return false;
}
I am so sick and tired of my program crashing on startup over and over again because my vector is getting set to a size of 200 million. I've tried a BUNCH of stuff for this... none of it works... Please GOD someone help me with both of these! I've been up for 18 hours working on this ( all night yes ) and i'm ALMOST done. I'm begging you....
My Code:
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace System;
using namespace std;
#pragma hdrstop
bool isValidChoice(int size, int choice);
bool checkFileEmpty();
template<typename T>
void writeVector(ofstream &out, const vector<T> &vec);
template<typename T>
vector<T> readVector(ifstream &in);
template<typename T>
vector<T> addItem(vector<T> &vec);
template<typename T>
void printItemDescriptions(vector<T> &vec);
template<typename T>
int displayRecord(vector<T> &vec);
template<typename T>
vector<T> editRecord(vector<T> &vec);
template<typename T>
vector<T> deleteRecord(vector<T> &vec);
struct InventoryItem {
string Description;
int Quantity;
int wholesaleCost;
int retailCost;
string dateAdded;
} ;
int main(void)
{
cout << "Welcome to the Inventory Manager extreme! [Version 1.0]" << endl;
ifstream in("data.dat");
if (in.is_open()) { cout << "File \'data.dat\' has been opened successfully." << endl; } else { cout << "Error opening data.dat" << endl;}
cout << "Loading data..." << endl;
vector<InventoryItem> structList = readVector<InventoryItem>( in );
cout <<"Load complete." << endl << endl;
in.close();
while (1)
{
string line = "";
cout << "There are currently " << structList.size() << " items in memory.";
cout << endl << endl;
cout << "Commands: " << endl;
cout << "1: Add a new record " << endl;
cout << "2: Display a record " << endl;
cout << "3: Edit a current record " << endl;
cout << "4: Delete a record " << endl;
cout << "5: Save current information " << endl;
cout << "6: Exit the program " << endl;
cout << endl;
cout << "Enter a command 1-6: ";
getline(cin , line);
int rValue = atoi(line.c_str());
system("cls");
ofstream out("data.dat");
switch (rValue)
{
case 1:
structList = addItem(structList);
break;
case 2:
displayRecord(structList);
break;
case 3:
structList = editRecord(structList);
break;
case 4:
deleteRecord(structList);
break;
case 5:
if (!structList.size()) { cout << "There are no items to save! Enter one first!" << endl << endl; system("pause"); system("cls"); break; }
writeVector(out , structList);
break;
case 6:
return 0;
default:
cout << "Command invalid. You can only enter a command number 1 - 6. Try again. " << endl;
}
out.close();
}
system("pause");
return 0;
}
template<typename T>
void writeVector(ofstream &out, const vector<T> &vec)
{
out << vec.size();
for(vector<T>::const_iterator i = vec.begin(); i != vec.end(); i++)
{
out << *i;
}
cout << "Save completed!" << endl << endl;
}
ostream &operator<<(ostream &out, const InventoryItem &i)
{
out << i.Description << ' ';
out << i.Quantity << ' ';
out << i.wholesaleCost << ' ' << i.retailCost << ' ';
out << i.dateAdded << ' ';
return out;
}
istream &operator>>(istream &in, InventoryItem &i)
{
in >> i.Description;
in >> i.Quantity;
in >> i.wholesaleCost >> i.retailCost;
in >> i.dateAdded;
return in;
}
template<typename T>
vector<T> readVector(ifstream &in)
{
size_t size;
if (checkFileEmpty())
{
size = 0;
} else {
in >> size;
}
vector<T> vec;
vec.reserve(size);
for(unsigned int i = 0; i < size; i++)
{
T tmp;
in >> tmp;
vec.push_back(tmp);
}
return vec;
}
template<typename T>
vector<T> addItem(vector<T> &vec)
{
system("cls");
string word;
unsigned int number;
InventoryItem newItem;
cout << "-Add a new item-" << endl << endl;
cout << "Enter the description for the item: ";
getline (cin , word);
newItem.Description = word;
cout << endl;
cout << "Enter the quantity on hand for the item: ";
getline (cin , word);
number = atoi(word.c_str());
newItem.Quantity = number;
cout << endl;
cout << "Enter the Retail Cost for the item: ";
getline (cin , word);
number = atoi(word.c_str());
newItem.retailCost = number;
cout << endl;
cout << "Enter the Wholesale Cost for the item: ";
getline (cin , word);
number = atoi(word.c_str());
newItem.wholesaleCost = number;
cout << endl;
cout << "Enter current date: ";
getline (cin , word);
newItem.dateAdded = word;
vec.push_back(newItem);
return vec;
}
template<typename T>
void printItemDescriptions(vector<T> &vec)
{
int size = vec.size();
if (size)
{
cout << "---------------------------------" << endl;
cout << "| ~ Item Descriptions ~ |" << endl;
cout << "---------------------------------" << endl;
cout << "*********************************" << endl;
for (int i = 0 ; i < size ; i++)
{
cout << "(" << i+1 << ")" << ": " << vec[i].Description << endl;
}
cout << "*********************************" << endl << endl;
}
}
template<typename T>
int displayRecord(vector<T> &vec)
{
string word = "";
string quit = "quit";
int choice = 1;
int size = vec.size();
if (size)
{
printItemDescriptions(vec);
cout << endl;
while (1)
{
cout << "Type \"exit\" to return to the Main Menu." << endl << endl;
cout << "Enter \"list\" to re-display the items." << endl << endl;
cout << endl;
cout << "Pick the number of the item you would like to display: ";
getline (cin , word);
if (convertToLower(word) == "exit") { system("cls"); return 0; }
if (convertToLower(word) == "list") { system("cls"); displayRecord(vec); }
choice = atoi(word.c_str());
choice -= 1;
if (isValidChoice(size, choice))
{
system("cls");
cout << endl << "[Item (" << choice << ") details] " << endl << endl;
cout << "******************" << endl;
cout << "* Description * " << vec[choice].Description << endl;
cout << "******************" << endl << endl;
cout << "******************" << endl;
cout << "*Quantity On Hand* " << vec[choice].Quantity << endl;
cout << "******************" << endl << endl;
cout << "******************" << endl;
cout << "* Wholesale Cost * " << vec[choice].wholesaleCost << endl;
cout << "****************** " << endl << endl;
cout << "******************" << endl;
cout << "* Retail Cost * " << vec[choice].retailCost << endl;
cout << "****************** " << endl << endl;
cout << "******************" << endl;
cout << "* Data Added * " << vec[choice].dateAdded << endl;
cout << "****************** " << endl << endl;
} else { system("cls"); cout << "That item doesn't exist!" << endl; cout << "Pick another item or enter \"list\" to see available items." << endl << endl; }
}
} else { cout << "There are currently no items to display." << endl << endl; system("pause"); system("cls"); return 0; }
return 1;
}
bool isValidChoice(int size, int choice)
{
for (int i = 0 ; i <= size ; i++)
{
if (choice == i) { return true; }
}
return false;
}
string convertToLower(string word)
{
for (unsigned int i = 0 ; i < word.size() ; i++)
{
word[i] = tolower(word[i]);
}
return word;
}
bool checkFileEmpty()
{
ifstream in("data.dat");
if (in.peek() == in.eofbit)
{
return true;
}
return false;
}
template<typename T>
vector<T> editRecord(vector<T> &vec)
{
string word;
int choice;
printItemDescriptions(vec);
cout << "Choose item to edit: ";
getline ( cin, word );
choice = atoi(word.c_str());
system("cls");
unsigned int number;
InventoryItem newItem;
cout << "-Edit an item-" << endl << endl;
cout << "Enter the description for the item: ";
getline (cin , word);
vec[choice-1].Description = word;
cout << endl;
cout << "Enter the quantity on hand for the item: ";
getline (cin , word);
number = atoi(word.c_str());
vec[choice-1].Quantity = number;
cout << endl;
cout << "Enter the Retail Cost for the item: ";
getline (cin , word);
number = atoi(word.c_str());
vec[choice-1].retailCost = number;
cout << endl;
cout << "Enter the Wholesale Cost for the item: ";
getline (cin , word);
number = atoi(word.c_str());
vec[choice-1].wholesaleCost = number;
cout << endl;
cout << "Enter current date: ";
getline (cin , word);
vec[choice-1].dateAdded = word;
system("cls");
cout << "Item edited successfully! " << endl;
return vec;
}
template<typename T>
vector<T> deleteRecord(vector<T> &vec)
{
if (!vec.size()) { cout << "There are no items to delete!" << endl << endl; return vec; }
printItemDescriptions(vec);
string word;
int choice;
cout << "Choose item to delete: ";
getline( cin, word);
choice = atoi(word.c_str());
vec.erase (vec.begin()+choice-1);
return vec;
}
You'd better move the ofstream openning and closing inside case 5.
Here you create a new file at each while iteration.
case 5:
{
ofstream out("data.dat");
writeVector(out , structList);
out.close();
}
break;
ofstream out("data.dat");
Opens the file for writing. By default, it will start at the very beginning wiping out anything that was there previously. First, use a different output file than the file you are reading from.
Try adding the close to the case 6 statements:
case 6:
out.close();
return 0;
I'm pretty sure the close isn't getting called as the return will exit main before getting to that statement. With the file unclosed you are left with a buffer unflushed and I suspect that will leave the data unwritten.
You should move the open to before the while loop and also remove the out.close() from the while loop as it's going to close the file after the first menu selection.
To check if a file is empty or cannot be opened
bool IsEmpty( const std::string & filename ) {
std::ifstream ifs( filename.c_str() );
if ( ifs.is_open() ) {
std::string line;
return ! std::getline( ifs, line );
}
else {
return true;
}
}