Ok, I've got a relatively large program here.
The program is that it temporarily populates the main array with an additional element (somehow taken from the last entered element). This temporary element contains one of the variables inputed by the user, but the rest remains blank. It is not saved to the disk. I'm not sure why this is happening, but I wan't to stop it. If anyone can see what's causing it, I'm all ears.
Here's the program:
Code:
#include <iostream.h>
#include <iomanip.h>
#include <string.h>
#include <stdlib.h>
#include <fstream.h>
#include <ctype.h>
#include <windows.h>
//Declare Variables
int count = 0; //primary input loop control
struct item //item information
{
int identifier;
char description[10];
char category;
double stockval;
};
item data[100] = {0,"",' ',0}; //this is the array that the program uses
//function prototypes
void mainmenu();
void add();
void list();
void catsum();
void exit();
void main()
{
//open the input file on disk
ifstream inFile;
inFile.open("C:\\stock.dat", ios::in);
if (!inFile.fail())
{
while (!inFile.eof())
{
inFile >> data[count].identifier;
inFile.ignore(1);
inFile.get(data[count].description, 10,'#');
inFile.ignore(1);
inFile >> data[count].category;
inFile.ignore(1);
inFile >> data[count].stockval;
inFile.ignore(1);
count = count++; //increase count for next record
}
inFile.close();
mainmenu(); //launch main menu
}
else
{
cout << "Error finding or creating file C:info.tb" << endl;
mainmenu();
}
}
void mainmenu() // main menu function
{
char sel[1];
int b = 0;
system("cls"); //clears the screen
cout << "ÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜ" << endl;
//changes the console to display yellow text (just makes it a bit more interesting)
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_INTENSITY);
cout << "ÛÛÛÛÛÛÛÛÛÛÛ²²²²²²²²²°°°°°°° STOCK MAN °°°°°°°²²²²²²²²²ÛÛÛÛÛÛÛÛÛÛÛ" << endl;
//changes the console back to the normal grey
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE);
cout << "ÛßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßÛ" << endl;
cout << "Û Û" << endl;
cout << "Û Welcome to Stock Man. Û" << endl;
cout << "Û Û" << endl;
cout << "Û Û" << endl;
cout << "Û Please refer to the following menu to make your selection: Û" << endl;
cout << "Û__________________________________________________________________Û" << endl;
cout << "Û Û" << endl;
cout << "Û Û" << endl;
cout << "Û MAIN MENU: Û" << endl;
cout << "Û Û" << endl;
cout << "Û Û" << endl;
cout << "Û 1) Add Û" << endl;
cout << "Û Û" << endl;
cout << "Û 2) List All Û" << endl;
cout << "Û Û" << endl;
cout << "Û 3) Category Summary Û" << endl;
cout << "Û Û" << endl;
cout << "Û 4) Quit Stock Man Û" << endl;
cout << "Û Û" << endl;
cout << "Û Û" << endl;
cout << "ßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßß" << endl;
cout << " " << "Your Selection -> ";
cin >> sel;
b = atoi(sel);
do{
//Checks for valid integer
if (b <= 0)
{ cout << endl;
cout << " " << "Please enter a valid choice [1 - 4]." << endl << endl;
cout << " " << "Your Selection -> ";
cin.ignore(100,'\n');
cin >> sel;
b = atoi(sel);
}
else
{
if ((b!=1)&&(b!=2)&&(b!=3)&&(b!=4))
{
cout << endl;
cout << " " << "Please enter a valid choice [1 - 4]." << endl << endl;
cout << " " << "Your Selection -> ";
cin.ignore(100,'\n');
cin >> sel;
b = atoi(sel);
}
}
}while ((b!=1)&&(b!=2)&&(b!=3)&&(b!=4));
//these are the valid possible choices
if(b == 1) //Add
{
add();
}
else
if(b == 2) //List All
{
list();
}
else
if(b == 3) //Category Summary
{
catsum();
}
else
if(b == 4) //Exit
{
exit();
}
cout << endl;
}
void exit() //exit function
{
system("cls");
cout << "Thankyou for using Stock Man" << endl;
}
void add() //add function
{
char a[1];
int ident = 0; //temp identify variable
char cat; //temp category variable
int ok = 0; //error check control
char stock[10];
system("cls");
//input identifier
ok = 0;
while (ok == 0)
{
cout << "Enter Identifier: ";
cin >> a;
cin.ignore(100, '\n');
ident = atoi(a);
//check if identifier is valid
if ((ident >= 5 && ident <= 53)||(ident >= 100 && ident <= 121))
{
data[count].identifier = ident;
ok = 1;
}
else
{
cout << endl << "Invalid identifier entered" << endl << endl;
ok = 0;
}
}
cout << endl;
cout << "Enter Description: ";
cin >> data[count].description;
cin.ignore(100, '\n');
cout << endl;
//input category
ok = 0;
while (ok == 0)
{
cout << "Enter Category: ";
cin >> cat;
cin.ignore(100, '\n');
cat = toupper(cat);
//check if category is valid
if((cat !='C')&&(cat !='K')&&(cat !='L')&&(cat !='R')&&(cat !='T'))
{
cout << endl << "Invalid category entered" << endl << endl;
ok = 0;
}
else
{
data[count].category = cat;
ok = 1;
}
}
cout << endl;
//input stock value
ok = 0;
while (ok == 0)
{
cout << "Enter Stock Value: ";
cin >> stock;
cin.ignore(100, '\n');
double c = atof(stock);
//check if stock value is valid
if ((c < 2.50)||(c > 500))
{
cout << endl << "Invalid stock value entered" << endl << endl;
ok = 0;
}
else
{
if((data[count].category =='C')||(data[count].category =='K')||(data[count].category =='T'))
{
if (c > 200)
{
cout << endl << "Stock Value is too high for selected category" << endl << endl;
ok = 0;
}
else
{
data[count].stockval = c;
ok = 1;
}
}
else
{
if(data[count].category =='L')
{
if ((c < 200)||(c > 450))
{
cout << endl << "Stock Value is outside of valid category range" << endl << endl;
ok = 0;
}
else
{
data[count].stockval = c;
ok = 1;
}
}
else
{
data[count].stockval = c;
ok = 1;
}
}
}
}
cout << endl;
//writes data to file on disk
ofstream outFile;
outFile.open("C:\\stock.dat", ios::app);
if(!outFile.fail())
{
outFile << data[count].identifier << '#';
outFile << data[count].description << '#';
outFile << data[count].category << '#';
outFile << data[count].stockval << '#';
outFile << endl;
outFile.close();
}
else
{
cout << "error opening file" << endl;
}
mainmenu();
}
void list() //list stock function
{
system("cls");
count = 0;
cout << setw(15) << "Identifier:" << setw(16) << "Description:" << setw(13) << "Category:" << setw(16) << "Stock Value:" << endl;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_INTENSITY);
cout << "_________________________________________________________________" << endl;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE);
//display information for stock list
while (data[count].identifier != 0)
{
cout << setw(15) << data[count].identifier << setw(16) << data[count].description << setw(13) << data[count].category << setw(16) << setprecision(2) << setiosflags(ios::fixed | ios::showpoint) << data[count].stockval << endl;
count = count++;
}
cout << "_________________________________________________________________" << endl;
cout << endl << endl;
system("pause");
mainmenu();
}
void catsum() //category summary function
{
//counters
int c = 0;
double ct = 0;
int k = 0;
double kt = 0;
int l = 0;
double lt = 0;
int r = 0;
double rt = 0;
int t = 0;
double tt = 0;
system("cls");
count = 0;
//open the input file on disk
ifstream inFile;
inFile.open("C:\\stock.dat", ios::in);
if (!inFile.fail())
{
while (!inFile.eof())
{
data[count].category = toupper(data[count].category);
if (data[count].category == 'C')
{
c = c++;
ct = data[count].stockval + ct;
}
else
{
if (data[count].category == 'K')
{
k = k++;
kt = data[count].stockval + kt;
}
else
{
if (data[count].category == 'L')
{
l = l++;
lt = data[count].stockval + lt;
}
else
{
if (data[count].category == 'R')
{
r = r++;
rt = data[count].stockval + rt;
}
else
{
if (data[count].category == 'T')
{
t = t++;
tt = data[count].stockval + tt;
}
else
{
system("cls");
}
}
}
}
}
//end if
inFile >> data[count].identifier;
inFile.ignore(1);
inFile.get(data[count].description, 10,'#');
inFile.ignore(1);
inFile >> data[count].category;
inFile.ignore(1);
inFile >> data[count].stockval;
inFile.ignore(1);
count = count++; //increase count for next record
}
inFile.close();
}
else
{
cout << "Error finding or creating file C:info.tb" << endl;
}
//display the count
cout << setw(13) << "Category:" << setw(10) << "Count:" << setw(10) << "Value:" << endl;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_INTENSITY);
cout << "_____________________________________" << endl;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE);
cout << setw(8) << "C" << setw(13) << c << setw(12) << setprecision(2) << setiosflags(ios::fixed | ios::showpoint) << ct << endl;
cout << setw(8) << "K" << setw(13) << k << setw(12) << setprecision(2) << setiosflags(ios::fixed | ios::showpoint) << kt << endl;
cout << setw(8) << "L" << setw(13) << l << setw(12) << setprecision(2) << setiosflags(ios::fixed | ios::showpoint) << lt << endl;
cout << setw(8) << "R" << setw(13) << r << setw(12) << setprecision(2) << setiosflags(ios::fixed | ios::showpoint) << rt << endl;
cout << setw(8) << "T" << setw(13) << t << setw(12) << setprecision(2) << setiosflags(ios::fixed | ios::showpoint) << tt << endl;
cout << "_____________________________________" << endl;
cout << endl << endl;
system("pause"); //pause the program to allow you to read report
mainmenu();
}