【发布时间】:2016-11-06 07:22:59
【问题描述】:
对于这个新手问题,我很抱歉, 我尝试使用 borland 5.02 制作这个程序,但是由于某种原因,当我写结婚时,如果(stat)中的 cout 没有显示在控制台窗口上。我不知道出了什么问题,我已经被困了好几个小时了。请帮帮我
#include <stdio.h>
#include <conio.h>
#include <iostream.h>
int main()
{
int NIP, GOL, GP, TI, TA, JA, TG ;
char NM[20], STAT[10] ;
cout << "ID Number : " ;
cin >> NIP ;
cout << "Name : " ;
cin >> NM ;
cout << "Faction : " ;
cin >> GOL ;
if (GOL == 1)
{
GP = 1500000 ;
}
else if (GOL == 2)
{
GP = 2000000 ;
}
else
{
GP = 2500000 ;
}
cout << "Status : " ;
gets (STAT) ;
if (STAT == "Married" || STAT == "married")
{
cout << "Number of children : " << endl ;
cin >> JA ;
TI = 0.05 * GP ;
if (JA <= 3)
{
TA = 0.02 * GP * JA ;
}
else
{
TA = 0.02 * GP * 3 ;
}
}
else
{
TI = 0 ;
TA = 0 ;
}
TG = GP + TI+ TA ;
cout << endl << "-Results-" << endl ;
cout << "Your GP: " << GP << endl ;
cout << "Your TI: " << TI << endl ;
cout << "Your TA: " << TA << endl ;
cout << "Your TG: " << TG << endl ;
getch () ;
}
更新:我之前曾尝试将gets(STAT) ; 更改为cin >> STAT ;,但似乎没有任何效果。当我运行它们时,程序看起来像这样
身份证号码:0123141421
名称:维克蒙
阵营:1
状态:已婚(这是问题所在)
-结果-
您的全科医生:1500000
你的 TI:0
你的助教:0
你的TG:1500000
即使我在状态上写了结婚,cout << "Number of children : " << endl ; 并没有出现在控制台窗口上。好像if (STAT == "Married" || STAT == "married") 不起作用,并且“状态:已婚”算作
else
{
TI = 0 ;
TA = 0 ;
}
【问题讨论】:
-
STAT == "Married"不是比较2个char数组的方法 -
(
gets()不推荐使用,因为它没有提供防止缓冲区溢出的方法。局部变量的 CAPS 名称是 - 不寻常的。)你试图找出发生了什么?与调试器交朋友。cout << '|' << STAT << '|' << endl;紧跟在std::cin.get(STAT, sizeof STAT);之后。试试"Married" == STAT和"Married" == string(STAT)。 -
修复了
main声明:C++ 从来没有“隐式 int”。
标签: c++ console-application borland-c++