Vykmon Vykmon - 27 days ago 7
C++ Question

Borland C++ 5.02 cout not showing on the console window

I'm sorry for this newbie question,
i tried to make this program using borland 5.02, but for some reason the cout in the if (stat) isn't showing on the console window when i write married. I don't know what's wrong and i've been stuck for hours. please help me

#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 () ;

}


Update : i have tried to change the
gets(STAT) ;
to
cin >> STAT ;
before but it doesn't seem to have any effect. the program looked like this when i run them




ID Number : 0123141421

Name : Vykmon

Faction : 1

Status : married (Here is the problem)

-Results-

Your GP: 1500000

Your TI: 0

Your TA: 0

Your TG: 1500000




Even though i have write married on the status, the
cout << "Number of children : " << endl ;
didn't show up on console window. it as if the
if (STAT == "Married" || STAT == "married")
aren't working, and the "Status: married" count as

else
{
TI = 0 ;
TA = 0 ;
}

Answer

STAT == "Married" would work in standard C++ if STAT were a std::string.

However, it's an array of char, and that means that you're comparing two pointers. Because C++ doesn't support direct comparison of arrays. So each of the two array expressions decay to pointer to first item.

And these pointers are guaranteed to be different.


Note 1: Borland C++ 5.02 sounds like mid 1990's, before the first C++ standard. There are a host of free modern compilers. The three most well known are g++, clang and Visual C++ (the latter is only available for the PC platform).

Note 2: As I recall the std::string in Borland C++ was totally botched. If just using std::string doesn't work, consider using strcmp to compare C strings (like the arrays you have).

Note 3: In standard C++ (since the first standard in 1998) there is no <iostream.h> header. Instead include <iostream>, and possibly add a using namespace std;. Or appropriate using directives, or qualifications of names like cout and endl, i.e. writing std::cout and std::endl.


In other news:

  • By reserving SHOUTCASE names for macros, you can ease the code reading experience significantly, and avoid unintended text substitutions to boot. Not to mention conforming to the common convention about this.

  • By using getline instead of >>, the program can read a name with a space in it. However, this only works nicely when the input buffer is empty (since getline doesn't skip whitespace). So, it's something to consider, but it can involve some work.

Comments