Code not doing what it's supposed to. (String manipulation)

Here first is the assignment I am having to complete.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
You should use either the C++ string object or C-String for this problem. Read content from a file.
The file format is such that in each line: 1) there are three columns, and 2) each column is delimited
by |.
Output each line to stdout. The first column width should be 15, the second column width should be
8, and the third column width should be 6. If the line starts with a #, you should skip it. Moreover,
the second column should be displayed to 2 decimal places.
There might be more than 3 rows; you cannot assume only 3 rows. However, you can assume that
there are only 3 columns.
Sample input
#Test file one
C1|c2|c4
Xyz|1.2|100
Abc|5.5|200
Sample output (output should be like this. Note extra line after first row only)
C1 c2 c4
Xyz 1.20 100
Abc 5.50 200


Here then is the code I've worked up.

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
  1 #include <iostream>
  2 #include <iomanip>
  3 #include <fstream>
  4 using namespace std;
  5
  6 struct Line
  7 {
  8    string one, two, three;
  9 };
 10 istream &operator >> (istream &is, Line &l)
 11 {
 12    is >> l.one;
 13    is.ignore(1, '|');
 14    is >> l.two;
 15    is.ignore(1, '|');
 16    is >> l.three;
 17    return is;
 18 }
 19 int main()
 20 {
 21    string s;
 22    int i = 0, numSign;
 23    cin >> s;
 24       Line l;
 25       ifstream input("test.txt");
 26       while(input >> l)
 27       {
 28          if(s[0] == '#')
 29          {
 30             continue;
 31          }
 32          cout << setw (15) << l.one;
 33          cout << setw (8) << l.two;
 34          cout << setw (6) << l.three;
 35
 36          if(i = 0)
 37          {
 38          cout << "\n";
 39          i++;
 40          }
 41       }
 42    }


The problem I've run in to is that when I run it, nothing is printed out. I just get the bash commandline back and that's it. When I had line 28 reading "if(s[0] == numSign" (the ASCII code for #), it spit out "#Test file one" and then had the rest of the sample input as one long line smooshed together. I'm not sure what exactly is wrong, I believe it to be the continue; command as I think that's breaking the entire while loop and not going back to the beginning.
s is only updated once.

So if s[0]=='#' is ever true once, it is always true, and then the rest of the while loop will never execute.

On line 36, you probably want to use == instead of =.

The logic in operator>> is wrong.
Last edited on
Topic archived. No new replies allowed.