Jump to content

  • Log In with Google      Sign In   
  • Create Account

NUCLEAR RABBIT

Member Since 12 Apr 2006
Offline Last Active Jan 06 2016 02:59 PM

Topics I've Started

C++ Deleting data from files

04 January 2016 - 10:06 PM

Hello,

 

I finished writing a Phonebook application today, I just wanted to practice dealing with writing/reading/handling data from external files. All went well and the application works just like I want it too, but I was just wondering if my implementation is "the correct" way of for deleting specific portions of txt files. I wrote the function 2 different ways, one by creating a vector filled with Person objects that contains info from the contact list and I use those to fill the contact list file and the other way (which is commented out) was done by writing to a tmp txt file with the data minus what I want to delete and write over the original file with whats in the tmp file. I didn't like this way because I had the tmp contact file list lingering around.

 

Any advice on the proper way of handling this type of task or if you know a better way please let me know! Thanks!

void Phonebook::deleteEntry()
{
	int selection;
	bool valid = false;

	do
	{
		displayAllEntries();

		if (num_of_contacts < 1)
			break;

		std::cout << "\n\n\t\t\t\t\tEnter in the Entry to delete (0 = Cancel): ";
		std::cin >> selection;

		valid = std::cin.good();

		// making sure the selection was an int value
		if (!valid)
		{
			std::cin.clear();
			std::cin.ignore(INT_MAX, '\n');
		}
		else
		{
			// checks if user has contacts to delete and if the selection
			// is in range
			if ((selection <= num_of_contacts) && (num_of_contacts > 0) && (selection != 0))
			{
				std::vector<Person> contacts;
				std::ifstream reader("ContactList.txt", std::ios::in);

				if (reader.is_open())
				{
					// read data from ContactList and create Persons
					// to fill the fill the file after deletion
					// ignores the selected contact
					std::string fname, lname, email, phone_number;
					for (int i = 0; i < num_of_contacts; i++)
					{
						reader >> fname >> lname >> email >> phone_number;
                                                
                                                // i is always 1 less than the actual selection number
						if ((i + 1) != selection)
							contacts.push_back(Person(fname, lname, email, phone_number));
					}

					num_of_contacts--;
					reader.close();
				}
				else
				{
					std::cout << "\n\n\t\t\t\t\t*** [ Error! Cannot Open Contact List ] ***";
					std::cout << "\n\t\t\t\t\t"; system("PAUSE");
				}
				
				std::ofstream writer("ContactList.txt", std::ios::out);

				if(writer.is_open())
				{
					// Fills the file with the data from the contact vector
					for (int i = 0; i < num_of_contacts; i++)
					{
						writer << contacts[i].getFirstName() << " "
							   << contacts[i].getLastName() << " "
							   << contacts[i].getEmail() << " "
							   << contacts[i].getNumber() << std::endl;
					}

					std::cout << "\n\n\t\t\t\t\t----------------------------";
					std::cout << "\n\t\t\t\t\t|   Deletion Successfull   |";
					std::cout << "\n\t\t\t\t\t----------------------------";

					writer.close();
				}
				else
				{
					std::cout << "\n\n\t\t\t\t\t*** [ Error! Cannot Open Contact List ] ***";
					std::cout << "\n\t\t\t\t\t"; system("PAUSE");
				}

				// FILE READING/WITING TO TMP FILE IMPLEMENTATION
				/*std::ifstream reader("ContactList.txt", std::ios::in);
				std::ofstream writer("tmp_contacts.txt", std::ios::out);

				if (reader.is_open())
				{
					std::string fname, lname, email, phone_num;

					for (int i = 1; i <= num_of_contacts; i++)
					{
						if (i != selection)
						{
							reader >> fname >> lname >> email >> phone_num;
							writer << fname << " "
								   << lname << " "
								   << email << " "
								   << phone_num << std::endl;
						}
						else
						{
							reader >> fname >> lname >> email >> phone_num;
						}
					}

					num_of_contacts -= 1;

					reader.close();
					writer.close();

					writer.open("ContactList.txt", std::ios::out);
					reader.open("tmp_contacts.txt", std::ios::in);

					for (int i = 1; i <= num_of_contacts; i++)
					{
						reader >> fname >> lname >> email >> phone_num;
						writer << fname << " " 
							   << lname << " " 
							   << email << " "
							   << phone_num << std::endl;
					}

					std::cout << "\n\n\t\t\t\t\t----------------------------";
					std::cout << "\n\t\t\t\t\t|   Deletion Successfull   |";
					std::cout << "\n\t\t\t\t\t----------------------------";
				}
				else
				{
					std::cout << "\n\n\t\t\t\t\t*** [ Error! Cannot Open Contact List ] ***";
					std::cout << "\n\t\t\t\t\t"; system("PAUSE");
				}*/
			}
			else if (selection == 0)
			{
				std::cout << "\n\n\t\t\t\t\t-----------------------------------------";
				std::cout << "\n\t\t\t\t\t|   Canceling Request To Delete Entry   |";
				std::cout << "\n\t\t\t\t\t-----------------------------------------";
			}
			else
			{
				valid = false;

				std::cout << "\n\n\t\t\t\t\t*** [ Error! Invalid Selection ] ***";
				std::cout << "\n\t\t\t\t\t"; system("PAUSE");
			}
		}
	} while ((!valid) && (num_of_contacts != 0));
}

