私はC++を初めてお使いになり、今日より早く学習を始めました。私は、2つの入力を取り、出力を印刷する計算機を作ろうとしています。文字列をdoubleに変換して出力する方法は?
文字列をダブルに変換する方法がわかりません。どのようにしたらよいでしょうか? (サンプルコードを記載してください!)これは私がこれまで持っているものである
:
#include <iostream>
using namespace std;
int main() {
string input = "";
string numberOne;
string numberTwo;
double output;
char myChar = {0};
while(true) {
cout << "Add (A), Subtract (S), Multiply (M), Divide (D):\n> ";
getline(cin, input);
if (input.length() == 1) {
myChar = input[0];
if (input == "A") {
cout << "Enter a number to add:\n> ";
getline(cin, numberOne);
cout << "Enter a number to add:\n> ";
getline(cin, numberTwo);
output = numberOne + numberTwo; //I know that I cannot do this because you can't
// add two strings and get a double, I just don't
// know how to make these two strings into doubles (or any other type). Any suggestions?
cout << "The sum is: " + output << endl;
output = numberOne + numberTwo
break;
}
if (input == "S") {
}
if (input == "M") {
}
if (input == "D") {
}
}
cout << "Invalid character, please try again" << endl;
}
return 0;
}
以下のShreevardhanの提案を使用するべきでしょうが、 'string'を' double'に変換する必要がある場合、C++ 11は 'std :: stod'関数を提供します。 'std :: stringstream'は別のオプションです。 –