3

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?

3
  • 3
    getline is the right start. Keep working on it. Maybe there's some way to parse a string into an integer? If you get stuck, try applying a web-based search engine to parts of the problem. Commented Apr 18, 2016 at 0:31
  • You could use a counter which, when odd/even, converts the std::string from getline to an int Commented Apr 18, 2016 at 0:33
  • @KerrekSB Do I need to convert a string to an integer or could I just set the given line equal to an int variable and it will do the rest? Commented Apr 18, 2016 at 0:42

5 Answers 5

0

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I'm now segfaulting when trying to find a solution to this. How could I possibly be doing that when only reading from a file?
I don't know, you haven't provided any sample code. If you are storing inputs in arrays, these may not have been initialised, or may not be large enough for the amount of data you are trying to store?
0

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);

Comments

0

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;
}

Comments

0

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;
}

Comments

0

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 !!!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.