C++ Class Constructor

27 December 2015 - 03:09 PM

Hello,

 

So I am working on a simple phonebook app using file reading/writing, but in my Phonebook class constructor I am having issues. For some reason it is not allowing me to pass a string argument to the constructor. I took out the argument in the definition and declaration and it compiled fine and ran so I know that is where the issue is.

 

The error message is "C2664    'Phonebook::Phonebook(Phonebook &&)': cannot convert argument 1 from 'std::string' to 'const Phonebook &'    MyPhonebook    c:\users\brandon\documents\visual studio 2015\projects\myphonebook\myphonebook\main.cpp    30" and it doesn't make any sense to me. I googled the error message and I am passing a string to the constructor not any other type. I also get another error message saying "C4430    missing type specifier - int assumed. Note: C++ does not support default-int    MyPhonebook    c:\users\brandon\documents\visual studio 2015\projects\myphonebook\myphonebook\person.h    14"
 

Can someone please help me see what is happening? I would greatly appreciate it!

 

Phonebook.h

#pragma once 

#include "LibIncludes.h"

class Phonebook
{
public:
	Phonebook(string tmp_pb_name);
	void addEntry();
	void deleteEntry();
	void searchByFirstName() const;
	void searchByNumber() const;
	void displayAllEntries() const;
	void clearAllEntries();
	int mainMenu();
private:
	string phonebook_name;
};

Phonebook.cpp

///////////////////////////////////////
//
//   Phonebook definitions   
//
///////////////////////////////////////

#include "LibIncludes.h"

Phonebook::Phonebook(string tmp_pb_name)
{
	phonebook_name = tmp_pb_name;
}

//--------------------------------------------------------------------

int Phonebook::mainMenu()
{
	int option = 0;

	do
	{
		system("CLS");

		cout << "\n\n\t\t-----" << phonebook_name << " Phonebook-----";
		cout << "\n\t\t|";
		cout << "\n\t\t|\t[1] Create New Entry";
		cout << "\n\t\t|\t[2] Delete Entry";
		cout << "\n\t\t|\t[3] Display ALL Entries";
		cout << "\n\t\t|\t[4] Search By FIRST Name";
		cout << "\n\t\t|\t[5] Search by PHONE NUMBER";
		cout << "\n\t\t|\t[6] Clear ALL Entries";
		cout << "\n\t\t|\t[7] Exit Your Phonebook";
		cout << "\n\t\t|----------------------------------------------";

		cout << "\n\n\t\t> ";
		cin >> option;

		if (option < 1 || option > 7)
		{
			cout << "\n\t\t*** [ Error! Option Out Of Range! ] ***\n";
			system("PAUSE");
		}
	} while (option < 1 || option > 7);

	return option;
}

//--------------------------------------------------------------------

void Phonebook::addEntry()
{
	
}

//--------------------------------------------------------------------

void Phonebook::displayAllEntries() const
{

}

//--------------------------------------------------------------------

void Phonebook::clearAllEntries()
{

}

//--------------------------------------------------------------------

void Phonebook::deleteEntry()
{

}

//--------------------------------------------------------------------

void Phonebook::searchByFirstName() const
{

}

//--------------------------------------------------------------------

void Phonebook::searchByNumber() const
{

}

Main.cpp

///////////////////////////////////////
//
//   Basic phonebook app that
//   uses writing and reading
//   from files for the data
//   entries
//
//////////////////////////////////////

#include "LibIncludes.h"

string setPhoneBookName()
{
	string name; 

	cout << "\n\n\n\n\t\t\t\t-----Please Enter In Your Desired Phonebook Name-----";
	cout << "\n\t\t\t\t> ";
	cin >> name;

	return name;
}

//---------------------------------------------------------------------------------------

int main()
{
	int user_option = 0;
	bool exit = false;

	Phonebook myBook(setPhoneBookName());

	while (user_option != 7)
	{
		while (!exit)
		{
			user_option = myBook.mainMenu();

			switch (user_option)
			{
			case 7:
				cout << "\n\n\t\t---------------Goodbye---------------\n";
				cout << "\t\t";
				system("PAUSE");
				exit = true;
				break;
			default:
				cout << "\n*** [ Invalid Option! ] ***\n";
			}
		}
	}

	return 0;
}

LibIncludes.h

/////////////////////////////////////
//
//   All header files needed
//
/////////////////////////////////////

#pragma once

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

#include "Person.h"
#include "Phonebook.h"

using namespace std;

C++ VirtualFunctions [SOLVED]

23 December 2015 - 09:18 PM

Hello,

 

