【问题标题】:Problems with spacing in variables变量间距问题
【发布时间】:2016-08-10 03:58:01
【问题描述】:

我正在为书籍构建引用机器。但是,我在引用时遇到了问题。这是我的代码:

#include <iostream>
using namespace std;
string nameofbook,lauthor,x,fmauthor,year,publisher,citystate;
int main(){
cout<<"Welcome to the citation machine. What do you want to cite?"<<endl;
cin>>x;
if(x == "book"){
cout<<"What is the name of the book?"<<endl;
cin>>nameofbook;
cout<<"What is the last name of the author?"<<endl;
cin>>lauthor;
cout<<"What is the first and middle name of the author in abbreviations?"<<endl;
cin>>fmauthor;
cout<<"Which year is the book published?"<<endl;
cin>>year;
cout<<"Who is the publisher?"<<endl;
cin>>publisher;
cout<<"Which state or city is the publisher located at?"<<endl;
cin>>citystate;
cout<<"You are done!"<<endl;
cout<<"The citation is"<<endl;
cout<<lauthor;
cout<<" ";
cout<<fmauthor;
cout<<" ";
cout<<"(";
cout<<year;
cout<<")" ;
cout<<" ";
cout<<nameofbook;
cout<<" ";
cout<<citystate;
cout<<":";
cout<<" ";
cout<<publisher;

}


return 0; 
}

当我在 Dev C++ 中编译并运行它时,它给了我这个:

Welcome to the citation machine. What do you want to cite?
book
What is the name of the book?
Catching Fire
What is the last name of the author?
What is the first and middle name of the author in abbreviations?
Collins S.
Which year is the book published?
Who is the publisher?
Scholastic Corporation
Which state or city is the publisher located at?
You are done!
The citation is
Fire Collins (S.) Catching Corporation: Scholastic
--------------------------------
Process exited after 88.09 seconds with return value 0
Press any key to continue . . .

当我让所有细节成为一个词时,它变成:

Welcome to the citation machine. What do you want to cite?
book
What is the name of the book?
Mockingjay
What is the last name of the author?
Collins
What is the first and middle name of the author in abbreviations?
S.
Which year is the book published?
2009
Who is the publisher?
Scholastic
Which state or city is the publisher located at?
NYC
You are done!
The citation is
Collins S. (2009) Mockingjay NYC: Scholastic
--------------------------------
Process exited after 72.02 seconds with return value 0
Press any key to continue . . .

因此,当所有细节都来自一个单词时,它就可以完美运行。但是当任何细节超过一个词时,第一个词来回答它应该回答的问题,第二个词来回答下一个问题。那么我应该如何让这两个词回答它应该回答的问题呢?如果有人能帮我解决这个问题,我将不胜感激。

【问题讨论】:

标签: c++


【解决方案1】:

而不是做

cin >> nameofbook;

试试

getline(cin, nameofbook);

其余的也是如此。当您只执行cin &gt;&gt; x 时,它只会读取到下一个空格。如果你想阅读整行,你应该做getline

【讨论】:

    【解决方案2】:

    您可以使用getline() 而不是cin,因为它从string 获取令牌。

     cout<<"What is the first and middle name of the author in abbreviations?"<<endl;
    
     getline (std::cin, fmauthor); // and as for other qsns
    

    【讨论】:

      【解决方案3】:

      您需要使用 std::getline(cin,stringName) 函数进行输入,该函数接受整行作为输入,直到您按下换行符为止。使用 cin 获取字符串输入会在出现第一个空格字符时中断输入。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-06-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多