Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

40,562 questions

52,727 answers

573 users

How to convert string to int in C++

6 Answers

0 votes
#include <iostream>
#include <sstream> 

int main() {
    auto s = "98735";
    
    std::stringstream ss(s); 
    
    int n; 
    ss >> n;
     
    std::cout << n;
}



/*
run:

98735

*/

 



answered Jun 3, 2020 by avibootz
0 votes
#include <iostream>

int main() {
    auto s = "98735";

    int n = std::stoi(s); 

    std::cout << n;
}
 
 
 
/*
run:
 
98735
 
*/

 



answered Jun 4, 2020 by avibootz
0 votes
#include <iostream>
#include <sstream> 
 
int main() {
    auto s = "98735";

    int n;
    std::istringstream(s) >> n;
    
    std::cout << n;
}
 
 
 
/*
run:
 
98735
 
*/

 



answered Jun 4, 2020 by avibootz
0 votes
#include <iostream>
#include <sstream>
  
int convertToInt(std::string str) {
   int n;
 
   std::stringstream ss(str);
   ss >> n;
     
   return n;
}
  
int main()
{
   std::string str = "903487";
     
   int num = convertToInt(str);
     
   std::cout << num;
} 
    
   
    
    
/*
run:
    
903487
    
*/

 



answered Apr 8, 2024 by avibootz
0 votes
#include <iostream>
 
int main()
{
   std::string str = "903487";
     
   int num = std::stoi(str);
     
   std::cout << num;
} 
    
   
    
    
/*
run:
    
903487
    
*/

 

 



answered Apr 8, 2024 by avibootz
0 votes
#include <iostream>
  
int convertToInt(std::string str) {
   int n;
   sscanf(str.c_str(), "%d", &n);
 
   return n;
}
  
int main()
{
   std::string str = "903487";
     
   int num = std::stoi(str);
     
   std::cout << num;
} 
    
   
    
    
/*
run:
    
903487
    
*/

 



answered Apr 8, 2024 by avibootz

Related questions

2 answers 141 views
2 answers 132 views
132 views asked Nov 26, 2022 by avibootz
1 answer 107 views
107 views asked Oct 15, 2022 by avibootz
2 answers 156 views
1 answer 169 views
169 views asked Feb 21, 2022 by avibootz
3 answers 277 views
1 answer 143 views
...