File Handling - String and Iteration

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 2151 Views - Last Post: 03 December 2010 - 07:23 AM Rate Topic: -----

#1 issyl  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 118
  • Joined: 25-October 10

File Handling - String and Iteration

Posted 01 December 2010 - 07:57 AM

I have questions in the code. in add function
#include <stdio.h>
#include <conio.h>
#include <iomanip.h>
#include <ctype.h>
#include <string.h>
#include <iostream.h>
#define input "d:\\tcpp\\bin\\files\\input.txt"
#define tempo "d:\\tcpp\\bin\\files\\tempo.txt"

void menu(void)
{
	cout << "Menu\n\n";
	cout << "A - Add\n";
	cout << "D - Delete\n";
	cout << "I - Display\n";
	cout << "E - Edit\n";
	cout << "S - Search\n";
	cout << "X - Exit\n\n\n";
	cout << "Enter your choice: ";
}

void add(void)
{
	FILE *infile;
	char Name[50], Target[50], Answer[3], Checker[50];
	int ID = 0;
	float Amount;
	char Gender;
do{
	clrscr();
	if((infile = fopen(input, "a+")) == NULL)
		ID = 1;
	else
	{
		do{
			fscanf(infile, "%i %s %c %f", &ID, Name, &Gender, &Amount);
			Name = Checker;
		}while(!feof(infile));
		ID += 1;
	}
	cout << "ID  :    " << ID << endl;
	cout << "Name:    ";
	cin >> Name;
	cout << "Gender:  ";
	cin >> Gender;
	cout << "Amount:  ";
	cin >> Amount;
/* How come this doesnt work at all ? /*
	if(Name == Checker)
	{
		cout << "Record Exist. Cannot add. " << endl;
		cout << "ID  :    " << ID << endl;
		cout << "Name:    ";
		cin >> Name;
		cout << "Gender:  ";
		cin >> Gender;
		cout << "Amount:  ";
		cin >> Amount;
	}

	fprintf(infile, "%i %s %c %f\n", ID, Name, Gender, Amount);
/* How come when I work just with char Answer and [y/n] as choices it works
but if yes or no declared as strings doesn''t work? how do i fix it? */
	cout << "Do you want to add another [yes/no]: ";
	cin >> Answer;
	fclose(infile);
}while(Answer != "no");
}




This post has been edited by issyl: 01 December 2010 - 08:26 AM


Is This A Good Question/Topic? 0
  • +

Replies To: File Handling - String and Iteration

#2 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: File Handling - String and Iteration

Posted 01 December 2010 - 09:22 AM

How come you're using C headers (*.h) but using C++ I/O functions?
How come you're using c-style strings (char arrays) but using C++ string operators ( == ) instead of strcmp?
Are you supposed to be writing C or C++ code?
Was This Post Helpful? 1
  • +
  • -

#3 jimblumberg  Icon User is online

  • member icon


Reputation: 4232
  • View blog
  • Posts: 13,281
  • Joined: 25-December 09

Re: File Handling - String and Iteration

Posted 01 December 2010 - 09:54 AM

The biggest problem you are having is the use of a non-standard compiler (Turbo-C) and mixing C and C++ code. You can not compare C-strings with the "==" you must use strcmp().

Jim
Was This Post Helpful? 2
  • +
  • -

#4 issyl  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 118
  • Joined: 25-October 10

Re: File Handling - String and Iteration

Posted 02 December 2010 - 05:12 AM

This is my overall program. Im still having a problem in adding. I just want to ask the user if he/she wants to add more[yes/no] and if the input(name) already existed, the screen will prompt that it already existed. cannot add... How am I supposed to fix it? Pls help me. Our next exam will be on Monday again. Pls.

No errors/warnings appeared. In input.txt works fine. But the also problem in testing if the name already existed is like this:

example:

ID: 1
Name: John
Gender: M
Amount: 123

Record existed. Cannot add!

ID: THEN ILL TYPE ANOTHER HERE!
Name: Jojo
Gender F
Amount: 321

