cout string order
May 9, 2012 at 9:37pm UTC
Hello,
(A) I am new and stuck on being able to cout strings that are found from "getline" & "find". I can cout the same order from the input file, but I can't manipulate that order. Notice that the input data is not in identical order except for the "Band".
(B) When I uncomment the two sections I commented out AND comment out the "cout"'s in the four "if" statements, why doesn't A,B,C,D print?
input.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
=============
Band=U2
Genre=Rock
Albums=12
Members=Four
=============
Band=Metallica
Genre=Hard Rock
Members=Four
Albums=9
=============
Band=Skrillex
Members=One
Albums=7
Genre=Electronica
=============
main
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
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
ifstream file("input.txt" );
if (!file.is_open())
{
cerr << "Failed to open file." << endl;
return (EXIT_FAILURE);
}
// string Atemp,Btemp,Ctemp,Dtemp,A,B,C,D;
for (string line;(getline(file,line));)
{
if ((line.find("Band" ) != string::npos))
{
const size_t start_pos = line.find("Band" );
string Atemp = line.substr(start_pos+5);
const size_t stop_pos = Atemp.find("\n" );
string A = Atemp.substr(0, stop_pos);
cout << endl << A << "-" ;
}
if ((line.find("Genre" ) != string::npos))
{
const size_t start_pos = line.find("Genre" );
string Btemp = line.substr(start_pos+6);
const size_t stop_pos = Btemp.find("\n" );
string B = Btemp.substr(0, stop_pos);
cout << B << "-" ;
}
if ((line.find("Albums" ) != string::npos))
{
const size_t start_pos = line.find("Albums" );
string Ctemp = line.substr(start_pos+7);
const size_t stop_pos = Ctemp.find("\n" );
string C = Ctemp.substr(0, stop_pos);
cout << C << "-" ;
}
if ((line.find("Members" ) != string::npos))
{
const size_t start_pos = line.find("Members" );
string Dtemp = line.substr(start_pos+8);
const size_t stop_pos = Dtemp.find("\n" );
string D = Dtemp.substr(0, stop_pos);
cout << D << ";" ;
}
// cout << A << "-" << B << "-" << C << "-" << D << ";";
}
cout << endl;
system("PAUSE" );
return 0;
}
cout
1 2 3
U2-Rock-12-Four;
Metallica-Hard Rock-Four;9-
Skrillex-One;7-Electronica-
Thanks in advance for any help.
May 9, 2012 at 10:04pm UTC
(A)
you could consider adding the results in a vector.
1 2 3
vector<string> v;
while (getline(file, line));
v.push_back(line);
after you've done that you can use the vector to manipulate your input.
You could for example create a for-loop like this:
1 2
for (int i = 0; i < v.size(); i++)
//add your loop here.
since you can now select your seperate elements (v[8] works too for example), you can create a function to show it in any order you like.
---------
(B)
This is because you are defining the strings again in the if-scope.
remove the words "string" in these positions and it will work.
you basically did this:
1 2 3 4 5 6
int i = 12;
if (i > 3) {
int i = 6;
cout << i << endl;
}
cout << i << endl;
output:
Last edited on May 9, 2012 at 10:07pm UTC
Topic archived. No new replies allowed.