File writing

Hello, I'm trying to make a program that you input your login info and it writes that info to a text file. Then, later on once I get my problem fixed, the prgram will read the info to the user. my code is as follows(the input part is a bit lengthy):
// Password Log

#include <iostream.h>
#include <fstream.h>
#include <string>
#include <time.h>

using namespace std;

ofstream login;
void user();
void pass();
void prgm();
char webprg[100];
char pswrd[100];
char name[100];

void Wait(int seconds)
{
clock_t endwait;
endwait = clock () + seconds * CLK_TCK;
while(clock() < endwait) {}
}


int main(){
int choice;
do{
cout << "----Welcome to the password archive!----\n";
cout << "Please select an option:\n";
cout << " 1: Input new login info\n 2: Look up login info\n 3: Quit\n";
cin >> choice;
switch(choice){
case 1:
prgm();
break;
case 2:
break;

}
}while(choice != 3);
system("CLS");
system("pause");
return 0;
}
void prgm(){
login.open("Login.txt");
system("CLS");
cout << "Please input name of program (spaces work):\n>";
cin.getline(webprg, 100);
login << webprg << endl;
cout << "Saving.";
Wait(1);
cout << ".";
Wait(1);
cout << ".";
Wait(1);
cout << ".";
Wait(1);
cout << "Info saved.";
Wait(1);
system("CLS");
login.close();
user();
}
void user(){
login.open("Login.txt");
cout << "Please input Username (spaces work):\n>";
cin.getline(name, 100);
login << name << endl;
cout << "Saving.";
Wait(1);
cout << ".";
Wait(1);
cout << ".";
Wait(1);
cout << ".";
Wait(1);
cout << "Info saved.";
Wait(1);
system("CLS");
login.close();
pass();
}
void pass(){
login.open("Login.txt");
cout << "Please input Password (spaces work):\n>";
cin.getline(pswrd, 100);
login << pswrd << endl;
cout << "Saving.";
Wait(1);
cout << ".";
Wait(1);
cout << ".";
Wait(1);
cout << ".";
Wait(1);
cout << "Info saved.";
Wait(1);
system("CLS");
login.close();
}
//End of code

What my problem is when I choose "Input new login info" it automatically inputs I have no clue, and calls next program. Sorry for the length post but I need the help. Thank you.
~Proggy
Please edit post and use code tags.
Try cin.ignore() before your cin.getline(char[], int) and see if that helps. I think it is reading in a newline character after either when the user types in a string or from the messages you print before each new prompt. cin.ignore() will flush the input stream of any newline characters.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <iostream.h>
#include <fstream>.h
#include <string>
#include <cstdlib>
#include <time.h>

using namespace std;

ofstream login;
void user();
void pass();
void prgm();
string webprg;
string pswrd;
string name;

void Wait(double );


int main()
{
	int choice;
	do
	{
		cout << "----Welcome to the password archive!----\n";
		cout << "Please select an option:\n";
		cout << " 1: Input new login info\n 2: Look up login info\n 3: Quit\n";
		cin >> choice;
		switch(choice)
		{
			case 1:
			login.open("Login.txt"); 
			prgm();
			break;
			case 2:
			break;

		}
	}
	while(choice != 3);
	system("cls");
	return 0;
}

void Wait(double seconds)
{
	double KeepTime = 0;
	clock_t start = clock();
	clock_t endwait = start;
	
	while(1) 
	{
		start = clock();
		KeepTime += (double)(start - endwait);
		endwait = start;

		if (KeepTime >= (double) (seconds * CLOCKS_PER_SEC))
		{
			break;
			return;
		}
	}
}

void prgm()
{
	system("cls");
	cin.ignore();
	cout << "Please input name of program (spaces work):\n>";
	getline(cin, webprg);
	login << webprg << endl;
	cout <<"\n";
	Wait(1);
	cout << "Saving.";
	Wait(0.5);
	cout << ".";
	Wait(0.5);
	cout << ".";
	Wait(0.5);
	cout << ".";
	Wait(0.5);
	cout << "Info saved.";
	system("cls");
	user();
}

void user()
{
	cout << "Please input Username (spaces work):\n>";
	getline(cin,name);
	login << name << endl;
	cout << "Saving.";
	Wait(1);
	cout << ".";
	Wait(1);
	cout << ".";
	Wait(1);
	cout << ".";
	Wait(1);
	cout << "Info saved.";
	Wait(1);
	system("CLS");
	pass();
}
void pass()
{
	cout << "Please input Password (spaces work):\n>";
	getline(cin, pswrd);
	login << pswrd << endl;
	cout << "Saving.";
	Wait(1);
	cout << ".";
	Wait(1);
	cout << ".";
	Wait(1);
	cout << ".";
	Wait(1);
	cout << "Info saved.";
	Wait(1);
	system("CLS");
	login.close();
}
Last edited on
Ok thank you Smac! It works great.
Topic archived. No new replies allowed.