Do you want to add another? Yes
if i say yes it will proceed to adding
if i say no it will still proceed to adding

By the way, we are only allowed to use Turbo C++. I cannot proceed to fstream because there will be separate quizzes on C++ and C with C++ language.

#include <stdio.h>
#include <conio.h>
#include <iomanip.h>
#include <ctype.h>
#include <string.h>
#include <iostream.h>
#define input "d:\\tcpp\\bin\\files\\input.txt"
#define tempo "d:\\tcpp\\bin\\files\\tempo.txt"

void menu(void)
{
	cout << "Menu\n\n";
	cout << "A - Add\n";
	cout << "D - Delete\n";
	cout << "I - Display\n";
	cout << "E - Edit\n";
	cout << "S - Search\n";
	cout << "X - Exit\n\n\n";
	cout << "Enter your choice: ";
}

void add(void)
{
	FILE *infile;
	char Name[50], Target[50], Checker[50], Answer[5], Answer2[5] = "No";
	int ID = 0;
	float Amount;
	char Gender;
do{
	clrscr();
	if((infile = fopen(input, "a+")) == NULL)
		ID = 1;
	else
	{
		do{
			fscanf(infile, "%i %s %c %f", &ID, Name, &Gender, &Amount);
			strcpy(Name,Checker);
		}while(!feof(infile));
		ID += 1;
	}
	cout << "ID  :    " << ID << endl;
	cout << "Name:    " << flush;
	cin >> Name;
	cout << "Gender:  " << flush;
	cin >> Gender;
	cout << "Amount:  " << flush;
	cin >> Amount;
	if(strcmp(Name,Checker) != 0)
	{
		cout << "Record Exist. Cannot add. " << endl;
		cout << "ID  :    " << ID << endl;
		cout << "Name:    ";
		cin >> Name;
		cout << "Gender:  ";
		cin >> Gender;
		cout << "Amount:  ";
		cin >> Amount;
	}

	fprintf(infile, "%i %s %c %f\n", ID, Name, Gender, Amount);
	cout << "Do you want to add another [yes/no]: ";
	cin >> Answer;
	fclose(infile);
}while(strcmp(Answer,Answer2) != 0);
}

void deletes(void)
{
	FILE *infile;
	FILE *tfile;
	char Name[50], Target[50];
	int ID = 0, found = 0;
	float Amount;
	char Gender;
	tfile = fopen(tempo, "w");

	if((infile = fopen(input, "r")) == NULL)
		cout << "File Empty!" << endl;
	else
	{
		cout <<"Enter Name to delete: ";
		cin >> Target;
		while(!feof(infile))
		{
			fscanf(infile, "%i %s %c %f", &ID, Name, &Gender, &Amount);
			if(feof(infile))
				break;
			if(strcmp(Target, Name) != 0)
				fprintf(tfile, "%i %s %c %f\n", ID, Name, Gender, Amount);
			else
			{
				found = 1;
				cout << "ID:   " << ID << endl;
				cout << "Name: " << Name << endl;
				if(toupper(Gender) == 'F')
					cout << "Gender:\tFemale\n";
				else
					cout << "Gender:\tMale\n";
				cout << "Amount: " << Amount << endl;
			}
		}

		if (!found)
			cout << "Record not found!\n";
	}
		cout << "\nRecord deleted.";
		fclose(infile);
		fclose(tfile);
		remove(input);
		rename(tempo, input);
}

void display(void)
{
	FILE *infile;
	char Name[50], Target[50];
	int ID = 0;
	float Amount;
	char Gender;
	if((infile = fopen(input, "a+")) == NULL)
		cout << "File Empty!";
	else
	{
		cout << "POLYTECHNIC UNIVERSITY OF THE PHILIPPINES" << endl;
		while(!feof(infile))
		{
			fscanf(infile, "%i %s %c %f", &ID, Name, &Gender, &Amount);
			if(feof(infile))
				break;
			cout << ID << " " << Name << " " << Gender << " " << Amount << endl;
		}
	}
	cout << "\nEnd of file, press any key to exit." << endl;
	fclose(infile);
}

