0

I tried to convert int into a string , but it doesn't work I dont know why... here my Code:

#include <stdio.h>
#include <stdlib.h>  
#include <iostream>
#include <array>


using namespace std;


int main()
{
    struct Studentendaten {
        int matrnr;
        string name;
        string vorname;
        string datum;
        float note;
    };
    Studentendaten stud;
    array<Studentendaten,100> studArray ;   

  FILE * pFile;
  int ch; 
  int mnr;
  string sub;
  string sub1;
  string sub2;
  string sub3;
  string sub4;
  string sub5;

  int i = 0;
  int pos ;

  pFile=fopen("studentendaten.txt","r");  
  if (pFile==nullptr) 
  {
      perror ("Fehler beim öffnen der Datei");
  }
  else
  {       
    while (ch != EOF) 
    {
      ch = fgetc(pFile);
      sub += (char)(ch);
      pos = sub.find(";");
      if (pos != -1) 
      {
          sub1 = sub.substr(0,pos);       
          sub2  = sub.substr(pos+1,pos);
          sub3  = sub.substr(pos+1,pos);
          sub4  =sub.substr(pos+1,pos);
          sub5  =sub.substr(pos+1,pos);       
          mnr   =stoi(sub1);
          stud.matrnr = mnr;
          stud.name = sub2;
          stud.vorname = sub3;
          stud.datum = sub4
          stud.note = float(sub5);
      }
      if (ch == '\n') 
      {
          stud = {matrn,name,vorname,datum,note};
          studArray.[i] = stud;
          i++;
      }


      putchar(ch);
    }   
    fclose (pFile);
    }


  return 0;
}

I tried int mnr =stoi(sub1); as well as int mnr = atoi(sub1); where sub1 = "029383214" something like that.... why does it not work? Compiler complains...

MMMM
  • 3,320
  • 8
  • 43
  • 80

5 Answers5

2

Simply use a std::stringstream:

int parse_int( const std::string& str )
{
    std::stringstream ss( str );
    int value;

    if( ss >> value )
        return value;
    else
        throw;
}
Manu343726
  • 13,969
  • 4
  • 40
  • 75
1

I would rather use boost::lexical_cast if I were on your place

string stringValue = lexical_cast<string>(101);
int intValue = lexical_cast<int>("101");

http://www.boost.org/doc/libs/1_54_0/doc/html/boost_lexical_cast.html

MarekR
  • 2,410
  • 1
  • 14
  • 10
1

You can use stringstream:

#include <sstream>
...

// int to string

int intVar = 10;

std::stringstream out;
out << intVar;
std::string converted = out.str();

// string to int

std::string src = "555";
std::stringstream in(str);

int result = 0;
in >> result;

And check boost::lexical_cast as well.

knov
  • 116
  • 1
  • 2
1

Use std::to_string(int).

Reference.

ScarletAmaranth
  • 5,065
  • 2
  • 23
  • 34
  • I get an error when I use std::dateiLesen.cc:54:19: error: 'to_string' is not a member of 'std' stud.matrnr = std::to_string(sub1); – MMMM Nov 01 '13 at 15:24
  • 1
    @user2774480 You will need to upgrade your compiler to use the new `std::string` feature in C++x11 – Thomas Matthews Nov 01 '13 at 15:57
  • but I already entered for compiler: g++ -std=c++11 -Wall -c "%f" and for the make g++ -std=c++11 -Wall -o "%e" "%f" – MMMM Nov 01 '13 at 17:09
1

You can use std::to_string for simple cases or std::stringstream when you need more control over formatting (zero padding, hexadecimal etc.)

#include <iostream>
#include <sstream>
#include <iomanip>

using namespace std;

int main(int argc, const char * argv[]) {
    int value = 19;

    cout << to_string( value ) << endl;

    stringstream s1, s2;
    s1 << setfill('0') << setw(4) << value;
    s2 << "0x" << hex << setfill('0') << setw( 8 ) << value;

    cout << s1.str() << endl << s2.str() << endl;
}
Tomas Andrle
  • 13,132
  • 15
  • 75
  • 92