目標:用C++編寫程序如何將整行作爲輸入到一個字符串變量後採取雙作爲輸入?
1.聲明變量:一個double類型,以及一個String類型。
2.將雙倍變量的總和打印到新行上的小數點後一位。
3.將您作爲輸入讀取的字符串連接在一起並將結果打印在新行中。 (完成這個程序)
int main()
{
double d = 4.0;
string s = "hello and welcome ";
//write ur code here
// double variable
//string variable
//i/p double from user
// i/p string from user
// print sum of double
// print concatenated string
}
樣品I/P = --- 4.0 umang馬漢特!
sample o/p ---- 8.0 hello and welcome umang mahant!
//this is my code but it isnt taking the line as input i really dont know why?
#include <iostream>
#include <iomanip>
#include <limits>
using namespace std;
int main() {
double d = 4.0;
string s = "hello and welcome ";
double b;
string s2;
cin>>b;
getline(cin, s2, '\n');
cout<<d+b<<"\n";
cout<<s<<s2<<"\n";
}
1)如果該字符串不存在於代碼中,也不在代碼輸入中,您將如何期望代碼輸出「hello and welcome」。 2)難道你不能定義你的意思是什麼_不把這行作爲input_?因爲它[以整行爲輸入](http://ideone.com/nN6W2Q)。 –
@Umang來自浦那的Mahant .Hi。將它們打印在一起並不是連接!你需要使用內置的函數:)並且只需要改變getline函數,就像忙碌的程序員所建議的那樣 – minigeek
@minigeek我是一個初學者...我只需要將它們打印在一起。 –