C ++跳过了cin行

时间:2019-06-13 20:22:55

标签: c++

运行此代码块将跳过第二个cin调用(cin>>color;),并将其传递到下一行。有人可以指出我的错误吗?

#include <iostream>
using namespace std;
int main()
{
    char cod;
    string colour,material;
    float abs,ord,r;

    cout<<" CODUL : " ; cin>>cod;
    cout<<"\nCOLOUR : "; cin>>colour;
    cout<<"\nMATERIAL : "; cin>>material;
    cout<<"\nABSISA :  "; cin>>abs;
    cout<<"\nORDONATA : " ; cin>>ord;
    cout<<"\nRAZA : " ; cin>>r;
}

2 个答案:

答案 0 :(得分:1)

您输入了多个字符。阅读整行,然后解析该行。

例如,运行您的程序,我为“ CODUL”输入“ xyzzy \ n”:

./stuff
CODUL : xyzzy
cod:x

COLOUR : colour:yzzy

MATERIAL :

对于此程序的修订版:

int
main()
{
char cod;
string colour,material;
float abs,ord,r;

cout<<" CODUL : " ; cin>>cod; cout<<"cod:"<<cod<<endl;
cout<<"\nCOLOUR : "; cin>>colour; cout<<"colour:"<<colour<<endl;
cout<<"\nMATERIAL : "; cin>>material; cout<<"material:"<<material<<endl;
cout<<"\nABSISA :  "; cin>>abs; cout<<"abs:"<<abs<<endl;
cout<<"\nORDONATA : " ; cin>>ord; cout<<"ord:"<<ord<<endl;
cout<<"\nRAZA : " ; cin>>r; cout<<"ord:"<<ord<<endl;
}

答案 1 :(得分:0)

您忘记了function getLibraryDocumentsImageUrl(_chooseLib, _docId) { // var libDocId = chooseLib(); // var documentId = docId; var request = new XMLHttpRequest(); request.open( "GET", `https://some.api.link/${libDocId}/documents/${documentId}/imageUrls`, true ); request.setRequestHeader( "Authorization", "some authorization code" ); request.onload = function() { //begin accessing JSON data here var data = JSON.parse(this.response); if (request.status >= 200 && request.status < 400) { console.log(data); } else { console.log(libDocId); console.log(documentId); console.log(data); } }; request.send(); } 。没有它,您将无法使用字符串。您还希望“鳕鱼”只用一个字符吗?因为一个字符只能包含一个数字/字母/特殊字符。如果您希望它包含多个字符,则需要将其声明为字符串。或者,您可以为在该行上键入的每个字符声明多个变量。无论如何,我更新了您的代码,对我来说效果很好。

#include<string>

编辑#1

只是一段更新的代码,显示了如何输入多个不同的char,它们都存储在多个变量中。下面的代码已经过测试,可以正常工作。假设这是您要寻找的。

#include <iostream>
#include<string>
using namespace std;
int main()
{
    char cod;   //If you want more than one character declare it as string or 
               // create multiple char variables to store the multiple
              //  variables in
    string colour,material;
    float abs,ord,r;

    cout<<" CODUL : "; cin>>cod;
    cout<<"\nCOLOUR : "; cin>>colour;
    cout<<"\nMATERIAL : "; cin>>material;
    cout<<"\nABSISA :  "; cin>>abs;
    cout<<"\nORDONATA : "; cin>>ord;
    cout<<"\nRAZA : "; cin>>r;

    system("pause");
    return 0;
}