Reading in a newline with getline loop.

I'm supposed to use getline to read in a joustdata.txt, and to ensure that the numbers read from the file are converted to integers, I'm supposed to add 99 to each number printed out. The joustdata.txt file looks like this:

Stormy|20|Sword|10|2|Mace|12|3
Starry|25|Morning Star|13|4|Pollaxe|9|1

the output is supposed to look like:

Stormy
119
Sword
109
101
Mace
111
102


Starry
124
Morning Star
112
103
Pollaxe
108
100

I want to do it with a loop rather than typing in getline for each string.
the problem I have is that my code skips over the newline and doesn't print starry. What can I do to fix it, any pointer is appreciated thanks:)

Here's my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<fstream>
#include<iostream>
#include<string>
#include<stdexcept>
using namespace std;

 int main() {
   ifstream in("joustdata.txt");
   string s;
   while (getline(in, s, '|')) 
     {
     try 
       {
       int x = stoi(s);
       cout << x+99 << endl;
       } 
       catch (invalid_argument const &e)
         {
          cout << s << endl;
         }
     }
  return 0;
 }    


Last edited on
Use two loops, say
1
2
3
4
5
6
7
8
// read a whole line
while ( getline(in,s) ) {
  istringstream is(s);
  string t;
  // read | separated tokens from line
  while ( getline(is,t,'|') ) {
  }
}
Thank you for responding so quickly. So would I keep everything else the same and just add a new loop on top? and if so when I try to compile it gives me an incomplete type error.

error:variable 'std::istringstream is' has initializer but incomplete type
istringsream is (s);
^

Last edited on
Did you include all you need?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
  ifstream in;
  string s;
  // read a whole line
  while ( getline(in,s) ) {
    istringstream is(s);
    string t;
    // read | separated tokens from line
    while ( getline(is,t,'|') ) {
    }
  }
}
yes I have all of that but it only prints out the second half

This is what I have:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 1#include<fstream>
  2 #include<iostream>
  3 #include<string>
  4 #include<stdexcept>
  5 #include<sstream>
  6 using namespace std;
  7
  8 int main() {
  9   ifstream in("joustdata.txt");
 10   string s;
 11   while (getline(in,s)) {
 12     istringstream is(s);
 13     string t;
 14   while (getline(in,t,'|')) {
 15     try {
 16       int x = stoi(t);
 17       cout << x+99<< endl;
 18     } catch (invalid_argument const &e) {
 19       cout << t << endl;
 20     }
 21   }
 22   }
 23   return 0;
 24 }

> yes I have all of that but it only prints out the second half
No you didn't.

Your inner getline reads from the file stream, NOT the string stream.
Oh, yeah, I'm sorry I didn't pay attention to that, thank you so much.
Registered users can post here. Sign in or register to post.