我想编写一个读取一行和一个字符的程序,然后迭代地将它们打印在一起。这是我的代码:
#include <string>
#include <iostream>
using namespace std;
int main() {
string s;
string a;
while (getline(cin,s)) {
cin>>a;
cout<<s<<a<<endl;
}
}
我第一次输入:&#34; abc d&#34;作为一条线然后&#34; a&#34;作为一个角色和 输出是&#34; abc da&#34;。 但后来我输入&#34; abc d&#34;再次,它立即输出&#34; abc&#34;没有等我输入&#34; a&#34;然后输出&#34; abc da&#34;。我的代码在哪里错了?
答案 0 :(得分:0)
在// the image button im trying to apply
<a href='logout.php'><img class='loggers' src="img/logout.png" onmouseover="this.src='img/logout2.png'"onmouseout="this.src='img/logout.png'" style="width: 11%; height: 6%"/></a>
<form class='options' action="" method="post">
//change these to images while maintaining 'submit' type
<input type="submit" name ="grades" value="Display Grades"
<br><br>
<input type="submit" name="password" value="Change Password">
</form>
<body>
<?php
if (isset($_POST['grades'])) {
$id = $login_session;
$dbhost = 'xxxx.xxxxx.com';
$dbuser = 'xxxxxx';
$dbpass = 'xxxxxx';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db('grades_db');
$sql = "Select student_id, course_id, prelim, midterm, avg_pre_mid, post_removal, pre_final, final_grade, remarks FROM `grades`
WHERE `student_id` ='".$id."';";
$retval = mysql_query($sql,$conn);
if (!$retval) {
die ('No Grades Yet');
}
echo "<div class='table-responsive'><table class='table table-bordered table-hover'><tr><td></td><td>Student ID</td><td>Subject</td><td>Preliminaries</td><td>Midterms</td><td>Average of Preliminaries and Midterm</td><td>Post Removal</td><td>Pre-Finals</td><td>Finals</td><td>Remarks</td></tr>";
while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
echo "<tr>
<td></td>
<td>{$row['student_id']}</td>
<td>{$row['course_id']}</td>
<td>{$row['prelim']}</td>
<td>{$row['midterm']}</td>
<td>{$row['avg_pre_mid']}</td>
<td>{$row['post_removal']}</td>
<td>{$row['pre_final']}</td>
<td>{$row['final_grade']}</td>
<td>{$row['remarks']}</td>
</tr>";
}
echo "</table></div>";
mysql_close($conn);
}
之后,缓冲区中仍然有换行符,然后由cin>>a;
读取。为避免这种情况,您需要从缓冲区中清除换行符。这可以通过调用getline()
并在某些情况下调用cin.ignore()
来实现,但我不确定cin.sync()
函数的确切功能。