read input String & Integer

Hi,

For my input I have in one line a string and an integer separated by ';'.
For example: Age: 24

So how can I read the line so that 'Age' is read as a string and '24' as an integer and I want to print the same line on the screen again?

Thanx!
I'm not quite understanding your question. Could you post the code sending the input?
Use the getline() string function.

1
2
3
4
5
6
7
8
9
10
11
12
stringstream ss( "Age: 24\n IQ: 120\n Miles Jogged: 19" );
while (ss) {
  string name;
  int value;

  getline( ss, name, ':' );

  ss >> value;
  ss.ignore( numeric_limits<streamsize>::max(), '\n' );

  cout << name << ": " << value << endl;
  }


Hope this helps.
Last edited on
Thank you Duoas.

Can you explain what ss.ignore(numeric_limits<streamsize>::max(), '\n'); does?

You use stringstream ss, but if the input is from the keyboard or from a file?
Hi Gzero,

The input is just either from the keyboard "Age: 20" or from a file stating "Math: 6"

cout << "Enter subject and point separate with ':' " << endl;
I only used a string stream for example. It is still just another input stream, so you can substitute any istream (like cin).

The ignore function reads and ignores characters until after a newline or EOF is encountered.
I tried to replace the stringstream with input from the keyboard. Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
char ss;

cout << "Please insert something separated with : " << endl <<;
cin >> ss;
while (ss) {
  string name;
  int value;

  getline( ss, name, ':' );

  ss >> value;
  ss.ignore( numeric_limits<streamsize>::max(), '\n' );

  cout << name << ": " << value << endl;
  }


The following error appeared:
1
2
error: no matching function for call to 'getline(char&, std::string&, char)'
error: request for member 'ignore' in 'ss', which is of non-class type 'char'


What does this mean and how can I resolve it?
This very site has an excellent and complete C++ reference. If you look up getline() and istream you'll see why the compiler is complaining.

Try:
1
2
3
4
5
6
7
8
9
10
string name;
int value;

cout << "Please enter a string: integer pair, separated with : ";

getline( cin, name, ':' );
cin >> value;
cin.ignore( numeric_limits<streamsize>::max(), '\n' );

cout << "You entered " << name << " : " << value << endl;


Hope this helps.
Thank you Duoas. You are very helpful.

My next step was to read the input from a file. That works fine. It also reads the string and ignore the ':' but the output of the value is strange. I don't know what the output of the value means.

Here is 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
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
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>
using namespace std;

int main() {
  string name;
  //char nr[40];
  double value;
  double amount;
  ifstream ins1; //open text1.txt for input
  ifstream ins2; //open text2.txt for input

  /* test1.txt
     Milk: 15.00
     Butter: 20.00
  */
  ins1.open ("test1.txt"); //opens file, exit if fail
  if(ins1.fail()) { //if opening fails then end program
    cout << "Input file opening failed." << endl;
    exit(1); //exit function from cstdlib library
  }

  if(ins1.is_open()) {
    while(! ins1.eof()) {
      cout << "Test1" << endl;

      getline(ins1, name, ':');
      cout << name << endl;

      cout << "Test2" << endl;

      //nr = ins1.get();
      value = ins1.get();
      cout << "Value: " << value << endl;

      cout << "Test3" << endl;
      //amount = atof(nr);
    
      ins1.ignore(numeric_limits<streamsize>::max(), '\n');
      
      cout << "Name: " << name << "\t Value: " << value << endl;
    }
  ins1.close();
  }
  else cout << "Unable to open file";
  return 0;
}


The output is:
1
2
3
4
5
6
7
8
9
10
11
12
Test1
Milk
Test2
Value: 32
Test3
Name: Milk            Value: 32
Test1
Butter
Test2
Value: 32
Test3
Name: Butter       Value: 32


As you can see the program reads the 2 lines perfectly. I just don't understand the output of the value; it should be 15.00 and 20.00. Any help is appreciated. Thanx!


I'm sorry but the Test1, Test2, and Test3 was for debugging
Why have you changed ins1 >> value; into value = ins1.get(); ? It is the cause of your problem.
Thank you Duoas! My problem is solved.
Topic archived. No new replies allowed.