CIN使用char类型阵列
cin using char type array
我正在尝试使用char[]
数组从std::cin
读取字符。
这是一个简单的程序:
#include <iostream>
#include <conio.h>
#include <iomanip>
using namespace std;
int main() {
int age, years;
char name[20];
cout << "Enter your age in years: " << endl;
cin >> years;
cout << "Enter your name in years: " << name[15] << endl;
age = years * 12;
cout << " Your age in months is: " << age << endl;
cout << "Your name is: " << name[15] << endl;
return 0;
}
以及我作为输出所得到的
Enter your age in years:
19
Enter your name in years:
Your age in months is: 228
Your name is:
它不识别来自std::cin
的数组。
有人可以帮忙吗?
#include <iostream>
#include <conio.h>
#include <iomanip>
using namespace std;
int main(){
int age , years ;
char name[20];
cout <<"Enter your age in years: "<< endl;
cin >> years;
cout <<"Enter your name in years: " <<endl;
cin >> name;
age = years*12;
cout << " Your age in months is: " << age <<endl;
cout << "Your name is: "<< name <<endl;
return 0;
}
您的代码有两个区别:
cin << name;
(...)
cout << name << endl;
我假设您以为
cout&lt;&lt;"输入您的名字"&lt;&lt;名称[15]&lt;&lt;endl;
会让它要求输入。这不是cout
所做的。Cout
在控制台上打印出东西,它不要求输入。那是cin
的任务。
您也不会在数组名称之后放置[15],这只会在数组中打印出第15个字符,只要输入的名称不达到15的长度,这就是垃圾字符。<</p>
int age, years;
char name[20];
cout << "Enter your age in years: " << endl;
cin >> years;
cout << "Enter your name in years: " << endl;
age = years * 12;
cin >> name;
cout << " Your age in months is: " << age << endl;
cout << "Your name is: " << name << endl;
cin.get();
相关文章:
- ArduinoJson 6.15.2:JsonObject没有命名类型
- 防止主数据类型C++的隐式转换
- 大量序列中核苷酸类型的快速计数
- 如何从C++中的依赖类型中获得它所依赖的类型
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 将平面阵列重塑为复杂的特征类型
- 将积分类型的数组作为另一个不相关的积分类型的阵列进行访问的安全且符合标准的方法
- 如何复制包含非原始类型的阵列
- 类型 *阵列的最小或最大值的类功能 *
- 如何将字节阵列的一部分施放为特定类型
- 将字符阵列施放到另一种类型中是否违反了严格的确定规则
- CIN使用char类型阵列
- 在Boost :: Multi_array中,这是子阵列的类型
- 类型从字符阵列到std :: string
- 创建类型结构的指针阵列
- 管理多维阵列的课程!我该如何管理单元格中的不同数据类型
- 具有两种不同数据类型的2d阵列
- 多种类型的锯齿状阵列
- 对于自己类的类型,什么是一个好的2D阵列替代方案
- 从闪电战阵列获取存储类型