help:fail load VOID after an if clause

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 784 Views - Last Post: 27 March 2009 - 07:11 PM Rate Topic: -----

#1 nevermine  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 10-March 09

help:fail load VOID after an if clause

Posted 27 March 2009 - 05:55 AM

im doing a program that allow user to add contact,and load added contact.
but i add a login password at the begining.once successfully login(password=1234),it would like to ask to select smth.but it is fail to select either 1 or 3,why?
sorry for my poor english

#include <fstream>
#include <stdio.h>
#include <iostream>
#include <conio.h>

using namespace std;

const int size=2;
struct Contact
{
	char firstname[15], lastname[30];
	char street1[50], street2[50], postcode[8], city[20], state[20], zip[20];
	char phone[20], email[20];
	char month[15];
	int year, day, age;
}list[size];

void LogIn();
void LoadFromFile();
void DisplayList();
void AddContact();
void SaveToFile();
void logout();


void LogIn()
{
	int password=1234, pw;
	int choice;

	cout<<"Main\n"
		<<"(1) Login\n"
		<<"(2) Quit\n";
	cin>> choice;
	
	
	if (choice==1)
	{
		cout<<"please enter password"<<endl<<"Password: ";
		cin>>pw;
			if (pw==password)
			{
				return;
			}
			else
				cout<<"Invalid password!!"<<endl;
				exit(1);
	}


	else if (choice==2)
	{
		cout<<"Quitting\n";
		exit(1);
	}
	return;
}

void logout()
{
	cout<<"You have logged out successfully.\n\n";
	exit(1);
}



void LoadFromFile()
{
	char filename[100], temp[10];
	cout<<"Load from file"<<endl;
	cout<<"Filename: ";
	gets (filename);
	fflush(stdin);
	ifstream fin;
	fin.open(filename,ios_base::in | ios_base::binary );
	if (!fin.is_open())
	{
		cout<<"ERROR! File not found."<<endl;
	}
	else
	{
		cout<<"start loading..."<<endl;
		fin.read((char *)list, sizeof(Contact) * size);
		fin.close();
		cout<<"Loading done!"<<endl;
		cout<<endl;
		DisplayList();
	}
	return;		
}


void DisplayList()
{
	for (int i=0; i<size; i++)
	{
		cout<<"First Name   :"<<list[i].firstname<<endl;
		cout<<"Last Name	:"<<list[i].lastname<<endl;
		cout<<"Address	  :"<<list[i].street1<<list[i].street2<<list[i].postcode<<list[i].city<<list[i].state<<list[i].zip<<endl;
		cout<<"Date Of Birth:"<<list[i].year<<"-"<<list[i].month<<"-"<<list[i].day<<endl;
		cout<<"Age		  :"<<list[i].age <<endl;
		cout<<"Email		:"<<list[i].email <<endl;
	}
	return;
}


void AddContact()
{
	for (int i=0; i<size; i++)
	{
		int age, year;
		int answer;
		cout<<"Name  :";
		gets(list[i].firstname);
		fflush(stdin);
		cout<<endl<<"Last Name:";
		gets(list[i].lastname);
		fflush(stdin);
		cout<<endl<<"Address: "<<endl<<"Street1:";
		gets(list[i].street1);
		fflush(stdin);
		cout<<endl<<"Street2:";
		gets(list[i].street2);
		fflush(stdin);
		cout<<endl<<"PostCode:";
		gets(list[i].postcode);
		fflush(stdin);
		cout<<endl<<"City:";
		gets(list[i].city);		
		fflush(stdin);
		cout<<endl<<"State:";
		gets(list[i].state);		
		fflush(stdin);
		cout<<endl<<"Zip:";
		gets(list[i].zip);		
		fflush(stdin);
		cout<<endl<<"Date of birth(dd,mm,yyyy, separated by spacings): "<<endl;
		cin>>list[i].day>>list[i].month>>year;
		fflush(stdin);
		cout<<endl<<"Email:";
		gets(list[i].email);		
		fflush(stdin);

		age=2009-year;
		list[i].year=year;
		list[i].age=age;

		cout<<"Do you wish to continue?"<<endl
			<<"1 - Yes"<<endl
			<<"2 - No"<<endl;
		cin>>answer;
	}
	return;
}


void SaveToFile()
{
	char filename[100];
	cout<<"Save To File"<<endl;
	cout<<"Filename:";
	gets(filename);
	fflush(stdin);
	ofstream fout;
	fout.open(filename, ios_base::out | ios_base::binary);
	cout<<"Start saving..."<<endl;
	fout.write((char *)list, sizeof(Contact) * size);
	fout.close();
	cout<<"saving done!"<<endl;
	return;
}

