我很困惑在选择中使用cin和cout [英] I am confused in using cin and cout inside a selection
本文介绍了我很困惑在选择中使用cin和cout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<string>
using namespace std;
void printBook(struct Books book);
void printBorrower(struct borrower borrow);
struct Books
{
char title[50];
char author[50];
char subject[50];
int book_id;
};
struct borrower
{
char name[50];
char course[50];
int year;
int id;
};
int main()
{
Books b1;
borrower person;
char opt;
do
{
cout<<"Enter opt: ";
cin>>opt;
if(opt=='A')
{
cout<<"Enter Book title: ";
cin.getline(b1.title,50);
cout << "Enter Book author: ";
cin.getline(b1.author,50);
cout << "Enter Book subject: ";
cin.getline(b1.subject,50);
cout << "Enter Book ID: ";
cin >> b1.book_id;
}
else if(opt=='B')
{
cout<<"Enter the borrower's name: ";
cin.getline(person.name, 50);
cout<<"Enter the borrower's course: ";
cin.getline(person.course, 50);
cout<<"Enter the borrower's year level: ";
cin>>person.year;
cout<<"Enter the borrower's ID : ";
cin>>person.id;
}
else
{
printBook(b1);
//Print Borrower
printBorrower(person);
}
}while(opt!='Y');
system("pause");
}
void printBook(struct Books book)
{
cout<<"Book title: "<<book.title<<endl;
cout<<"Book author: "<<book.author<<endl;
cout<<"Book subject: "<<book.subject<<endl;
cout<<"Book ID : "<<book.book_id<<endl;
}
void printBorrower(struct borrower borrow)
{
cout<<"Borrower's name: "<<borrow.name<<endl;
cout<<"Borrower's course: "<<borrow.course<<endl;
cout<<"Borrower's year: "<<borrow.year<<endl;
cout<<"Borrower's ID : "<<borrow.id<<endl;
}
我的尝试:
我尝试执行代码但是我似乎无法在每个选项中输入第一个语句,如果我输入选项A,它只会显示输入书名:输入书籍作者:而我不能似乎为第一行输入任何内容,同样是选项B,我的代码有问题吗?单个选择中有多少cin和cout语句有限制吗?请帮我。谢谢。
What I have tried:
I tried executing the code but then I cant seem to enter the first statement in every option, If I enter the option A, it will only show the "Enter book title: Enter book author: " and I cant seem to enter anything for the 1st line, same goes to the option B, is something wrong with my code? Is there a limit as to how many cin and cout statements inside a single selection? Please help me. Thank you.
推荐答案
使用cin>>
后跟cin.getline( )
会让你相信getline()缺少的东西:
使用C ++ I / O(输入/输出)的提示和技巧 [ ^ ]
Usingcin>>
followed bycin.getline()
can lead you to believe getline() is missing things:
Tips and Tricks for Using C++ I/O (input/output)[^]
这篇关于我很困惑在选择中使用cin和cout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文