[英]Outputting text before a break in C++
我正在嘗試運行一個程序,該程序輸出一些文本行,然后在輸入“ |”時退出(或至少顯示“按任意鍵以繼續”),但是,當我運行該程序並輸入“ |”時,它很簡單說“按任意鍵繼續”。因此它不會輸出我想要的文本。
這是我嘗試達到的目標的代碼:
if(value == '|')
{
cout << lowest << lowest_unit << " is the lowest number.\n";
cout << highest << highest_unit << " is the highest number. \n";
break;
}
如果原因不在上述代碼中,那么這就是我程序的所有代碼:
int main()
{
double value;
double realvalue;
double lowest = 0;
double highest = 0;
string unit;
string highest_unit;
string lowest_unit;
cout << "Please enter a number follow by a unit: ";
//convert units to cm for all
while (cin >>value>>unit)
{
if(unit=="cm")
{
realvalue = value;
}
else if(unit=="in")
{
realvalue = value*2.54;
}
else if(unit=="m"){
realvalue = value*100;
}
else if(unit=="ft")
{
realvalue = value*12*2.54;
}
else
{
cout << "Your units are incorrect.\n";
break;
}
if (lowest == 0 && highest == 0)
{
lowest = value;
highest = value;
lowest_unit = unit;
highest_unit = unit;
cout << lowest << highest_unit << " is the smallest so far.\n"
<< highest << lowest_unit <<" is the largest so far.\n";
}
else
{
if (realvalue < lowest)
{
lowest = value;
lowest_unit = unit;
cout << value << lowest_unit << " is the smallest so far.\n"
<< highest << highest_unit << " is still the highest.\n";
}
else
{
if (realvalue > highest)
{
highest = value;
highest_unit = unit;
cout << lowest << lowest_unit << " is still the lowest.\n"
<< highest << highest_unit << " is the largest so far.\n";
}
else
{
cout << lowest << lowest_unit << " is still the lowest.\n"
<< highest << highest_unit << " is still the highest.\n";
}
}
}
cout << "\nPlease enter another number: ";
if(value == '|')
{
cout << lowest << lowest_unit << " is the lowest number.\n";
cout << highest << highest_unit << " is the highest number. \n";
break;
}
}
}
(它的基本作用是比較用戶輸入的不同長度,然后確定用戶輸入的最小長度和最大長度)。
如何在顯示“按任意鍵繼續”之前(或更准確地說,在輸出時)實現輸出文本的目標?
value
是一倍。 鍵入|時,它不會被讀取為數字,也不會設置value
。 而是應在cin中設置一個故障位。
比較value == '|'
檢查value
等於ASCII字符|的數字值,恰好是124,這不是您想要的。
您的程序每次在循環中都需要兩個輸入。
如果要在第一個輸入處停止,則需要分兩個步驟讀取輸入。
另外,您的value
是數字,而不是字符串,您不能以相同的方式讀取數字和字符串。
while (1) {
string tmp;
if (!(cin >> tmp)) {
break;
}
if (tmp == "|") {
break;
}
//convert string into double
value = stod(tmp); //todo, check conversion was well done
if (! (cin >> unit) ) {
break;
}
// rest of code
}
"|"
不是可以解釋為double
,因此您應該使用std::string
讀取輸入。
您的代碼應如下所示:
#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::stod;
using std::string;
int main()
{
double value;
double realvalue;
double lowest = 0;
double highest = 0;
string value_str;
string unit;
string highest_unit;
string lowest_unit;
cout << "Please enter a number follow by a unit: ";
//convert units to cm for all
while (cin >>value_str>>unit)
{
if (value_str == "|") // use string literal, not character constant
{
cout << lowest << lowest_unit << " is the lowest number.\n";
cout << highest << highest_unit << " is the highest number. \n";
break;
}
value = stod(value_str);
// do works with value and unit
cout << value << "\t" << unit << "\n";
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.