SOS

This is driving me nuts!
I finally got the box to output but now I have the problem that nothing is being showing up. It's like there is something wrong with my function to read from the file. I can't figure it out.
This is my input
3
Jordan Lillie
123 Main Street
Anywhere MI, 9999




This is my code;


#include<iomanip>
#include<iostream>
#include <string>
#include <fstream>
using namespace std;


// Global constants
fstream fin("input.txt");

const int LIMIT=60;
const int LIMIT1=56;




// Purpose: To print formatted address
void PrintFormattedAddress(string str1,string str2,string str3)

{

cout<<setw(LIMIT)<<setfill('*')<<('*')<<endl;

// Outputs string 1
{

if(str1.length()<LIMIT)
cout<<"| "<<left<<setw(LIMIT1)<<setfill(' ')<<str1<<" |"<<endl;
else if(str1.length()>=LIMIT)
cout<<"| "<<left<<setw(LIMIT1)<<setfill(' ')<<str1.substr(0,LIMIT1)<<" |"<<endl;

}

//Output string 2


if (str2.length()>=LIMIT)
cout<<"| "<<str2.substr(0,LIMIT1)<<" |"<<endl;

else if(str2.length()<LIMIT)
cout<<"| "<<left<<setw(LIMIT1)<<setfill(' ')<<str2<<" |"<<endl;





//Outputs string 3

{

if(str3.length()<LIMIT)
cout<<"| "<<left<<setw(LIMIT1)<<setfill(' ')<<str3<<" |"<<endl;


else if(str3.length()>=LIMIT)
cout<<"| "<<left<<setw(LIMIT1)<<setfill(' ')<<str3.substr(0,LIMIT1)<<" |"<<endl;


}

cout<<setw(LIMIT)<<setfill('*')<<('*')<<endl;



}














//Purpose: To print address with no formatting
void PrintAddress( string str1, string str2, string str3)

{

cout<<"Address"<<endl;
cout<<"1: "<<str1<<endl;
cout<<"2: "<<str2<<endl;
cout<<"3: "<<str3<<endl;




}









//Purpose to echo input
void Echo(string str1,string str2, string str3)
{
cout<<"Echo"<<endl;
cout<<"1: "<<str1<<endl;
cout<<"2: "<<str2<<endl;
cout<<"3: "<<str3<<endl;



}












//Purpose: To obtain print selection
int PrintSelection()
{
int choice=0;
int nullchoice=-1;


{ fin<<choice;


if((choice>0)&&(choice<=3))

return choice;

else if((choice<0)&&(choice>3))
choice= nullchoice;

return nullchoice;



}


}











//Purpose to get three vailid lines of address
void GetAddress(string& str1, string& str2, string& str3)
{


getline(fin,str1);
getline(fin,str2);
getline(fin,str3);




}








void main()
{

//Variables

string str1="";
string str2="";
string str3="";
int Printchoice=0;

//Function testing

Printchoice=PrintSelection();
cout<<Printchoice<<endl;

GetAddress(str1,str2,str3);
cout<<str1<<endl;
cout<<str2<<endl;
cout<<str3<<endl;


PrintAddress(str1,str2,str3);


Echo(str1,str2,str3);


PrintFormattedAddress(str1,str2,str3);













system("pause");


}


My output is all the formatting and no strings to fill the places.
What is going on here.
Thanks

Try adding to the fstream constructor

fstream::in

This tells fstream that you will be extracting data.
So, there are a few things wrong. I'm going to do a quick response, because I should be out the door. D=

fstream fin("input.txt"); should be either fstream fin("input.txt", ios::in); or
ifstream fin("input.txt");.

In PrintSelection, fin<<choice; should probably be fin>>choice;, but you will have a leftover newline in the file stream that needs taken care of.

void main() should be int main(). Not sure how that compiled for you.

I assumed that all input was coming from the TXT file. You should have output at this point. What you will notice is that your address lines are shifted down a line because your first getline read the extra newline that I mentioned. I don't know of a good fix off the top of my head, maybe someone else can help.

Also, you should use fin.close() when you are finished with the file and should return from main.

Sorry for the rushed response, but I hope it helps.
Last edited on
Hey thanks I have fixed and moved on to a new problem which is LOOPS!



This is where I'm at
This is just the main part I didn't copy over the functions I will assume they are all fine because I have tested them. But the problem comes in when I try and use a loop.
Now I have thought that maybe I should use a do loop while loop and I just got some crazy infinite loop. I would like it to be after the algorithm part. If my functions worked previously as I tested them then am I correct to assume that the loop is my trouble?
Thanks






void main()
{

//Variables
fstream fin("inputpractice.txt");
string str1="";
string str2="";
string str3="";
int Printchoice=0;
fin.open("inputpractice.txt");


//Algorithm

Programmer();




Printchoice=PrintSelection();

GetAddress(str1,str2,str3);

Echo(str1,str2,str3);

Displayprintselection(Printchoice);


switch(Printchoice)
{

case 1: PrintAddress(str1,str2,str3);

cout<<"\n"<<endl;

break;

case 2:PrintFormattedAddress(str1,str2,str3);

cout<<"\n"<<endl;

break;

case 3: PrintAddress(str1,str2,str3);
cout<<'\n'<<endl;

PrintFormattedAddress(str1,str2,str3);

cout<<"\n"<<endl;

break;

default: cout<<" "<<endl;


}



















system("pause");




}












Topic archived. No new replies allowed.