void search(void)
{
	FILE *infile;
	char Name[50], Target[50];
	int ID = 0, found = 0;
	float Amount;
	char Gender;

	if ((infile = fopen(input, "r+")) == NULL)
		cout << "File Empty!";
	else
	{
		cout << "Enter Name to search: ";
		cin >> Target;
		while(found == 0 && !feof(infile))
		{
			fscanf(infile, "%i %s %c %f", &ID, Name, &Gender, &Amount);
			if(strcmp(Target, Name) == 0)
			found = 1;
		}
		if(found)
		{
			cout << "ID:   " << ID << endl;
			cout << "Name: " << Name << endl;
			if(toupper(Gender) == 'F')
				cout << "Gender: Female\n";
			else
				cout << "Gender: Male\n";
			cout << "Amount: " << Amount << endl;
		}
	}
	fclose(infile);
}

void edit(void)
{
	FILE *infile;
	FILE *tfile;
	char Name[50], Target[50];
	int ID = 0, found = 0, back;
	float Amount;
	char Gender;
	tfile = fopen(tempo, "a+");
	if ((infile = fopen(input, "r+")) == NULL)
		cout << "File Empty!";
	else
	{
		cout << "Enter Name to edit: ";
		cin >> Target;
		while(!found)
		{
			while(!feof(infile))
			{
				fscanf(infile, "%i %s %c %f\n", &ID, Name, &Gender, &Amount);
				if(strcmp(Target, Name) != 0)
					fprintf(tfile, "%i %s %c %f\n", ID, Name, Gender, Amount);
				else
				{
					back = ID;
					found = 1;
					cout << "ID:   " << ID << endl;
					cout << "Name: " << Name << endl;
					if(toupper(Gender) == 'F')
						cout << "Gender: Female\n";
					else
						cout << "Gender: Male\n";
					cout << "Amount: " << Amount << endl;
				}
			}
		}
		if(found)
		{

			ID = back;
			cout << "Enter new information\n\n";
			cout << "ID  :    " << ID << endl;
			cout << "Name:    ";
			cin >> Name;
			cout << "Gender:  ";
			cin >> Gender;
			cout << "Amount:  ";
			cin >> Amount;
			cout << "Record edited!" << endl;

			fprintf(tfile, "%i %s %c %f\n", ID, Name, Gender, Amount);
		}
	}
	fclose(infile);
	fclose(tfile);
	remove(input);
	rename(tempo, input);

}
int main(void)
{
	char choice;

	cout.precision(2);
	cout.setf(ios :: fixed);
do{
	clrscr();
	menu();
	scanf("%c", &choice);
	switch(toupper(choice))
	{
	case 'A':
		clrscr();
		add();
		getch();
		break;
	case 'D':
		clrscr();
		deletes();
		getch();
		break;
	case 'I':
		clrscr();
		display();
		getch();
		break;
	case 'E':
		clrscr();
		edit();
		getch();
		break;
	case 'S':
		clrscr();
		search();
		getch();
		break;
	}
}while(toupper(choice) != 'X');
	return 0;
}


Was This Post Helpful? 0
  • +
  • -

#5 issyl  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 118
  • Joined: 25-October 10

Re: File Handling - String and Iteration

Posted 02 December 2010 - 06:43 AM