int main()
{
	int choice2;
	cout<<"~ - ~ - ~ - ~ - ~ - ~ - ~"<<endl
		<<"Welcome to UCSI@eContact"<<endl
		<<"~ - ~ - ~ - ~ - ~ - ~ - ~"<<endl<<endl;

	LogIn();

	cout<<"Enter your choice (1~4)"<<endl
		<<"1 - Load saved contacts"<<endl
		<<"2 - Add new contact"<<endl
		<<"3 - Save Contact"<<endl
		<<"4 - LogOut"<<endl;

	cin>>choice2;

	if (choice2==1)
	{
	LoadFromFile();
	}

	else if (choice2==2)
	{
	AddContact();
	}

	else if (choice2==3)
	{
	SaveToFile();
	}

	else if (choice2==4)
	{
	logout();
	}


	LoadFromFile();
	AddContact();
	SaveToFile();
	cout<<endl;
	
	getch();
	return 0;
}


another questions.in my void addcontact,
cout<<"Do you wish to continue?"<<endl
<<"1 - Yes"<<endl
<<"2 - No"<<endl;
cin>>answer;
how to continue it?i have no idea...
how can i make it return to main selection (1~4) after save file or load file or add new contact?

beside,if i change the for loop :> for (int i=0; i<size; i++) into for (int i=0; i<50; i++)
how to make it stop if i would like to stop after add 3 contact?

This post has been edited by nevermine: 27 March 2009 - 06:02 AM


Is This A Good Question/Topic? 0
  • +

Replies To: help:fail load VOID after an if clause

#2 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 988
  • View blog
  • Posts: 5,135
  • Joined: 28-September 06

Re: help:fail load VOID after an if clause

Posted 27 March 2009 - 06:17 AM

Welcome to DIC!

One thing at a time.

