如何在C ++中相乘

时间:2015-02-06 20:39:17

标签: c++

基本上我坚持这个:

#include <iostream>
#include <string>

using namespace std;

int main() {
    cout << "please enter your first name and age\n"; //prompts user to enter name and age
    string first_name; //string variable
    int age; //integer variable
    cin first_name; //reads first_name
    cin age; //reads age
    cout << "Hello " << first_name << " (age" << age << ")\n"; 
}

嗨,我刚刚开始使用c ++两天前,我想帮忙。我想将int年龄乘以12,我可以这样做吗?

cin age*12 

或者我把它放在这里

int age*12

再次,对不起,这是一个新问题,但我刚刚开始两天前。我甚至不确定它是否有错误,但我只需要回答这部分内容。

3 个答案:

答案 0 :(得分:3)

你的程序应该是这样的:

int main() {
   cout << "please enter your first name and age\n"; //prompts user to enter name and age
   string first_name; //string variable
   int age; //integer variable
   cin >> first_name; //reads first_name
   cin >> age; //reads age

   int result = age * 12;
   cout << "Hello " << first_name << " (age" << age << ")\n"; 
   cout << "Your age multiplied by 12 is " << result << "\n";
}

答案 1 :(得分:2)

创建一个名为int agetwo的新int,然后执行agetwo = age*12并打印agetwo

基本上,

int agetwo=age*12;
cout << agetwo;

正如其他人所说,也可以做到

cout << age*12

只是您使用的风格偏好。

答案 2 :(得分:0)

如果您只想输出age * 12的值,那么您可以说:

std::cout << age * 12;

如果您想存储age * 12的值,那么您可以说:

age = age * 12;