C++语言学习记录-2:利用cin进行输入操作

cin的简单介绍

首先来看这样一个问题:当cout被称为一种将相关数据插入输出流的运算时,有没有一种手段可以将数据放入输入流中呢?

cin就是用于从键入设备读取相关信息并将其插入输入流,因此cin是一个标准输入流对象。

在现在的学习过程中,首先需要掌握的cin的使用方法就是cin>>。

在理解cin时,不得不提标准输入缓冲区。当我们从键盘输入字符串的时候需要敲一下回车键才能够将这个字符串送入到缓冲区中,那么敲入的这个回车键(\r)会被转换为一个换行符\n,这个换行符\n也会被存储在cin的缓冲区中并且被当成一个字符来计算!比如我们在键盘上敲下了11111这个字符串,然后敲一下回车键(\r)将这个字符串送入了缓冲区中,那么此时缓冲区中的字节个数是6 ,而不是5。

cin>>的用法

先来看一个问题:当一个用户需要设置他的密码时,想在键入密码后看到反馈的密码来验证自己键入是否正确,这时就可以用以下程序实现。

#include<iostream>
using namespace std;
int main()
{
	int a;
	cout<<"please type your password in"<<endl;
		cin>> a;
		cout<<"your password is" << a;
	return 0;
}

利用这种方式,在运行后会跳出窗口让用户键入他的密码,当用户按下回车键确定后,将会再重新向用户返回这段密码。

在这里,利用“a”对后面键入的密码进行替换,之后再利用cout输出a的值,当然,a就需要预先做一个定义。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的 C++ 代码示例,实现了上述课程设计的基本要求和拓展功能: ```c++ #include <iostream> #include <fstream> #include <string> #include <vector> #include <algorithm> #include <iomanip> using namespace std; struct Record { int id; string date; string type; double amount; double balance; string currency; string category; string note; }; vector<Record> records; void addRecord(bool isIncome) { Record r; r.id = records.size() + 1; r.type = isIncome ? "收入" : "支出"; cout << "请输入日期(格式为YYYY-MM-DD):"; cin >> r.date; cout << "请输入金额:"; cin >> r.amount; cout << "请输入货币种类:"; cin >> r.currency; cout << "请输入分类:"; cin >> r.category; cout << "请输入备注:"; cin >> r.note; if (records.empty()) { r.balance = r.amount; } else { Record& last = records.back(); r.balance = last.balance + (isIncome ? r.amount : -r.amount); } records.push_back(r); cout << "添加成功!" << endl; } void showRecords(const vector<Record>& rs) { cout << "+----+------------+--------+----------+----------+--------------+----------------+------------------------+" << endl; cout << "| ID | 日期 | 类型 | 金额 | 余额 | 货币种类 | 分类 | 备注 |" << endl; cout << "+----+------------+--------+----------+----------+--------------+----------------+------------------------+" << endl; for (const auto& r : rs) { cout << "| " << setw(2) << r.id << " | " << setw(10) << r.date << " | " << setw(6) << r.type << " | " << setw(8) << fixed << setprecision(2) << r.amount << " | " << setw(8) << fixed << setprecision(2) << r.balance << " | " << setw(12) << r.currency << " | " << setw(14) << r.category << " | " << setw(24) << r.note << " |" << endl; } cout << "+----+------------+--------+----------+----------+--------------+----------------+------------------------+" << endl; } void showDetail() { cout << "请输入起始日期(格式为YYYY-MM-DD):"; string start, end; cin >> start; cout << "请输入结束日期(格式为YYYY-MM-DD):"; cin >> end; vector<Record> rs; for (const auto& r : records) { if (r.date >= start && r.date <= end) { rs.push_back(r); } } if (rs.empty()) { cout << "没有符合条件的记录!" << endl; } else { showRecords(rs); } } void showSummary(bool byMonth) { cout << "请输入年份:"; int year; cin >> year; vector<string> dates; for (const auto& r : records) { if (stoi(r.date.substr(0, 4)) == year) { string date = byMonth ? r.date.substr(0, 7) : r.date.substr(0, 4); if (find(dates.begin(), dates.end(), date) == dates.end()) { dates.push_back(date); } } } if (dates.empty()) { cout << "没有符合条件的记录!" << endl; return; } cout << "+--------------+----------+----------+" << endl; cout << "| 日期 | 收入 | 支出 |" << endl; cout << "+--------------+----------+----------+" << endl; double totalIncome = 0; double totalExpense = 0; for (const auto& date : dates) { double income = 0; double expense = 0; for (const auto& r : records) { if (byMonth && r.date.substr(0, 7) == date || !byMonth && r.date.substr(0, 4) == date) { if (r.type == "收入") { income += r.amount; } else { expense += r.amount; } } } totalIncome += income; totalExpense += expense; cout << "| " << setw(12) << date << " | " << setw(8) << fixed << setprecision(2) << income << " | " << setw(8) << fixed << setprecision(2) << expense << " |" << endl; } cout << "+--------------+----------+----------+" << endl; cout << "| 总计 | " << setw(8) << fixed << setprecision(2) << totalIncome << " | " << setw(8) << fixed << setprecision(2) << totalExpense << " |" << endl; cout << "+--------------+----------+----------+" << endl; } void saveToFile() { ofstream ofs("records.txt"); for (const auto& r : records) { ofs << r.id << " " << r.date << " " << r.type << " " << r.amount << " " << r.balance << " " << r.currency << " " << r.category << " " << r.note << endl; } ofs.close(); cout << "保存成功!" << endl; } void loadFromFile() { ifstream ifs("records.txt"); if (!ifs) { cout << "文件不存在!" << endl; return; } records.clear(); string line; while (getline(ifs, line)) { Record r; sscanf(line.c_str(), "%d %s %s %lf %lf %s %s %s", &r.id, &r.date[0], &r.type[0], &r.amount, &r.balance, &r.currency[0], &r.category[0], &r.note[0]); records.push_back(r); } ifs.close(); cout << "读取成功!" << endl; } int main() { while (true) { cout << "请选择操作:" << endl; cout << "1. 添加收入记录" << endl; cout << "2. 添加支出记录" << endl; cout << "3. 显示所有记录" << endl; cout << "4. 显示明细表" << endl; cout << "5. 按月统计收支情况" << endl; cout << "6. 按年统计收支情况" << endl; cout << "7. 保存数据到文件" << endl; cout << "8. 从文件读取数据" << endl; cout << "0. 退出程序" << endl; int choice; cin >> choice; switch (choice) { case 1: addRecord(true); break; case 2: addRecord(false); break; case 3: showRecords(records); break; case 4: showDetail(); break; case 5: showSummary(true); break; case 6: showSummary(false); break; case 7: saveToFile(); break; case 8: loadFromFile(); break; case 0: return 0; default: cout << "无效的输入!" << endl; } } return 0; } ``` 这段代码使用了结构体 `Record` 来存储每一条记录的信息,使用了 STL 容器 `vector` 来存储所有记录利用C++11 的字符串格式化和输出格式控制来美化输出结果,同时也实现了文件保存和读取的功能。如果你有任何问题,可以随时问我。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值