C/C++ Replacing stoi in C++ program for converting string to integer

  • Thread starter Thread starter elmessican
  • Start date Start date
  • Tags Tags
    C++ Program
AI Thread Summary
To replace the `stoi` function in a C++ program when the compiler does not support C++11, users can utilize `stringstream` to convert strings to integers. The discussion highlights the importance of correctly extracting the substring before conversion. The correct approach involves creating a temporary string variable to hold the substring, then using a `stringstream` to convert that string to an integer. An example is provided where a substring is extracted, stored in a variable, and then processed through a `stringstream` to achieve the desired integer conversion. The conversation also notes that the `stoi` function is part of C++11, and if the compiler does not recognize it, using `stringstream` is a viable alternative. Users express uncertainty about integrating this solution into their existing code, indicating a need for further clarification on implementation.
elmessican
Messages
6
Reaction score
0
What can I use to replace stoi since the homework program won't accept it? I'm trying to read a string and display it as an integer. I know I have to use stringstream but have no idea how.
Code:
//ReadData.cpp

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <sstream>
#include <iomanip>
#include <stdlib.h>

using namespace std;

int main() {
   
   

    //Define the required variables

    string title, head1, head2, s;
   vector<string> data;

   vector<int> point;

    //Prompt and read the title of the data

    cout << "Enter a title for the data: " << endl;

    getline(cin, title);

    cout << "You entered: " << title << endl;
    cout << endl;

    //Prompt and read the headers of two columns

    cout << "Enter the column 1 header: " << endl;

    getline(cin, head1);

    cout << "You entered: " << head1 << endl;
    
        cout << endl;    cout << "Enter the column 2 header: " << endl;

    getline(cin, head2);

    cout << "You entered: " << head2 << endl;
        cout << endl;    //Use while loop to read the data points until user enters -1

    while (1) {

         cout << "Enter a data point (-1 to stop input): " << endl;

         getline(cin, s);

         if (s == "-1") {

             break;

         }

         if (s.find(',') == -1) {

             cout << "Error: No comma in string." << endl;
               cout << endl;
         }

         else {

             int count = 0;

             for (int i = 0; i<s.length(); i++) {

                 if (s.at(i) == ',') {

                      count++;

                      if (count > 1) {

                          break;

                      }

                 }

             }

             //If the input have single ',', then display the data string and data integer

             if (count == 1) {

                 data.push_back(s.substr(0, s.find(',')));

               point.push_back(stoi(s.substr(s.find(',') + 1, s.length() - 1)));

                 cout << "Data string: " << s.substr(0, s.find(',')) << endl;
         

                 cout << "Data integer: 6" << stoi.s.substr(s.find(',') + 1, s.length() - 1) << endl;
                  cout << endl;
             }

             else {

                 //Display the appropriate error message

                 cout << "Error: Too many commas in input." << endl;
                  cout << endl;
             }

         }

    }

    //Display the data in the required format

    cout << "\t" << title << "\t" << endl;

    cout << head1 << "\t|\t" << head2 << endl;

    for (int i = 0; i<data.size(); i++) {

         cout << data[i] << "\t|\t" << point[i] << endl;

    }

    for (int i = 0; i<data.size(); i++) {

         cout << data[i] << " ";

         for (int j = 0; j<point[i]; j++) {

             cout << "*";

         }

         cout << endl;

    }

    

    return 0;

}
 
Technology news on Phys.org
As I could see you've used stoi incorrectly.. are you sure it's not
Code:
stoi(substr(...))
You've used
Code:
stoi.substr(...)

stoi - C++ Reference
If you want to use string streams,

Code:
string tempstring = substring(...);
int tempint;

stringstream strstream;

strstream << tempstring;
strstream >> tempint;
 
I changed that and I still get this error apparently the compiler is not up to date with c++11 and doesn't recognizes 'stoi', how can I add the stringstream to my code. It would be a great help if you could show me. Thanks.main.cpp:97:78: error: 'stoi' was not declared in this scope
point.push_back(stoi(s.substr(s.find(',') + 1, s.length() - 1)));
 
Oh yeah std::stoi is a c++11 feature so unless your complier doesn't support c++11 you won't be able to use it. I have given you an example on how to use the stringstream, is there anything not clear about it?
 
Yea I think I get it but i just don't know how or where to added to my code lol
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Back
Top