Here's a corker for you guys

This is a discussion on Here's a corker for you guys within the C++ Programming forums, part of the General Programming Boards category; Ok, I've got a relatively large program here. The program is that it temporarily populates the main array with an ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    10

    Here's a corker for you guys

    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();
    }
    Go you big red fire engine!

  2. #2
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    You count variable is being incremented an extra time because of the way you are reading the file which is messing up your order. Decrement the count variable by one before your first call to mainmenu() in main and it should work.

    If you have a reasonably large piece of code and you're unsure where your error is, it can help to run it through your compilers debugger (if it's got one) and just watch some of the variables.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    10
    Zen, don't ever let anyone tell you that you're not a legend.

    Thankyou
    Go you big red fire engine!

  4. #4
    Unregistered
    Guest
    The bigger problem is how you are using the file. When you write to the file you always have a # sign at the end of every item. BUT you don't want a # at the end of the last file, you want the EOF marker right after the last item. As it is when you read in the item members using getline() with # as the delimiter you do fine until you get to the end of the last item. Then getline() finds the last #, but it still hasn't found EOF yet. It has read in all of the last item, but it hasn't encountered EOF so the loop condition is still true and the entire loop runs again, but what does it read in??? Probably whatever was in the appropriate input buffer from the previous read in, thus causing the duplicate/extra entry at the end.

    How to prevent this??? Avoid the # at the end of the last item.

    How? By writing each item to file differently. First initialize count to -1. Then increment count before each item is written to (or read from) file. If count is 0 write the first line of data[0] without a preceding # sign and write the # sign before any other data. Otherwise write the # and the new line to the end of the last line of the preceding data[i] before writing the next data[i].

    I have to go now. If you want to see code on how to do this post and I will write it if no one else has already.

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    10
    Thankyou. That appears to have solved the problem. I am forever grateful.
    Go you big red fire engine!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hey guys, I'm new!
    By MrDoomMaster in forum C++ Programming
    Replies: 15
    Last Post: 10-31-2003, 04:47 PM
  2. How long have you guys been working with C/C++??
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 08-01-2003, 03:41 PM
  3. Tic Tac Toe -- Can you guys rate this please?
    By Estauns in forum Game Programming
    Replies: 2
    Last Post: 09-15-2001, 10:22 AM
  4. hello guys
    By lupi in forum C++ Programming
    Replies: 4
    Last Post: 09-13-2001, 06:42 AM
  5. hello guys
    By lupi in forum C++ Programming
    Replies: 1
    Last Post: 09-09-2001, 01:00 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21