John
John

Reputation: 449

Reading string then int line by line in C++

So, I have a file that contains a pattern of a string then an int alternating line by line.

Something like this:

John McClane
30
James Bond
150
Indiana Jones
50

In this example, I would set John McClane to a string variable and then 30 to an integer variable. My issue is dealing with two types. I want to use getline(), but that only works with strings.

Is there an efficient or "right" way of doing this?

Upvotes: 3

Views: 2827

Answers (5)

Md. Monirul Islam
Md. Monirul Islam

Reputation: 731

You can try like this to read a string then an int alternating line by line.

#include<iostream>
#include<string>
#include<cstdio>
using namespace std;

int main()
{
    string name;
    int number;
    freopen("input.txt", "r", stdin);
    while (getline(cin, name))
    {
        cin >> number;
        /*
        process the input here
        ...
        ...
        */
        getline(cin, name); // just to read the new line and/or spaces after the integer
        //getchar();   //you can use getchar() instead of getline(cin, name) if there is no spaces after the integer
    }
    return 0;
}

Thanks !!!

Upvotes: 0

user6094958
user6094958

Reputation:

I don't know if this fully works as i didn't tested it however it just compiles fine and answer should be something like this i think.

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>

using namespace std;

int main()
{
    int counter = 0;
    int number;
   string test_string;
  ifstream myfile ("example.txt");
  if (myfile.is_open())
  {
    while ( getline (myfile,test_string) )
    {
     cout << test_string << '\n';
      ++counter;

      if(counter % 2 == 0 ){
            number = atoi(test_string.c_str()); 
          cout << number << '\n';            
      }else{

           cout << test_string << '\n';
      }
    }


    myfile.close();
  }

  else cout << "Unable to open file";

    return 0;
}

Upvotes: 0

bstadt
bstadt

Reputation: 146

One thing about C++ is that there are a large number of ways to accomplish any one task. One way to get integers from strings is to use a stringstream. There is a tutorial on stringstreams here

As for your problem with reading the alternating file, consider the following pseudocode:

boolean isInt  = false;
while(fileIsNotOver) {
        //getline
    if(isInt) {
        //use stringstream to get int here
    } else {
        //do whatever with the name here
    }
    isInt = !isInt;
}

Upvotes: 0

Griz
Griz

Reputation: 65

You could make a variable like this

string ibuf;

Then convert it to an integer doing this

getline(cin, ibuf);
(Whatever your int variable is) = strtol(ibuf.c_str(), NULL, 10);

Upvotes: 0

Dimpl
Dimpl

Reputation: 942

There are a number of approaches you could try.

  • Get string input, and convert to an integer if valid
  • Convert every second string to an integer
  • Try to read an integer when you expect one (just using cin >> in;). If you want a robust program, you can check validity with cin.good()

I don't know if there is a "right" way of doing this per say, but it's not a very taxing operation, so whatever you choose should be fine.

Upvotes: 0

Related Questions