LOOP!

Hello,
Programming troubles.
So here is the deal. In the program below I'm supposed to make some functions which I did and use my new found knowledge of functions to output from a file a input which is like the following.
2
Sally Robertson
456 Cross Ave East
Somewhere Else, New Mexicio 91991
Where the number 2 is a print selection. There are three total choice numbers 1,2,3. The choices work themselves the trouble is when I go to loop in main. I want to continue this process for how ever main addresses and lines I have.
This is a copy of my input I'm just using two groups I will have more in the future.
1
Sam Alexander Smith
1234 S. Main Street
Anywhere, U.S.A. 99999-0000
2
Sally Robertson
456 Cross Ave East
Somewhere Else, New Mexicio 91991


This is my code;

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


// Global constants
ifstream fin;

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 display programmers name
void Programmer()
{
cout << "Student: Jordan smith" << endl;
cout << '\n' << 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;

cout<<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 choice;


}


//Purpose to display print selection
void Displayprintselection( int Printchoice)
{
if(Printchoice!=-1)
cout << "Your print selection is : "<< Printchoice <<"\n" << endl;

else if( Printchoice=-1)
cout << "You have selected an invalid printing option." << endl;
}



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

while(str1.size()==0&&str1.size()!=1)
getline(fin,str1);

while
(str2.size()==0)
getline(fin,str2);

while(str3.size()==0)
getline(fin,str3);

}

void main()
{

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

//Algorithm
Programmer();

//Calling functions

while(!fin.eof())
{
Printchoice=PrintSelection();
GetAddress(str1,str2,str3);
Echo(str1,str2,str3);
Displayprintselection(Printchoice);

//Control of selection
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");

}

}
OK, so let me tell you about the problem. It reads the file once correctly, stops, then after I hit any key the loop outputs the next address. Which is not correct it is a repetition of the first except the print selection is properly chosen which makes the output different but the strings three lines of address stay the same. For instance look at the input file above it reads the number 2 but outputs the appropriate formatting with the same address as the first.

I hope that I have explained myself sufficiently if not just ask me and I can try and rephrase what I was explaining. And yes I know that void main() is not correct it should be int main() or something but it compiles and its what my teacher uses so lets just overlook this ha.
Thanks

Last edited on
Right, here is the deal: You edit your post so it uses code tags - the <> button on the right

If you do that, then we can help you.
What does that mean ?
It means select all your code using the mouse, and then push the button on the right that looks like this: <>
Never mind I figured the loop question I'm stuck though on formatting this box


It is supposed to trim it if it is to long and all that it does is add an extra space or if it is over the limit it doesn't trim it. But I have used setw and substring so Why would it do this ?
Here is the function


// 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;






There are three of these one for each type of string. But they are all the same and all producing the same result. The line with the else if(str1.length()>=LIMIT) is the problem and the code following that else if.


LIMIT =60 and LIMIT1=56.
Thanks
I am not going to answer until you use the code tags.

Am happy to help, but it would be better if you co-operated a bit.
nevermind ...sorry got it again it was the left thing that was messing it up but does anyone know why?
Thanks
Topic archived. No new replies allowed.