outputting two getline'd strings
I'm having a problem with my program, I can't get it to output properly
when I use getline() to input two strings to my program then try to output them, its not working
heres the output code:
1 2 3 4 5 6 7 8
|
for (int y=0;y < P_array[z].no_tags; y++) //all tags on each poster
{
if (((P_array[z].tags[y]) == query) && (!already_found))
{
cout<<P_array[z].name<<" by "<<P_array[z].author<<endl;
already_found = true;
}
}
|
I need the output to look like
Book_name by Author
but when I run the code I get
by Author.. . truncating the book name.
Edited to add: It works if I manually input all the data, but when I input it from a file it doesn't work.
The input file is formatted like such:
Twoword name
Twoword author
3
word
word
word
twoword name
twoword author
I'm guessing its something to do with the newline? how do I fix this?
Last edited on
The only explanation is that P_array[z].name is an empty string.
Are you sure it contains the book name? Can you post the code where you're setting it?
Also, just a curiosity, are these actual strings? or at they char arrays?
P_array[z].name works.
if I seperate the code into
1 2 3 4 5 6 7 8 9
|
for (int y=0;y < P_array[z].no_tags; y++) //all tags on each poster
{
if (((P_array[z].tags[y]) == query) && (!already_found))
{
cout<<P_array[z].name<<endl;
cout<<" by "<<P_array[z].author<<endl;
already_found = true;
}
}
|
it works.
but here's the input code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
for (int i=0; i<posts; i++)
{
getline(cin, waste);
getline(cin, temp_n);
cout<<"name: "<<temp_n<<endl;
getline(cin, temp_a);
cout<<"author: "<<temp_a<<endl;
P_array[i].set(temp_n,temp_a); //need to input tags
cin>>P_array[i].no_tags;
cout<<"number tags: "<<P_array[i].no_tags<<endl;
P_array[i].tags = new string[P_array[i].no_tags];
for (int k=0; k<P_array[i].no_tags; k++) //to set tags
{
cin>>P_array[i].tags[k];
cout<<"current tag: "<<P_array[i].tags[k]<<endl;
}
}
|
line 8 here sets the name and author
and yes, they are actual strings
Last edited on
Topic archived. No new replies allowed.