我正在爲一個班級製作一個小型辦公桌費用計劃。我想包括一個循環。但是,每當我到達程序結束並將其循環回到開始時,它都會跳過我要求客戶名稱的部分,並將其留空。任何想法如何解決它?爲什麼在循環內部不重置字符串?
這裏是我的代碼:
#include <iostream> // needed for Cin and Cout
#include <string> // needed for the String class
#include <math.h> // math functions
#include <stdlib.h>
using namespace std;
#define baseCost 200.00
#define drawerPrice 30.00
int main(void)
{
while(true)
{
string cname;
char ch;
cout << "What is your name?\n";
getline(cin, cname);
cout << cname;
cout << "\nWould you like to do another? (y/n)\n";
cin >> ch;
if (ch == 'y' || ch == 'Y')
continue;
else
exit(1);
}
return 0;
}
你有一個while循環圍繞main?這甚至編譯? >。< – OMGtechy
由於您已經發現問題出在循環和字符串上,您是否可以將代碼簡化爲這樣?這讓每個人都更容易。 (P.S:'while(true)int main(){...'what?) – Borgleader
請編輯你的問題以提供[mcve]。 –