Advertisement
danrleydaniel

stringtoint.cpp

Jan 24th, 2020
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.99 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <Math.h>
  4.  
  5. using namespace std;
  6.  
  7. int chartoint(char);
  8. int stringtoint(string);
  9.  
  10. int main(void){
  11.     int num;
  12.     string str;
  13.     char resp;
  14.  
  15.     cout << "\nTRANSFORMA STRING EM INTEIRO\n";
  16.  
  17.     begin:
  18.  
  19.     cout << "\nDigite um número: ";
  20.     cin >> str;
  21.  
  22.     num = stringtoint(str);
  23.  
  24.     cout << "\n\nNúmero digitado como string: " << str;
  25.     cout << "\nNúmero transformado em inteiro: " << num;
  26.     num = num + 1;
  27.     cout << "\nNúmero transformado em inteiro + 1: " << num << "\n";
  28.  
  29.     cout << "\nNovamente? [S ou N]: ";
  30.     cin >> resp;
  31.     if(resp == 'S' || resp == 's'){
  32.         goto begin;
  33.     }
  34. }
  35.  
  36. int stringtoint(string s){
  37.     int tam = s.size();
  38.     int j = 0;
  39.     int soma = 1;
  40.     int pos;
  41.     for(int i = tam - 1; i >= 0; i--){
  42.         pos = chartoint(s[j]);
  43.         pos = pos * pow(10, i);
  44.         soma = soma + pos;
  45.         j++;
  46.     }
  47.     return soma;
  48. }
  49.  
  50. int chartoint(char c){
  51.     return c - '0';
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement