为什么用户名二总是被跳过
Why does usernametwo always get skipped?
我正在努力学习C++,无论如何我都在if语句上。
我编写了一个程序,询问两个用户的全名和年龄(虚构用户),它询问用户1的姓名和年龄,以及用户2的年龄,但不知何故,询问用户2的姓名被跳过,最终询问用户2年龄
为什么?
这是我的代码:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string usernameone;
string usernametwo;
int age1;
int age2;
//ask the users their name and age
cout << "Hi may I know your full name ? : ";
getline ( cin, usernameone, 'n');
cout << "nHello " << usernameone << " May I know now whats your age ? : ";
cin >> age1;
cout << "Ok thanks for the information, now may I talk to the other user ? thanks.nn";
cout << "Hello may I know your full name ? : ";
getline ( cin, usernametwo, 'n');
cout << "nHello " << usernametwo << " May I know now whats your age ? : ";
cin >> age1;
if(age1 < age2)
{
cout << "looks like " << usernameone << " is older than " << usernametwo;
}
else
{
cout << "ok " << usernametwo << " is older than " << usernameone;
}
if(age2 && age1 >= 100)
{
cout << "your both lying your age can't be 100 and above";
}
cin.ignore();
cin.get();
return 0;
}
cin >> age1;
cout << "Ok thanks for the information, now may
I talk to the other user ? thanks.nn";
cout << "Hello may I know your full name ? : ";
将'n'
留在输入流中,并且您正在下一次读取中读取它
getline ( cin, usernametwo, 'n');
您可以使用以下选项忽略此字符:
cin >> age1;
cout << "Ok thanks for the information, now may
I talk to the other user ? thanks.nn";
cout << "Hello may I know your full name ? : ";
cin.ignore();
getline ( cin, usernametwo, 'n');
相关文章:
- 为什么即使使用-cudart-static进行编译,库用户仍然需要链接到cuda运行时
- 为什么用户定义的函数不按照给定的顺序对相同长度的元素进行排序?
- 在 C++ 中声明 const 对象需要用户定义的默认构造函数.如果我有一个可变成员变量,为什么不呢?
- 为什么 std::cin 不提示用户输入?
- 使用用户定义的类型 UDT 实例化 std::atomic<>。如果 UDT 具有虚函数,则 l 墨水将失败。为什么?
- 为什么左值不能改变自己。左值用户是什么意思?
- 用户定义的转换不适用于可变参数函数参数?为什么不呢?
- 为什么在代码块中执行时会跳过输入用户输入的行?
- 为什么我的代码在 c++ 中无限循环.我的代码需要反复提示用户
- 为什么用户定义的转换函数模板不能有推导的返回类型?
- 为什么需要编译用户定义的文件?C++
- 为什么共享内存(在IPC中)不需要上下文切换?这是内核空间的内存,可以映射到用户空间
- 为什么用户定义的转换不会在调用对象上隐式发生
- 为什么此代码无法正确读取用户输入
- 为什么没有用户提供的cp/mv ctor并且具有虚函数但没有虚拟基的类没有"trival cp/mv ctor"?
- 为什么我的C++控制台在即将接受用户输入时关闭
- 为什么复制构造函数不是微不足道的,因为有一个用户定义的析构函数?
- 为什么我可以阻止基元的隐式转换,而不是用户定义类型的隐式转换
- 为什么基元类型和用户定义类型在从函数返回为"const"时的行为不同?
- 为什么转换涉及两个用户定义的转换功能/构造函数