teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

6

コードを訂正しました

2018/02/01 08:09

投稿

Momomo.
Momomo.

スコア22

title CHANGED
File without changes
body CHANGED
@@ -16,31 +16,30 @@
16
16
 
17
17
  using namespace std;
18
18
 
19
- void toInteger(string num, int n, int x, int numInInteger){
19
+ int toInteger(string num, int n, int x, int numInInteger){
20
+ int count=num.length()-1;
20
21
 
21
22
  //base case
22
23
  if(num.length()==0)
23
- return;
24
+ return 0;
24
25
 
25
26
  //keep doing if the length is not zero
26
- while(num.length()>=1){
27
+ while(count>=0){
27
28
  numInInteger = numInInteger*n+(num.at(x)-'0');
28
-
29
+ count--;
30
+ //move position from right to left, and increase n by 10 times
31
+ toInteger(num, n=n*10, x-1, numInInteger);
29
32
  }
33
+ return numInInteger;
30
34
 
31
- //move position from right to left, and increase n by 10 times
32
- toInteger(num, n=n*10, x-1, numInInteger);
33
-
34
35
  }
35
36
 
36
- void toIntegerIterative(string num, int numInInteger){
37
+ int toIntegerIterative(string num, int numInInteger){
37
38
 
38
39
  //keep doing until the length of string
39
40
  for(int i=0; i<num.length(); i++)
40
41
  numInInteger = numInInteger*10+(num.at(i)-'0');
41
-
42
- //print result
42
+ return numInInteger;
43
- cout << "The result is " << numInInteger <<endl;
44
43
  }
45
44
 
46
45
  int main(){
@@ -57,7 +56,12 @@
57
56
 
58
57
  //call functions
59
58
  toInteger(num,n,x,numInInteger);
59
+ //print result
60
+ cout << "The result is " << numInInteger <<endl;
61
+
60
62
  toIntegerIterative(num,numInInteger);
63
+ //print result
64
+ cout << "The result is " << numInInteger <<endl;
61
65
 
62
66
  return 0;
63
67
  }

5

誤字

2018/02/01 08:09

投稿

Momomo.
Momomo.

スコア22

title CHANGED
File without changes
body CHANGED
@@ -28,7 +28,7 @@
28
28
 
29
29
  }
30
30
 
31
- //move index from right to left, and increase n by 10 times
31
+ //move position from right to left, and increase n by 10 times
32
32
  toInteger(num, n=n*10, x-1, numInInteger);
33
33
 
34
34
  }

4

ご指摘いただいた箇所を修正しました

2018/02/01 07:43

投稿

Momomo.
Momomo.

スコア22

title CHANGED
File without changes
body CHANGED
@@ -11,22 +11,25 @@
11
11
 
12
12
 
13
13
  ```c++
14
- include <iostream>
14
+ #include <iostream>
15
15
  #include <ctime>
16
16
 
17
17
  using namespace std;
18
18
 
19
- void toInteger(string num, int n, int index, int numInInteger){
19
+ void toInteger(string num, int n, int x, int numInInteger){
20
20
 
21
+ //base case
21
22
  if(num.length()==0)
22
23
  return;
23
24
 
25
+ //keep doing if the length is not zero
24
26
  while(num.length()>=1){
25
- numInInteger = numInInteger*10+(num.at(index)-'0');
27
+ numInInteger = numInInteger*n+(num.at(x)-'0');
26
28
 
27
29
  }
28
30
 
31
+ //move index from right to left, and increase n by 10 times
29
- toInteger(num, n*10, index-1, numInInteger);
32
+ toInteger(num, n=n*10, x-1, numInInteger);
30
33
 
31
34
  }
32
35
 
@@ -34,24 +37,26 @@
34
37
 
35
38
  //keep doing until the length of string
36
39
  for(int i=0; i<num.length(); i++)
37
- //
38
40
  numInInteger = numInInteger*10+(num.at(i)-'0');
39
41
 
42
+ //print result
40
43
  cout << "The result is " << numInInteger <<endl;
41
44
  }
42
45
 
43
46
  int main(){
47
+ //define variables
44
48
  string num;
45
- int n=1;
46
- int index=num.at(num.length());
47
- int numInInteger=0;
48
49
 
49
50
  //ask user to enter a string
50
51
  cout << "Enter an integer number" <<endl;
51
52
  cin >> num;
52
53
 
54
+ int n=1;
55
+ int x=num.at(num.length()-1);
56
+ int numInInteger=0;
57
+
53
58
  //call functions
54
- toInteger(num,n,index,numInInteger);
59
+ toInteger(num,n,x,numInInteger);
55
60
  toIntegerIterative(num,numInInteger);
56
61
 
57
62
  return 0;

3

実際に実行した際の状況を追加いたしました

2018/02/01 07:42

投稿

Momomo.
Momomo.

スコア22

title CHANGED
File without changes
body CHANGED
@@ -2,9 +2,14 @@
2
2
 
3
3
  期待する結果:例えばユーザーがStringで8967と入力した場合にintの8967をプリントしたいのです。
4
4
 
5
+ 実際に実行すると
5
- 自分なりに通常のループを使ったパターンと回帰のパターンを書きました。よろしければアドバイスをください。
6
+ libc++abi.dylib: terminating with uncaught exception of type std::out_of_range: basic_string
7
+ このように表示されてしまいます。
6
8
 
9
+ このコードは自分なりに通常のループを使ったパターンと回帰のパターンを書きました。よろしければアドバイスをください。
7
10
 
11
+
12
+
8
13
  ```c++
9
14
  include <iostream>
10
15
  #include <ctime>

2

期待する結果を追加しました

2018/02/01 07:25

投稿

Momomo.
Momomo.

スコア22

title CHANGED
File without changes
body CHANGED
@@ -1,9 +1,8 @@
1
1
  C++初心者です。既にライブラリにあるファンクション(atoiやstoi)を使わずに文字列Stringを数値intに変換するプログラムを作りたいです。
2
2
 
3
- 例えばユーザーがStringで8967と入力したintの8967に変換したいです。
3
+ 期待する結果:例えばユーザーがStringで8967と入力した場合にintの8967をプリントしたいです。
4
- atoiやstoiを使ってできることをatoiやstoiを使わずにしたいということです。
5
4
 
6
- 自分なりに通常のループを使ったパターンと回帰と、両方のパターンでプログラムを書いてみたのですがどうやらコードにどこかおかしいところがあるようで、思うようにできません。よろしければアドバイスをください。
5
+ 自分なりに通常のループを使ったパターンと回帰のパターンを書きました。よろしければアドバイスをください。
7
6
 
8
7
 
9
8
  ```c++

1

質問が分かりにくかったようなので例えを追加いたしました。もしまだ分かりにくかったら教えてください!

2018/02/01 07:06

投稿

Momomo.
Momomo.

スコア22

title CHANGED
File without changes
body CHANGED
@@ -1,7 +1,11 @@
1
1
  C++初心者です。既にライブラリにあるファンクション(atoiやstoi)を使わずに文字列Stringを数値intに変換するプログラムを作りたいです。
2
2
 
3
+ 例えば、ユーザーがStringで8967と入力したらintの8967に変換したいです。
4
+ atoiやstoiを使ってできることをatoiやstoiを使わずにしたいということです。
5
+
3
6
  自分なりに通常のループを使ったパターンと回帰と、両方のパターンでプログラムを書いてみたのですがどうやらコードにどこかおかしいところがあるようで、思うようにできません。よろしければアドバイスをください。
4
7
 
8
+
5
9
  ```c++
6
10
  include <iostream>
7
11
  #include <ctime>