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

  • C/C++
  • Thread starter elmessican
  • Start date
  • Tags
    C++ Program
In summary, you can use stringstream to replace stoi in order to read a string and display it as an integer.
  • #1
elmessican
6
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
  • #2
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;
 
  • #3
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)));
 
  • #4
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?
 
  • #5
Yea I think I get it but i just don't know how or where to added to my code lol
 

What is C++ and why do I need help with it?

C++ is a high-level programming language used for developing a variety of software applications, including operating systems, video games, and web browsers. It is a complex language that requires knowledge and practice to use effectively, which is why many people seek help when learning or working with C++.

How can I get help with my C++ program?

There are several ways to get help with your C++ program. You can consult online resources, such as tutorials and forums, or seek help from experienced programmers. Additionally, some schools and universities offer classes or tutoring for C++ programming.

What are some common errors in C++ programming?

Some common errors in C++ programming include syntax errors, logical errors, and runtime errors. Syntax errors occur when the code does not follow the rules of the language, logical errors occur when the code does not produce the expected result, and runtime errors occur when the program crashes or encounters an unexpected problem while running.

How can I debug my C++ program?

To debug a C++ program, you can use a debugger tool that allows you to step through your code and see how it executes. You can also use print statements to output the values of variables at different points in your program to help identify errors.

Can I practice C++ without writing a full program?

Yes, there are many online platforms that allow you to practice and experiment with C++ code without having to write and run a full program. These platforms provide a coding environment where you can write and run code, as well as access tutorials and challenges to improve your skills.

Similar threads

  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
5
Views
2K
Replies
10
Views
961
  • Programming and Computer Science
Replies
15
Views
2K
  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
2
Replies
66
Views
4K
  • Programming and Computer Science
Replies
6
Views
8K
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
3
Views
3K
  • Programming and Computer Science
3
Replies
89
Views
4K
Back
Top