目标:用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我是一个初学者...我只需要将它们打印在一起。 –