can anyone answer my question plssss i beg you guys. sorry for begging though :( but i really need it for my exams.
Was This Post Helpful? -1
  • +
  • -

#6 jimblumberg  Icon User is online

  • member icon


Reputation: 4232
  • View blog
  • Posts: 13,281
  • Joined: 25-December 09

Re: File Handling - String and Iteration

Posted 02 December 2010 - 07:22 AM

Quote

I just want to ask the user if he/she wants to add more[yes/no


So where have you tried to ask the user the question?

Quote

if the input(name) already existed, the screen will prompt that it already existed


So where have you tried to see if the record is a duplicate?

Quote

I cannot proceed to fstream because there will be separate quizzes on C++ and C with C++ language.


This makes no sense as most of this program is C++ already.

Jim
Was This Post Helpful? 0
  • +
  • -

#7 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: File Handling - String and Iteration

Posted 02 December 2010 - 07:31 AM

ON line 64 you are comparing Answer to Answer2. Answer2 is "No". If you enter "no" for Answer, of course they are different so the input loop continues.
Was This Post Helpful? 1
  • +
  • -

#8 issyl  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 118
  • Joined: 25-October 10

Re: File Handling - String and Iteration

Posted 02 December 2010 - 08:10 AM

Oh oh yeah tnx for solving the Yes / No portion. Now my only problem is on checking whether the input already existed.

@jimblumberg

on line 35- 57 i used strcpy to copy the name to another variable. Then if the new variable with the same content are the same wih the new input, it will prompt the user that the record already existed. How do i fix it?
Was This Post Helpful? 0
  • +
  • -

#9 issyl  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 118
  • Joined: 25-October 10

Re: File Handling - String and Iteration

Posted 02 December 2010 - 08:54 AM

IS ANYBODY HERE? PLS HELP LAST QUESTION PLSSS
Was This Post Helpful? -1
  • +
  • -

#10 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: File Handling - String and Iteration

Posted 02 December 2010 - 09:19 AM

Checker is a char array that you didn't initialize, so whatever is stored there is garbage.

If the input file exists, each time you read an entry from the file, fscanf (line 36) stores the name in Name. Then (line 37) you do strcpy(Name, Checker), which copies garbage from Checker to Name, wiping out the name that you just read from the file. That's one problem. Didn't you mean to copy FROM Name TO Checker? i.e. strcpy(Checker, Name);

If you fix that, when you finish reading entries from the file Checker will contain the LAST name that was read from the file. Now, on line 48, if(strcmp(Name,Checker) != 0) says:
"if the string in Name (the new one entered at the keyboard) is different from the string Checker (the LAST one entered from the file)" then cout << "Record Exist. Cannot add. " and so on...
You have 2 problems there: first of all, instead of "is different" didn't you want "is the same"? And second, what if the file contained not 1 but 5 entries. Don't you need to compare Name to the names of all 5 entries from the file?

In other words, either you should be reading entries from the file into a 2-dimensional array of records, and then checking the new entry against all of the records in the array, or you should switch things around so that you input the new entry from the keyboard FIRST, and then while you are reading entries from the file, check the new name against each record that you read from the file.

This post has been edited by r.stiltskin: 02 December 2010 - 09:37 AM

Was This Post Helpful? 0
  • +
  • -

#11 jimblumberg  Icon User is online

  • member icon


Reputation: 4232
  • View blog
  • Posts: 13,281
  • Joined: 25-December 09

Re: File Handling - String and Iteration

Posted 02 December 2010 - 09:38 AM

	if(strcmp(Name,Checker) != 0)
	{
		cout << "Record Exist. Cannot add. " << endl;
		cout << "ID  :    " << ID << endl;
		cout << "Name:    ";
		cin >> Name;
		cout << "Gender:  ";
		cin >> Gender;
		cout << "Amount:  ";
		cin >> Amount;
	}

	fprintf(infile, "%i %s %c %f\n", ID, Name, Gender, Amount); 
	cout << "Do you want to add another [yes/no]: ";



So in this section of code you tell the user that this is a duplicate and can not add the record, But then you go ahead and add it anyway.

You need to rethink the entire add function. Just tacking on a do/while loop here or there will not do the trick. You should read the file one time only, putting the variables that your read into arrays or better yet an array of structures. After you read the file then you can get the new name, check it against the names you read from the file, and if no duplicates then add the record. If you have duplicates then don't add the record, but ask the user if they want to try again. If they want to try again then just get the new information and recheck against the variables you got from the file.

Jim
Was This Post Helpful? 1
  • +
  • -

#12 issyl  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 118
  • Joined: 25-October 10

Re: File Handling - String and Iteration

Posted 02 December 2010 - 06:54 PM

If i will store the name into the array it can only store 1. We haven't studied structures yet. We are still on file handling. Here is the code that i've tried. What do you mean to store it into an array? If i will only read once then i cannot check the whole thing.

void add(void)
{
	FILE *infile;
	char Name[50], Name2[50], Target[50], Checker[50], Answer[5], Answer2[5] = "No";
	int ID = 0, Found = 0;
	float Amount;
	char Gender;
do{
	clrscr();
	if((infile = fopen(input, "a+")) == NULL)
		ID = 1;
	else
	{
		do{
			fscanf(infile, "%i %s %c %f\n", &ID, Name, &Gender, &Amount);
			if(feof(infile))
			{
				ID++;
				break;
			}
			cout << "ID  :    " << ID << endl;
			cout << "Name:    " << flush;
			cin >> Name2;
			cout << "Gender:  " << flush;
			cin >> Gender;
			cout << "Amount:  " << flush;
			cin >> Amount;


				if(strcmp(Name,Name2) == 0)
					cout << "Record Exist. Cannot add. " << endl;
				else
				{
				fprintf(infile, "%i %s %c %f\n", ID, Name, Gender, Amount);
				Found = 1;
				}

		}while(!feof(infile) && Found == 0);
	}
	cout << "Do you want to add another [Yes/No]: ";
	cin >> Answer;
	fclose(infile);
}while(strcmp(Answer,Answer2) != 0);
}



Was This Post Helpful? 0
  • +
  • -

#13 issyl  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 118
  • Joined: 25-October 10

Re: File Handling - String and Iteration

Posted 02 December 2010 - 07:35 PM

This is my next try:

void add(void)
{
	FILE *infile;
	char Name[50], Name2[50], Target[50], Checker[50], Answer[5], Answer2[5] = "No";
	int ID = 0, Found = 0;
	float Amount;
	char Gender;

	clrscr();
	if((infile = fopen(input, "a+")) == NULL)
		ID = 1;
	else
	{
	do{
		do{
			fscanf(infile, "%i %s %c %f\n", &ID, Name, &Gender, &Amount);
			ID++;
			cout << "ID  :    " << ID << endl;
			cout << "Name:    " << flush;
			cin >> Name2;
			cout << "Gender:  " << flush;
			cin >> Gender;
			cout << "Amoun:  " << flush;
			cin >> Amount;


				if(strcmp(Name,Name2) == 0)
					cout << "Record Exist. Cannot add. " << endl;
				else
				{
				fprintf(infile, "%i %s %c %f\n", ID, Name, Gender, Amount);
				Found = 1;
				}

		}while(!feof(infile) && Found == 0);
		cout << "Do you want to add another [Yes/No]: ";
		cin >> Answer;
	}while(strcmp(Answer,Answer2) != 0);
	fclose(infile);
	}

}



The output was :
1 ''n* 3[* ^  `   m 123.000000
2 ''n* 3[* ^  `   m 123.000000
3 ''n* 3[* ^  `   m 213.000000


Pls tell me the answer... I guess i understand the clue but i do not know how will i supposed to store the variable into an array. Plss
Was This Post Helpful? 0
  • +
  • -

#14 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: File Handling - String and Iteration

Posted 02 December 2010 - 08:29 PM

There are many ways to do this but obviously that's not one of them. You can't just aimlessly "try out" some code & hope that it works. Why don't you write down in plain English (pseudocode) step by step how to do this, and THEN translate it into C code.
Was This Post Helpful? 1
  • +
  • -

#15 issyl  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 118
  • Joined: 25-October 10

Re: File Handling - String and Iteration

Posted 02 December 2010 - 11:09 PM

Pls fix my code plsss and after you answered me ban me rather... I rather risk my account just for this question. I do not want to repeat c++ next year plsss.... take my account but pls fix my code...
Was This Post Helpful? -3
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2