Formatting errors with loops and io stream objects

I have the code right for the most part, just my formatting is off. My loops aren't working exactly as i want them to and i am getting some unwanted things being repeated on the screen. Any suggestions would be appreciated.

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
 #include<iomanip>
#include<iostream>
#include<string>
#include<cmath>
#include<fstream>
#include<cstdlib>
 
using namespace std;

int main()
{
	string info;
	ofstream file1;
	ifstream file2;
	string ans; 
	string repeat = "yes";

	while(repeat == "yes")
	{
	
	file1.open("original.txt.");


	cout<<"Please enter your name and street address"<<endl<<endl;
	
	getline(cin, info);
	file1<<info;
	system("cls");
	
	
	cout<<"Would you like to update your street address?"<<endl;
	cin>>ans;
	
	if(ans == "yes")
	{

		file2.open("original.txt.");
		cout<<endl<<"Your current info is "<<info<<endl<<endl;
		
		file1.open("changed_info.txt");
		cout<<"Please input your name and updated address"<<endl;
		getline(cin,info);
	}

	else
	{
	
	cout<<"Thank you for using this program"<<endl;
	cout<<"Do you want to run this again?"<<endl;
		cin>>repeat;
	}

	}
	system("pause");

	return 0;
}
closed account (SECMoG1T)
Line 21 and 40 its never a good idea to attach a stream to two different source especially when both are used in alternation , please check that out
Ok, i just changed that actually. My problem now is that my if statement in line 34 doesn't work correctly, it outputs the cout statements but then skips down to the else part without letting me input anything. I tried putting a while loop in it, like below, but now it just repeats what is in the while loop over and over. Which would be better and why am I getting these errors?

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
#include<iomanip>
#include<iostream>
#include<string>
#include<cmath>
#include<fstream>
#include<cstdlib>
 
using namespace std;

int main()
{
	string info;
	ofstream file1;
	ifstream file2;
	ofstream file3;
	string ans;
	

	
	file1.open("original.txt.");

	if(file1.fail())
	{
		cout<<"File could not be opened, check for correct name and path"<<endl;
	}

	cout<<"Please enter your name and street address"<<endl<<endl;
	
	getline(cin, info);
	file1<<info;
	system("cls");
	
	
	cout<<"Would you like to update your street address?"<<endl;
	cin>>ans;

	while(ans == "yes")
	{
		file2.open("original.txt.");
		cout<<endl<<"Your current info is "<<info<<endl<<endl;

		file3.open("changed_info.txt");

	
		cout<<"Please input your name and updated address"<<endl;
		
		getline(cin,info);
		file3<<info;
		
	}

	cout<<"Thank you"<<endl;

	
	system("pause");
	return 0;
}
Nevermind, pretty sure i fixed it.
Ok, last question. I put a while loop around the whole program so that I could repeat it again but when i want to repeat it, it outputs the if statement for failing to open the file original.txt. Why is that?

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
#include<iomanip>
#include<iostream>
#include<string>
#include<cmath>
#include<fstream>
#include<cstdlib>
 
using namespace std;

int main()
{
	string info;
	ofstream file1;
	ifstream file2;
	ofstream file3;
	string ans;
	string repeat = "yes";

	while(repeat == "yes")
	{
	
	
	
	file1.open("original.txt.");

	if(file1.fail())
	{
		cout<<"File could not be opened, check for correct name and path"<<endl;
	}

	cout<<"Please enter your name and street address"<<endl<<endl;
	
	getline(cin, info);
	file1<<info;
	system("cls");
	
	
	cout<<"Would you like to update your street address?"<<endl;
	getline(cin, ans); 

	if(ans =="yes")
	{
	
		file2.open("original.txt.");
		cout<<endl<<"Your current info is: "<<info<<endl<<endl;

		file3.open("changed_info.txt");

	
		cout<<"Please input your name and updated address"<<endl;
		
		getline(cin,info);
		file3<<info;
		
		
	}

	cout<<"Thank you for using this program"<<endl;
	cout<<"Would you like to run this again?"<<endl;
	getline(cin, repeat);

	}
	system("pause");
	return 0;
}
closed account (SECMoG1T)
I guess it's because of the extension you provide for the file
1
2
"original.txt." it should be
 "original.txt"  /// line 44 and 24  
Last edited on
Topic archived. No new replies allowed.