将格式化的输入提取器与getline混合会导致cout显示粘在一起 [英] Mixing formatted input extractors with getline causes cout display to stick together

查看:62
本文介绍了将格式化的输入提取器与getline混合会导致cout显示粘在一起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码可用于建立电影数据库并对其进行查询.它按照预期的方式工作,只是将第一个cout语句聚集在一起.代码如下.

The following code seves to establish a movie database and query through it. It works as intended except for the first cout statements being clumped together. The code is as follows.

// array of structures
#include <iostream>
#include <string>
#include <sstream>
#include <stdio.h>
#include <cctype>
using namespace std;

#define NUM_MOVIES 6
//structure
struct movies_iit{
    string title;
    int year;
} films [NUM_MOVIES];
//global variables
char title [20], y, n;
int year;
string search;
//function 1
void sort_on_title(movies_iit films[], int n)
{
//Local struct variable used to swap records
movies_iit temp;    

for(int i=0; i<n-1; i++)
    {
for(int i=0; i<n-1; i++)
        {
/*If s[i].title is later in alphabet than
s[i+1].title, swap the two records*/
        if(films[i].title>films[i+1].title)
            {
                temp = films[i];
                films[i] = films[i+1];
                films[i+1] = temp;
            }  
        }
    }
}
//end function 1
//function query1 prototype
void query1 (movies_iit movie);
//function query2 prototype
void query2 (movies_iit movie);
//function 2 prototype
void printmovie (movies_iit movie);
//beginning of main
int main ()
{
//login
//username: user
//password: word
string mystr;
int n;
char response;
string pass;
string name;
cout << "enter your username "<<endl;
cin >> name;
cout << "enter your password "<<endl;
cin >> pass;

cout << "\n" << endl;

if (name == "user" && pass == "word")
    cout << "Welcome, user." << endl;
else
{cout << "###" <<  "unrecognized username/password combination" << "\t" << "please try again"     << "###" << endl;
    system("PAUSE");
    return 0;
    }
for (n=0; n<NUM_MOVIES; n++)
{
    cout << "Enter title: ";
    getline (cin,films[n].title);
    cout << "Enter year: ";
    getline (cin,mystr);
    stringstream(mystr) >> films[n].year;
}
//sort records, function 1 call
sort_on_title(films, NUM_MOVIES);
cout << "\nYou have entered these movies:\n";
for (n=0; n<NUM_MOVIES; n++) 
    printmovie (films[n]);  //function 2 call
//Query 1
cout << "Perform an alphabetical search? (y/n)" << endl;
cin >> response;

if (response == 'y')
    {cout << "Please enter title" << endl;
    cin >> title;
    for (n=0; n<NUM_MOVIES; n++)
        {query1 (films[n]);
            response == n;
        }
    }
else if (response == 'n')
    cout << "\n" << endl;
else
    cout << "invalid entry" << endl;
//Query 2
cout << "Perform a chronological search? (y/n)" << endl;
cin >> response;
//greater than
if (response == 'y')
{   cout << "greater than what year?" << endl;
    cin >> year;
    for (n=0; n<NUM_MOVIES; n++)
        {   query2 (films[n]);
        }
}
else if (response == 'n')
    cout << "Thank you, goodbye." << endl;
else
    cout << "invalid entry" << endl;
system("pause");
return 0;
}
//end of  main
//function 2 definition
void printmovie (movies_iit movie)
{
  cout << movie.title;
  cout << " (" << movie.year << ")\n";
}
//function query1 defintion
void query1 (movies_iit movie)
{
  if (movie.title == title)
      {cout << " >> " << movie.title;
  cout << " (" << movie.year << ")\n";}
  else
      {cout << movie.title;
  cout << " (" << movie.year << ")\n";}
}
//function query2 definition
void query2 (movies_iit movie)
{
  if (movie.year >= year)
      {cout << movie.title;
        cout << " (" << movie.year << ")\n";
      }
}

我设法将问题追溯到代码开头的登录名,而没有

I managed to trace the problem back to the login at the beginning of the code, without the

cout << "enter your username "<<endl;
cin >> name;
cout << "enter your password "<<endl;
cin >> pass;

cout << "\n" << endl;

if (name == "user" && pass == "word")
    cout << "Welcome, user." << endl;
else
{cout << "###" <<  "unrecognized username/password combination" << "\t" << "please try again"     << "###" << endl;
    system("PAUSE");
    return 0;
    }

部分,代码工作正常.为什么我的代码开头会粘在一起,如何在不忽略登录名的情况下进行修复?

section, the code works fine. Why is the beginning of my code sticking together, and how can I fix it without omitting the login?

推荐答案

cin >> pass停止在空白处读取,但仍在缓冲区中.当您调用getline时,它将看到换行符在缓冲区中等待并返回.您需要先清空缓冲区,然后再调用getline.

cin >> pass stops reading at the whitespace, but it is still there in the buffer. When you call getline it sees the newline waiting in the buffer and returns. You need to empty the buffer before you call getline.

请参阅: http://en.cppreference.com/w/cpp/io/manip/ws

这里似乎也有错误:

for (n=0; n<NUM_MOVIES; n++)
    {query1 (films[n]);
        response == n;
    }

应该是response = n吗?就目前而言,该声明没有任何用处.

Should that be response = n? As it stands that statement has no utility.

这篇关于将格式化的输入提取器与getline混合会导致cout显示粘在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