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.
#include<iomanip>
#include<iostream>
#include<string>
#include<cmath>
#include<fstream>
#include<cstdlib>
usingnamespace 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;
}
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?
#include<iomanip>
#include<iostream>
#include<string>
#include<cmath>
#include<fstream>
#include<cstdlib>
usingnamespace 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;
}
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?
#include<iomanip>
#include<iostream>
#include<string>
#include<cmath>
#include<fstream>
#include<cstdlib>
usingnamespace 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;
}