如何在C++中进行乘法运算

如何在C++中进行乘法运算,c++,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 ag

所以基本上我被困在这个问题上:

#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"; 
}
还是我把它放在这里

int age*12
再次抱歉,这是一个新问题,但我两天前才开始。我甚至不确定其中是否有错误,但我只需要回答这一部分

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

所以基本上

int agetwo=age*12;
cout << agetwo;
正如其他人所说,我们也可以这样做


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

std::cout << age * 12;
age = age * 12;

您的程序应如下所示:

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";
}

在读取一个值之前不能乘法,所以在cin年龄执行之前不能乘法。我不会把它放在任何地方。我会把它放在cout线上:cout。。。cin年龄//但事实并非如此。你需要像cin>>年龄这样的东西//我甚至不确定其中是否有错误——这是你的主要问题。任何编译器都会抱怨并告诉你有。@chris只是为了加强你的评论:或年龄*=12。OP确实需要获得并阅读基本文本。这类事情很早就被报道了。是的,这就是我使用的。但他没有涵盖那部分。一个好的编码标准是在使用流插入运算符时将乘法放在括号内:std::cout int main{cout@KurokoTomoko是的,可以工作,但最好将其放入变量中,以便以后在程序中更改任何内容。@jgr208也可以与std::cout expression语句内联完成,实际上不需要变量。@πάνταῥεῖ 是的,我知道我的风格,我想你可以这么说。为了让下一个人更容易阅读,但也没什么错。@KurokoTomoko如果你愿意接受,请选中“选择答案”复选标记,谢谢。
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";
}