I am messing around with inheritance and I was doing a simple example using it, but for some reason it is not working like I expected it to. When I run the code, the greet() method from the Animal base class is being called on both objects, even though I have it as a virtual function. I thought this would allow the child class to behave differently when used with pointers. Could anyone please help me see what I am doing incorrectly or maybe misunderstood?

/////////////////////////////////
//
//   Random C++ tests on
//   concepts and ideas
//
/////////////////////////////////

#include <iostream>
#include <string>
#include <iterator>
#include <vector>

using namespace std;

class Animal
{
public:
	Animal(string tmp_name)
	{
		name = tmp_name;
	}

	virtual void greet()
	{
		cout << "I am " << name << ", and I am Animal" << endl;
	}
protected:
	string name;
};

class Bird : public Animal 
{
public:
	Bird(string tmp_name) : Animal(tmp_name) {};

	void greet()
	{
		cout << "I am " << name << " and I am a Bird" << endl;
	}
};

//////////////////////////////////////////////////////////////////
//   MAIN
//////////////////////////////////////////////////////////////////

int main()
{
	Animal * steve = new Bird("Steve");
	Animal * david = new Animal("David");

	vector<Animal> animal_list = { *steve, *david };

	for (vector<Animal>::iterator itr = animal_list.begin(); itr != animal_list.end(); itr++)
	{
		itr->greet();
	}

	cin.get();

	return 0;
}

MASM Assembly bug [SOLVED]

07 February 2015 - 05:15 PM

Hello,

 

So I was working on a very lame application to practice using assembly and I got the application working and doing what I want it to, but at the application endpoint it prints out the welcome greeting string and I only have one section that calls that greet (in the beginning). Does anyone know why this could be happening? Any help is appreciated!

 

PS - I took a screenshot of the output for you guys to see what I mean.

 

age.asm:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;                                              ;
;   Name: Brandon                              ;
;   Date: 2/6/2015                             ;
;   Desc: Simply asks the user for their age   ;
;         and then greets the user with the    ;
;         age value they entered in            ;
;                                              ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

.model small
.stack 100h

.data
age db ?
welcome_greet db 'Welcome to the GREATEST application EVER!!!', 10, 13, '$'
age_prompt db 'Please enter your age: ', '$'
result_prompt db 10, 13, 'The age you entered is: ', '$'
exit_prompt db 'Goodbye, hope you enjoyed this AMAZING application!', 10, 13, '$'

.code
main proc
mov AX, @DATA              ; must do everytime
mov DS, AX                 ; must also do everytime as well

lea DX, welcome_greet
mov AH, 9h                 ; DOS command to print a string
int 21h

lea DX, age_prompt         ; prompts the user for their age
mov AH, 9h
int 21h

mov AH, 1h                 ; 1h is a DOS command to read in a char
int 21h
mov AGE, AL              

lea DX, result_prompt      ; prints the result message
mov AH, 9h
int 21h

lea DX, AGE                ; prints resulting age
mov AH, 9h
int 21h

mov AH, 04ch               ; exits the DOS application
int 21h

main ENDp
end main


MASM Assembly Help!

06 February 2015 - 06:35 PM

Hello,

 

So I am currently learning MASM 8086 Assembly right now I am have trouble with an application I am attempting to mess around with and create for practice. Not sure what the errors I am getting mean (I am extremely new to assembly). Can someone please help me understand what I am doing wrong? I have attached the screenshot of the error messages and the source code. ANY help would be greatly appreciated!

 

age.asm

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;                                              ;
;   Name: Brandon                              ;
;   Date: 2/6/2015                             ;
;   Desc: Simply asks the user for their age   ;
;         and then greets the user with the    ;
;         age value they entered in            ;
;                                              ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

.model small
.stack 100h

.data
age db ?
welcome_greet db 'Welcome to the GREATEST application EVER!!!', 10, 13, '$'
age_prompt db 'Please enter your age: ', 10, 13, '$'
result_prompt db 'The age you entered is: ', 10, 13, '$'
exit_prompt db 'Goodbye, hope you enjoyed this AMAZING application!', 10, 13, '$'

.code
main proc
mov AX, @DATA              ; must do everytime
mov DS, AX                 ; must also do everytime as well

lea DX, welcome_greet      ; loads the welcome_greet into the DX registery
mov AH, 9h                 ; DOS command to print a string
int 21h

lea DX, age_prompt
move AH, 9h
int 21h

move AH, 1h                ; 1h is a DOS command to read in a char
int 21h
sub AL, '0'                ; converts to an integar
move AGE, AL               ; saves the value into AGE

move AH, 9h
lea DX, result_prompt
int 21h

move AH, 2h                ; prints a char
int 21h

move AH, 04ch              ; exits the DOS application
int 21h

main endp
end main

Also, I have a few questions about the code I have. Is the DX registery only used for loading output messages into?

lea DX, welcome_greet      ; loads the welcome_greet into the DX registery
mov AH, 9h                 ; DOS command to print a string
int 21h

Like I said, I am extreme beginner to assembly so any explinations/corrections would be awesome! biggrin.png


PARTNERS