Try adding this line of debug code to your function:
void LoadFromFile()
{
	/* debug */ cout << "entered LoadFromFile" << endl;
	char filename[100], temp[10];
		...



Then run your code to prove that you are successfully entering the function when you choose option 1.

So your original question isn't making sense as written.
Perhaps you meant something else?

There is the question about what "temp[10]" is used for.
Can you clarify that?
Was This Post Helpful? 0
  • +
  • -

#3 nevermine  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 10-March 09

Re: help:fail load VOID after an if clause

Posted 27 March 2009 - 06:42 AM

View Postjanotte, on 27 Mar, 2009 - 05:17 AM, said:

Welcome to DIC!

One thing at a time.

Try adding this line of debug code to your function:
void LoadFromFile()
{
	/* debug */ cout << "entered LoadFromFile" << endl;
	char filename[100], temp[10];
		...



Then run your code to prove that you are successfully entering the function when you choose option 1.

So your original question isn't making sense as written.
Perhaps you meant something else?

There is the question about what "temp[10]" is used for.
Can you clarify that?


thx Janotte!
after adding the /* debug */,it is still the same problem...asking me to choose whether Abort/Retry/Ignore.when i choose retry(press retyr to debug..),it show out "Dont send smth... -.-" "
but when i choose ignore the program still can run,just showing that "error,file not found",but then can key in a file name again

oops,i forgot to delete the temp[10] thing.

besides,how can i make it stop in adding new contact? actually i'd like to have a 50 maximum contact number in 1 file.so i change the for (i=0,i<50,i++)
for example,lets say,if i adding contacts untill 3rd contact and i feel enought and would like to stop,how to make it?
i googled 1 example from here
cout << " Would ou like to add additonal Information? [Y/N]" << endl;
		cin >> userchoice;
	   if(userchoice== 'Y')
	{
		goto Inputsection;
	   
	}


how about if userchoice=="N"?
but it cannot goto a void...

This post has been edited by nevermine: 27 March 2009 - 06:44 AM

Was This Post Helpful? 0
  • +
  • -

#4 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 988
  • View blog
  • Posts: 5,135
  • Joined: 28-September 06

Re: help:fail load VOID after an if clause

Posted 27 March 2009 - 06:50 AM

You are confusing me with so much all at once.

Just go slower and let's deal with one issue at a time.

When you said "....but it is fail to select either 1 or 3,why?" What did you mean? It seems my guess as to what you meant was wrong.

Please just choose one problem and see if we can solve that one then move on to the next.

Carefully describe one problem/bug and let's see if we can help fix one at a time.
Was This Post Helpful? 0
  • +
  • -

#5 nevermine  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 10-March 09

Re: help:fail load VOID after an if clause

Posted 27 March 2009 - 07:01 AM

View Postjanotte, on 27 Mar, 2009 - 05:50 AM, said:

When you said "....but it is fail to select either 1 or 3,why?" What did you mean? It seems my guess

im sorry... :P

it mean when the program come to menu, if i select either 1 or 3,window will prompt out"Microsoft Visual C++ debug library" << this thing

program: C:\eContact.exe
file: fopen.c
line 54

expression: #file!=T('\o')

(abort) (retry) (ignore)
if i choose retry,program close...but if choose ignore,still can run but will cout<<"ERROR! File not found." and cin file name again
Was This Post Helpful? 0
  • +
  • -

#6 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 988
  • View blog
  • Posts: 5,135
  • Joined: 28-September 06

Re: help:fail load VOID after an if clause

Posted 27 March 2009 - 07:04 AM

View Postnevermine, on 27 Mar, 2009 - 06:01 AM, said:

it mean when the program come to menu,


Do you mean when it displays this menu?
	cout<<"Enter your choice (1~4)"<<endl
		<<"1 - Load saved contacts"<<endl
		<<"2 - Add new contact"<<endl
		<<"3 - Save Contact"<<endl
		<<"4 - LogOut"<<endl;


Was This Post Helpful? 0
  • +
  • -

#7 nevermine  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 10-March 09

Re: help:fail load VOID after an if clause

Posted 27 March 2009 - 07:09 AM

View Postjanotte, on 27 Mar, 2009 - 06:04 AM, said:

View Postnevermine, on 27 Mar, 2009 - 06:01 AM, said:

it mean when the program come to menu,


Do you mean when it displays this menu?
	cout<<"Enter your choice (1~4)"<<endl
		<<"1 - Load saved contacts"<<endl
		<<"2 - Add new contact"<<endl
		<<"3 - Save Contact"<<endl
		<<"4 - LogOut"<<endl;



yup~ :)
problem come out either cin 1 or 3...
Was This Post Helpful? 0
  • +
  • -

#8 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 988
  • View blog
  • Posts: 5,135
  • Joined: 28-September 06

Re: help:fail load VOID after an if clause

Posted 27 March 2009 - 07:18 AM

If you choose option 1 do you see the debug code we added earlier printed on the screen before the error message?

Here is my output:
Main
(1) Login
(2) Quit
1
please enter password
Password: 1234
Enter your choice (1~4)
1 - Load saved contacts
2 - Add new contact
3 - Save Contact
4 - LogOut
1
entered LoadFromFile
Load from file
warning: this program uses gets(), which is unsafe.
Filename: ERROR! File not found.
entered LoadFromFile
Load from file
Filename:

This post has been edited by janotte: 27 March 2009 - 07:25 AM

Was This Post Helpful? 0
  • +
  • -

#9 nevermine  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 10-March 09

Re: help:fail load VOID after an if clause

Posted 27 March 2009 - 07:27 AM

get it~
here the error message
eContact.obj : error LNK2001: unresolved external symbol "void __cdecl SaveToFile(void)" (?SaveToFile@@YAXXZ)
eContact.obj : error LNK2001: unresolved external symbol "void __cdecl AddContact(void)" (?AddContact@@YAXXZ)
Debug/eContact.exe : fatal error LNK1120: 2 unresolved externals

if add/*debug/* at void savetofile

below error found
C:\C++\eContact\eContact.cpp(223) : fatal error C1071: unexpected end of file found in comment

This post has been edited by nevermine: 27 March 2009 - 07:30 AM

Was This Post Helpful? 0
  • +
  • -

#10 nevermine  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 10-March 09

Re: help:fail load VOID after an if clause

Posted 27 March 2009 - 07:33 AM

warning: this program uses gets(), which is unsafe.
so i need to change them into getline(cin,filename) <<?
Was This Post Helpful? 0
  • +
  • -

#11 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 988
  • View blog
  • Posts: 5,135
  • Joined: 28-September 06

Re: help:fail load VOID after an if clause

Posted 27 March 2009 - 07:33 AM

Worry less about the error messages themselves.

First look at where they appear in the context of your code and its output.

If you have a look at the output I am getting you will see that your code is faulty.
Before I ever get to enter a file name it enters the if test and prints you error message.
Your code isn't working properly and is requesting a file providing a null or line end or some other dud character as the file name.

The error message is not your first problem.

Try using something better than "gets" to collect the file name.

I have to sleep now.
I hope others will help you from here on.

This post has been edited by janotte: 27 March 2009 - 07:34 AM

Was This Post Helpful? 0
  • +
  • -

#12 nevermine  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 10-March 09

Re: help:fail load VOID after an if clause

Posted 27 March 2009 - 07:34 AM

warning: this program uses gets(), which is unsafe.
so i need to change them into getline(cin,filename) <<?
Was This Post Helpful? 0
  • +
  • -

#13 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 988
  • View blog
  • Posts: 5,135
  • Joined: 28-September 06

Re: help:fail load VOID after an if clause

Posted 27 March 2009 - 07:37 AM

View Postnevermine, on 27 Mar, 2009 - 06:34 AM, said:

warning: this program uses gets(), which is unsafe.
so i need to change them into getline(cin,filename) <<?

I definitely would.

gets is a 'bad thing'

View Postnevermine, on 27 Mar, 2009 - 06:27 AM, said:

if add/*debug/* at void savetofile

below error found
C:\C++\eContact\eContact.cpp(223) : fatal error C1071: unexpected end of file found in comment

/* comment */ NOT /* comment /*
Was This Post Helpful? 0
  • +
  • -

#14 nevermine  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 10-March 09

Re: help:fail load VOID after an if clause

Posted 27 March 2009 - 07:50 AM

ok,i will try to change them into getline.
then how about stop adding new contact? actually i'd like to have a 50 maximum contact number in 1 file.so i change the for (i=0,i<50,i++).but how about if i'd like to stop at 3rd contact i keyed in?
Was This Post Helpful? 0
  • +
  • -

#15 nevermine  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 10-March 09

Re: help:fail load VOID after an if clause

Posted 27 March 2009 - 08:24 AM

what does this mean huh?
fatal error C1071: unexpected end of file found in comment
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2